COMP 110: Introduction to Programming Tyler Johnson Mar 23, 2009 MWF 11:00AM-12:15PM Sitterson 014.

Slides:



Advertisements
Similar presentations
COMP 110: Introduction to Programming Tyler Johnson Feb 11, 2009 MWF 11:00AM-12:15PM Sitterson 014.
Advertisements

COMP 110: Introduction to Programming Tyler Johnson Feb 18, 2009 MWF 11:00AM-12:15PM Sitterson 014.
COMP 110: Introduction to Programming Tyler Johnson Feb 25, 2009 MWF 11:00AM-12:15PM Sitterson 014.
COMP 110: Introduction to Programming Tyler Johnson Mar 16, 2009 MWF 11:00AM-12:15PM Sitterson 014.
COMP 110: Introduction to Programming Tyler Johnson Apr 20, 2009 MWF 11:00AM-12:15PM Sitterson 014.
COMP 110: Introduction to Programming Tyler Johnson Apr 13, 2009 MWF 11:00AM-12:15PM Sitterson 014.
COMP 110: Introduction to Programming Tyler Johnson January 26, 2009 MWF 11:00AM-12:15PM Sitterson 014.
COMP 110: Introduction to Programming Tyler Johnson Feb 23, 2009 MWF 11:00AM-12:15PM Sitterson 014.
COMP 110: Introduction to Programming Tyler Johnson January 12, 2009 MWF 11:00AM-12:15PM Sitterson 014.
COMP 110: Introduction to Programming Tyler Johnson Mar 25, 2009 MWF 11:00AM-12:15PM Sitterson 014.
COMP 110: Introduction to Programming Tyler Johnson Apr 8, 2009 MWF 11:00AM-12:15PM Sitterson 014.
COMP 110: Introduction to Programming Tyler Johnson Apr 1, 2009 MWF 11:00AM-12:15PM Sitterson 014.
COMP 110: Introduction to Programming Tyler Johnson January 28, 2009 MWF 11:00AM-12:15PM Sitterson 014.
COMP 110: Introduction to Programming
COMP 110: Introduction to Programming Tyler Johnson Mar 2, 2009 MWF 11:00AM-12:15PM Sitterson 014.
COMP 110: Introduction to Programming Tyler Johnson Apr 27, 2009 MWF 11:00AM-12:15PM Sitterson 014.
COMP 110: Introduction to Programming Tyler Johnson Feb 4, 2009 MWF 11:00AM-12:15PM Sitterson 014.
Tax Year TYPES OF PAYMENTS 1040 PG 2 Line & 68 Federal income tax withheld from W-2s, 1099s Estimated payments & $ applied from prior year.
Lecture 10 Methods COMP1681 / SE15 Introduction to Programming.
1 Cathay Life Insurance Ltd. (Vietnam) 27/11/20091.
1 public class Newton { public static double sqrt(double c) { double epsilon = 1E-15; if (c < 0) return Double.NaN; double t = c; while (Math.abs(t - c/t)
February 12, 2013 COMP Introduction to Programming For Statement and More Loops Haohan Li TR 11:00 – 12:15, SN 011 Spring 2013.
Introduction to Computer Science Robert Sedgewick and Kevin Wayne Recursive Factorial Demo pubic class Factorial {
STRING AN EXAMPLE OF REFERENCE DATA TYPE. 2 Primitive Data Types  The eight Java primitive data types are:  byte  short  int  long  float  double.
1 Review Quisioner Kendala: Kurang paham materi. Praktikum Pengaruh teman.
CSci 1130 Intro to Programming in Java
7-13 September 2009 Coronal Shock Formation in Various Ambient Media IHY-ISWI Regional Meeting Heliophysical phenomena and Earth's environment 7-13 September.
Java Math Class. What is the Math Class? The Math Class is another class that is prepared by Java for us to use We use this class for mathematical operations.
Panel 3D = XML file pointer 08/09/20091 LHCb calorimeter meeting (jean-luc PANAZOL)
COP 2800 Lake Sumter State College Mark Wilson, Instructor.
Core Java Lecture 4-5. What We Will Cover Today What Are Methods Scope and Life Time of Variables Command Line Arguments Use of static keyword in Java.
1 Calling within Static method /* We can call a non static method from a static method but by only through an object of that class. */ class Test1{ public.
Objects contains data and methods Class – type of object Create class first Then object or instance String – defined class String s; // creates instance.
Self Check 1.Which are the most commonly used number types in Java? 2.Suppose x is a double. When does the cast (long) x yield a different result from.
1 Fundamental Data types Overview l Primitive Data Types l Variable declaration l Arithmetical Operations l Expressions l Assignment statement l Increment.
1 COMP 110 Static Methods and Variables Tabitha Peck M.S. March 24, 2008 MWF 3-3:50 pm Philips 367.
1 TP #4 n Last TP exercises solved; n Parsing command line arguments; n Static; n Wrapper classes for primitive types; n Package visibility.
COMP 14 Introduction to Programming Miguel A. Otaduy May 25, 2004.
Lecture 4 Types & Expressions COMP1681 / SE15 Introduction to Programming.
1 COMP 110 Designing and Overloading Methods Tabitha Peck M.S. March 26, 2008 MWF 3-3:50 pm Philips 367.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie July 8, 2005.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Introduction to Java Appendix A. Appendix A: Introduction to Java2 Chapter Objectives To understand the essentials of object-oriented programming in Java.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
JAVA Tokens. Introduction A token is an individual element in a program. More than one token can appear in a single line separated by white spaces.
Outline Character Strings Variables and Assignment Primitive Data Types Expressions Data Conversion Interactive Programs Graphics Applets Drawing Shapes.
Introduction to Java Lecture Notes 3. Variables l A variable is a name for a location in memory used to hold a value. In Java data declaration is identical.
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
Basics and arrays Operators:  Arithmetic: +, -, *, /, %  Relational:, >=, ==, !=  Logical: &&, ||, ! Primitive data types Byte(8), short(16), int(32),
1 More data types Character and String –Non-numeric variables –Examples: char orange; String something; –orange and something are variable names –Note.
COMP 110 Static variables and methods Luv Kohli October 29, 2008 MWF 2-2:50 pm Sitterson 014.
Chapter One Lesson Three DATA TYPES ©
CPRG 215 Introduction to Object-Oriented Programming with Java Module 2- Using Java Built-in Classes Topic 2.4 Using Java Built-in Classes Produced by.
1 Lecture # 2. * Introducing Programming with an Example * Identifiers, Variables, and Constants * Primitive Data Types * Byte, short, int, long, float,
CS0007: Introduction to Computer Programming Primitive Data Types and Arithmetic Operations.
Review by Mr. Maasz, Summary of Chapter 2: Starting Out with Java.
More About Objects and Methods
using System; namespace Demo01 { class Program
CMSC 202 Static Methods.
Unit-2 Objects and Classes
Chapter 3, cont Sept 20, 2004.
Introduction to Java Programming
Classes and Objects 5th Lecture
Chapter 2: Java Fundamentals
Questions? Math Class Wrapper Classes Writing / Testing Methods.
Classes and Objects Static Methods
Today in COMP 110 Brief static review The Math Class Wrapper Classes
Chap 2. Identifiers, Keywords, and Types
CS 1054 Introduction to Programming in Java
Presentation transcript:

COMP 110: Introduction to Programming Tyler Johnson Mar 23, 2009 MWF 11:00AM-12:15PM Sitterson 014

COMP 110: Spring Questions?

COMP 110: Spring Today in COMP 110 Review Overloading Programming Demo

COMP 110: Spring Review From last time

COMP 110: Spring The Math Class Provides many standard mathematical methods All methods are static, no need for an object of the Math class Call methods of the Math class using class name Math.abs Math.max Math.min Math.pow Math.round Others Predefined constants Math.PI Math.E

COMP 110: Spring Wrapper Classes Each primitive type has an associated Wrapper class Byte Short Integer Long Float Double Character Boolean

COMP 110: Spring Writing Methods Solving a problem using decomposition Divide into subproblems (pseudocode) Solve each subproblem separately as a method Use the methods youve created to solve the problem

COMP 110: Spring Review More broadly

COMP 110: Spring Calling Methods within Methods Its possible to call methods within other methods If calling a method of the same class, no need to specify receiving object public class Example { public void method1() { method2(); //no object needed, current object assumed } public void method2() { //do something }

COMP 110: Spring Calling Methods within Methods public class Example { public void method1() { System.out.println("method1!"); method2(); //no object needed, current object assumed } public void method2() { System.out.println("method2!"); } public static void main(String[] args) { Example example = new Example(); //create object of class Example example.method1(); }

COMP 110: Spring Input to Methods The input to a method is in the form of arguments public class Account { private double balance; private double limit; public void addPurchase(double amount) { if(balance + amount <= limit) balance = balance + amount; //only add if the transaction //is valid } The value is filled in by whomever calls the method

COMP 110: Spring Input to Methods public class Account { private double balance; private double limit; public void addPurchase(double amount) { if(balance + amount <= limit) balance = balance + amount; //only add if the transaction is valid } public class AccountTester { public static void main(String[] args) { Account accnt = new Account(); account.addPurchase(15.); //call addPurchase and w/ 15 for the amount account.addPurchase(20.); //call addPurchase and w/ 20 for the amount } Separate Java Files! A Driver program (Used for testing)

COMP 110: Spring Input to Methods NEVER do this public class Account { private double balance; private double limit; public void addPurchase(double amount) { Scanner keyboard = new Scanner(System.in); amount = keyboard.nextDouble(); if(balance + amount <= limit) balance = balance + amount; //only add if the transaction is valid } Overwriting the value that was passed in!

COMP 110: Spring Methods that Return a Value public class Account { private double balance; private double limit; //a helper method to determine if a transaction is valid private boolean transactionValid(double amount) { if(balance + amount <= limit) return true; else return false; } public void addPurchase(double amount) { boolean valid = transactionValid(amount); //is the transaction valid? if(valid) balance = balance + amount; //only add if the transaction is valid }

COMP 110: Spring Method Calls in If-Statements public class Account { private double balance; private double limit; //a helper method to determine if a transaction is valid private boolean transactionValid(double amount) { if(balance + amount <= limit) return true; else return false; } public void addPurchase(double amount) { if(transactionValid(amount)) balance = balance + amount; //only add if the transaction is valid } Call to a method inside an if-statement

COMP 110: Spring Booleans Theres no need to write if(systemsGo == true) System.out.println("Launch"); if(transactionValid(amount) == true) System.out.println("Accepted"); The more concise and equivalent way is if(systemsGo) System.out.println("Launch"); if(transactionValid(amount)) System.out.println("Accepted");

COMP 110: Spring Overloading Section 6.4 in text

COMP 110: Spring Overloading public class InputOne { public void readInput() { … } public class InputTwo { public void readInput() { … } public static void main(String[] args) { InputOne iOne = new InputOne(); InputTwo iTwo = new InputTwo(); iOne.readInput(); //readInput method of class InputOne iTwo.readInput(); //readInput method of class InputTwo } Methods in different classes can have the same name

COMP 110: Spring Overloading Methods in the same class can also have the same name This is called overloading Distinguished by the number & types of the parameters

COMP 110: Spring Overloading Example public class Average { //average two values public double getAverage(double a, double b) { return (a + b) / 2.; } //average three values public double getAverage(double a, double b, double c) { return (a + b + c) / 3.; }

COMP 110: Spring Overloading You have already been using overloaded methods System.out.print(7); //print an integer System.out.print('7'); //print a character System.out.print("seven"); //print a string System.out.print(7.0); //print a double

COMP 110: Spring Other Overloading Examples The Math class double Math.max(double a, double b) int Math.max(int a, int b) long Math.max(long a, long b) Allows the following int m = Math.max(1,3); double d = Math.max(5.6, 5.7);

COMP 110: Spring Overloading Any kind of method can be overloaded Void methods Methods returning a value Static methods Non-static methods Constructors

COMP 110: Spring Constructor Overloading public class Pet { private String name; private int age; private double weight; public Pet() { name = No name yet.; age = 0; weight = 0; } public Pet(String initName, int initAge, double initWeight) { name = initName; age = initAge; weight = initWeight; } Pet myPet = new Pet(); Pet myPet = new Pet("Fang", 12, 10.);

COMP 110: Spring Method Signatures A methods signature consists of Method name Number of parameters Types of parameters Example public double getAverage(double a, double b) { … } Signature Name: getAverage NumParams: 2 Param Types: –Param1: double –Param2: double Return type is NOT considered part of the signature!

COMP 110: Spring Method Signatures Java does not allow you to define two methods with the same signature in the same class Examples //these two are the same float getAverage(float a, float b) double getAverage(float a, float b) //these two are different float getAverage(float a, float b) double getAverage(double a, double b)

COMP 110: Spring Automatic Type Conversion Recall that automatic type conversion can sometimes occur with method calls double square(double x) { return x*x; //square the argument and return it } We can call this method as follows square(7.0); //returns 49.0 square(7); //also returns 49.0, auto type conversion

COMP 110: Spring The situation gets more complicated with overloading public class Example { double square(double x) { return x*x; } int square(int x) { return x*x; } public static void main(String[] args) { Example e = new Example(); e.square(7.0); e.square(7); } Interaction with Overloading Which method is being called?

COMP 110: Spring Overloading/Type Conversion Java will always use a method that is an exact match before it attempts type conversion

COMP 110: Spring Exact Overloading Match public void example(int i, double d, char c) {…} Are these calls to example an exact match? example(23, 55, 'c'); example(88, 76.0, ';'); example(4.0, 25, '!'); No. Automatic type conversion used Yes. No need for Automatic type conversion No. Automatic type conversion not possible

COMP 110: Spring Ambiguous Method Calls Java will only perform type conversion if the method call is unambiguous There is only ONE method for which automatic type conversion can be used to find a match

COMP 110: Spring Ambiguous Method Calls public class Example { double sum(int a, double b) { return a + b; } double sum(double a, int b) { return a + b; } public static void main(String[] args) { Example e = new Example(); e.sum(7, 7); //error, this method call is ambiguous e.sum(7, 7.0); //this is ok e.sum(7.0, 7); //this is ok }

COMP 110: Spring In Summary How Java determines which method you intend to call Exact Match? Use the Method Unambiguous Match using Type Conversion? Use the Method Error Match Based on Method Name, Num & Types of Parameters

COMP 110: Spring Use of Overloading Misuse of overloading can lead to strange bugs Use only with good reason public Pet(double initWeight) { //constructor for weight weight = initWeight; } public Pet(int initAge) { //constructor for age age = initAge; } public static void main(String[] args) { Pet myPet = new Pet(10); //meant to set weight, set age instead }

COMP 110: Spring Programming Demo Room Occupancy Create a class called Room that can be used to record the number of people in the rooms of a building

COMP 110: Spring Room Occupancy Attributes numberInRoom – the number of people in a room totalNumber – the total number of people in all rooms as a static variable Methods default constructor – sets number of people in room to 0 addOneToRoom – add a person to the room removeOneFromRoom – remove a person from the room (dont go below 0 persons) getNumber – returns the number of people in the room getTotal – a static method that returns the total number of people in all rooms validRemoval(int num) – returns whether num people can be removed from the room

COMP 110: Spring Programming Demo Programming

COMP 110: Spring Wednesday Array Basics (Section 7.1)