Java Object Oriented Programming Lab 12 Advanced Methods
Java Object Oriented Programming Lab 12 Advanced Methods
Objectives: § Learn how arguments are passed to methods § Learn how to return values from methods § Learn how to overload methods Lab 12 -2
Methods q Method declarations consists of four parts: Lab 12 -3
Methods q Method declarations consists of four parts: – Access modifier (usually public or private) public Lab 12 -4
Methods q Method declarations consists of four parts: – Access modifier (usually public or private) – Return Type or void. public double Lab 12 -5
Methods q Method declarations consists of four parts: – Access modifier (usually public or private) – Return Type or void. – Method Name. public double sum Lab 12 -6
Methods q Method declarations consists of four parts: – – Access modifier (usually public or private) Return Type or void. Method Name. Argument list. public double sum(double a, double b) Lab 12 -7
Methods q Every method must have a body. public double sum(double a, double b) { } Lab 12 -8
return q q A method, unless void, returns a value of the specified type to the calling method. The return statement is used to immediately quit the method and return a value: The type of the return public double sum(double a, double b) value or expression { must match the return a + b; method’s declared } return type. Lab 12 -9
Passing Arguments (cont’d) Primitive data types are always passed “by value”: the value is copied into the parameter. Circle c = new Circle(); double radius = 3. 5; double area = c. get. Area ( radius ); public class Circle {. . . public static double get. Area(double r) { return Math. PI * Math. pow( r, 2 ); } } radius: 3. 5 r: 3. 5 Lab 12 -10
Passing Arguments (cont’d) Primitive data types are always passed “by value”: the value is copied into the parameter. Circle c = new Circle(); double radius = 3. 5; double area = c. get. Area ( radius ); copy public class Circle {. . . public static double get. Area(double r) { return Math. PI * Math. pow( r, 2 ); } } radius: 3. 5 r: 3. 5 Lab 12 -11
Passing Arguments (cont’d) Primitive data types are always passed “by value”: the value is copied into the parameter. Circle c = new Circle(); radius: 3. 5 double radius = 3. 5; double area = c. get. Area ( radius ); copy public class Circle {. . . public static double get. Area(double r) r: 3. 5 { return Math. PI * Math. pow( r, 2 ); r acts like a local } variables in get. Area } Lab 12 -12
Passing Arguments (cont’d) Objects are always passed “by reference”: the value copied into the parameter is a memory location. String. Parser sp = new String. Parser(); String str = “Hello World”; String first. Word = sp. get. First. Word( str ); str : 14 B 761 F 3 public class String. Parser {. . . public String get. First. Word(String s) { int index = s. index. Of(“ ”); return s. substring(0, index); } } s: 14 B 761 F 3 Lab 12 -13
Passing Arguments (cont’d) Objects are always passed “by reference”: the value copied into the parameter is a memory location. String. Parser sp = new String. Parser(); String str = “Hello World”; String first. Word = sp. get. First. Word( str ); public class String. Parser {. . . public String get. First. Word(String s) { int index = s. index. Of(“ ”); return s. substring(0, index); } } str : 14 B 761 F 3 copy s: 14 B 761 F 3 Lab 12 -14
Passing Arguments to Methods Any expression that has an appropriate data type can serve as an argument: double a = 3, b = -4; double sum = add(a, b); double sum = add(3, 4), add(5, 6)); int width = 7; int height = 8; double area = area(width, height); Lab 12 -15
Passing Arguments (cont’d) § int is promoted to double when necessary: double area = p. get. Area ( 3 , 4. 0 ); The same as: ( 3. 0 ) l A “smaller” type can be promoted to a “larger” type (e. g. , int to long, float to double). Lab 12 -16
Overloaded Methods § Methods of the same class that have the same name but different numbers or types of arguments are called overloaded methods. § Use overloaded methods when they perform similar tasks: public void move (int x, int y) {. . . } public void move (double x, double y) {. . . } public int add(int a, int b) {. . . } public double add(double a, double b) {. . . } Lab 12 -17
Overloaded Methods (cont’d) § The compiler treats overloaded methods as completely different methods. § The compiler knows which one to call based on the number and the types of the arguments: public class Circle circle = new Circle(5); {. . . public void move (int x, int y) {. . . } public void move (Point p) {. . . } circle. move (50, 100); . . . Point center = new Point(50, 100); circle. move (center); . . . } Lab 12 -18
Java Object Oriented Programming How Do Return Methods Work?
public class Example { private int a = 3; private int b = 4; A Simple Program Using A Method That Receives Arguments And Returns A Value. public static void main(String[ ] args) { Example ex = new Example(); ex. output(); } public static int sum(int num 1, int num 2) { return num 1 + num 2; } public void output() { System. out. println(a + “ + ” + b + “ = ” + sum(a, b)); } } Lab 12 -20
public class Example { private int a = 3; private int b = 4; public static void main(String[ ] args) { Example ex = new Example(); ex. output(); } public static int sum(int num 1, int num 2) { return num 1 + num 2; } public void output() { System. out. println(a + “ + ” + b + “ = ” + sum(a, b)); } } Lab 12 -21
public class Example { private int a = 3; private int b = 4; public static void main(String[ ] args) { Example ex = new Example(); ex. output(); } public static int sum(int num 1, int num 2) { return num 1 + num 2; } public void output() { System. out. println(a + “ + ” + b + “ = ” + sum(a, b)); } } Lab 12 -22
public class Example { private int a = 3; private int b = 4; public static void main(String[ ] args) { Example ex = new Example(); ex. output(); } public static int sum(int num 1, int num 2) { return num 1 + num 2; } public void output() { System. out. println(a + “ + ” + b + “ = ” + sum(a, b)); } } Lab 12 -23
public class Example { private int a = 3; private int b = 4; public static void main(String[ ] args) { Example ex = new Example(); ex. output(); } public static int sum(int num 1, int num 2) { return num 1 + num 2; } public void output() { System. out. println(a + “ + ” + b + “ = ” + sum(a, b)); } } Lab 12 -24
public class Example { private int a = 3; private int b = 4; public static void main(String[ ] args) { Example ex = new Example(); ex. output(); } public static int sum(int num 1, int num 2) { return num 1 + num 2; } public void output() { System. out. println(a + “ + ” + b + “ = ” + sum(a, b)); } } Lab 12 -25
public class Example { private int a = 3; private int b = 4; public static void main(String[ ] args) { Example ex = new Example(); ex. output(); } public static int sum(int num 1, int num 2) { return num 1 + num 2; } public void output() { System. out. println(a + “ + ” + b + “ = ” + sum(a, b)); } } Lab 12 -26
public class Example { private int a = 3; private int b = 4; public static void main(String[ ] args) { Example ex = new Example(); ex. output(); } public static int sum(int num 1, int num 2) { return num 1 + num 2; } public void output() { System. out. println(a + “ + ” + b + “ = ” + sum(a, b)); } } Lab 12 -27
public class Example { private int a = 3; private int b = 4; public static void main(String[ ] args) { Example ex = new Example(); ex. output(); } public static int sum(int num 1, int num 2) { return num 1 + num 2; } } public void output() { System. out. println(a + “ + ” + b + “ = ” + sum(a, b)); } “ 3 + ” Lab 12 -28
public class Example { private int a = 3; private int b = 4; public static void main(String[ ] args) { Example ex = new Example(); ex. output(); } public static int sum(int num 1, int num 2) { return num 1 + num 2; } } public void output() { System. out. println(a + “ + ” + b + “ = ” + sum(a, b)); } “ 3 + ” “ 3 + 4” Lab 12 -29
public class Example { private int a = 3; private int b = 4; public static void main(String[ ] args) { Example ex = new Example(); ex. output(); } public static int sum(int num 1, int num 2) { return num 1 + num 2; } } public void output() { System. out. println(a + “ + ” + b + “ = ” + sum(a, b)); } “ 3 + 4” “ 3 + 4 = ” Lab 12 -30
public class Example { private int a = 3; private int b = 4; public static void main(String[ ] args) { Example ex = new Example(); ex. output(); } public static int sum(int num 1, int num 2) { return num 1 + num 2; } } public void output() { System. out. println(a + “ + ” + b + “ = ” + sum(a, b)); } “ 3 + 4 = ” Lab 12 -31
public class Example { private int a = 3; private int b = 4; public static void main(String[ ] args) { Example ex = new Example(); ex. output(); } public static int sum(int num 1, int num 2) { return num 1 + num 2; } } public void output() { System. out. println(a + “ + ” + b + “ = ” + sum(a, b)); } “ 3 + 4 = ” Lab 12 -32
public class Example { private int a = 3; private int b = 4; public static void main(String[ ] args) { Example ex = new Example(); ex. output(); } public static int sum(int num 1, int num 2) { return num 1 + num 2; } } 3 public void output() { System. out. println(a + “ + ” + b + “ = ” + sum(a, b)); } “ 3 + 4 = ” Lab 12 -33
public class Example { private int a = 3; private int b = 4; public static void main(String[ ] args) { Example ex = new Example(); ex. output(); } public static int sum(int num 1, int num 2) { return num 1 + num 2; } } 3 public void output() { System. out. println(a + “ + ” + b + “ = ” + sum(a, b)); } “ 3 + 4 = ” Lab 12 -34
public class Example { private int a = 3; private int b = 4; public static void main(String[ ] args) { Example ex = new Example(); ex. output(); } public static int sum(int num 1, int num 2) { return num 1 + num 2; } } 4 public void output() { System. out. println(a + “ + ” + b + “ = ” + sum(a, b)); } “ 3 + 4 = ” Lab 12 -35
public class Example { private int a = 3; private int b = 4; public static void main(String[ ] args) { Example ex = new Example(); ex. output(); } public static int sum(int num 1, int num 2) { return num 1 + num 2; } } 4 public void output() { System. out. println(a + “ + ” + b + “ = ” + sum(a, b)); } “ 3 + 4 = ” Lab 12 -36
public class Example { private int a = 3; private int b = 4; public static void main(String[ ] args) { Example ex = new Example(); ex. output(); } public static int sum(int num 1, int num 2) { return num 1 + num 2; } } public void output() { System. out. println(a + “ + ” + b + “ = ” + sum(a, b)); } “ 3 + 4 = ” Lab 12 -37
public class Example { private int a = 3; private int b = 4; public static void main(String[ ] args) { Example ex = new Example(); ex. output(); } public static int sum(int num 1, int num 2) { return num 1 + num 2; } 3 + 4 } public void output() { System. out. println(a + “ + ” + b + “ = ” + sum(a, b)); } “ 3 + 4 = ” Lab 12 -38
public class Example { private int a = 3; private int b = 4; public static void main(String[ ] args) { Example ex = new Example(); ex. output(); } } public static int sum(int num 1, int num 2) { return num 1 + num 2; } 7 public void output() { System. out. println(a + “ + ” + b + “ = ” + sum(a, b)); } “ 3 + 4 = ” Lab 12 -39
public class Example { private int a = 3; private int b = 4; public static void main(String[ ] args) { Example ex = new Example(); ex. output(); } public static int sum(int num 1, int num 2) { return num 1 + num 2; } 7 } public void output() { System. out. println(a + “ + ” + b + “ = ” + sum(a, b)); } “ 3 + 4 = ” “ 3 + 4 = 7” Lab 12 -40
public class Example { private int a = 3; private int b = 4; public static void main(String[ ] args) { Example ex = new Example(); ex. output(); } public static int sum(int num 1, int num 2) { return num 1 + num 2; } } public void output() { System. out. println(a + “ + ” + b + “ = ” + sum(a, b)); } “ 3 + 4 = 7” Lab 12 -41
public class Example { private int a = 3; private int b = 4; public static void main(String[ ] args) { Example ex = new Example(); ex. output(); } public static int sum(int num 1, int num 2) { return num 1 + num 2; } public void output() { System. out. println(a + “ + ” + b + “ = ” + sum(a, b)); } } Lab 12 -42
public class Example { private int a = 3; private int b = 4; public static void main(String[ ] args) { Example ex = new Example(); ex. output(); } public static int sum(int num 1, int num 2) { return num 1 + num 2; } public void output() { System. out. println(a + “ + ” + b + “ = ” + sum(a, b)); } } Lab 12 -43
Java Object Oriented Programming Top Down Programming
Top-Down Programming (cont’d) § Start JCreator. § Open the file “Lab 12. java”. § Lab 12. java is in your Lab 12 folder. Lab 12 -45
Top-Down Programming WAP that calculates the area of two rectangles. The two sides of each rectangle will be entered from the keyboard. Both side of one rectangle will be integer values. Both side of the other rectangle will be double values. Lab 12 -46
Top-Down Programming (cont’d) The “big problem” is defined in the main method. Lab 12 -47
Top-Down Programming (cont’d) import java. text. Decimal. Format; import java. util. Scanner; public class Lab 12 { public static void main(String[] args) { Lab 12 lab = new Lab 12( ); lab. input(); // Enter data from the kybd lab. output( ); // Display output } } Lab 12 -48
Top-Down Programming (cont’d) We need four instance fields to solve the problem. Lab 12 -49
Top-Down Programming (cont’d) public class Lab 12 { private int width 1; private int height 1; private double width 2; private double height 2; public static void main(String[] args) { Lab 12 lab = new Lab 12( ); lab. input(); // Enter data from the kybd lab. output( ); // Display output } } Lab 12 -50
Top Down Design (cont’d) input() and output( ) are the smaller parts of the problem. Lab 12 -51
Top-Down Programming (cont’d) input( ) Lab 12 -52
Top-Down Programming (cont’d) public void input() { Scanner reader = new Scanner(System. in); System. out. print(“Enter an integer value for the width of a rectangle: ”); width 1 = reader. next. Int(); System. out. print(“Enter an integer value for the height of a rectangle: ”); height 1 = reader. next. Int(); System. out. print(“Enter a double value for the width of a rectangle: ”); width 2 = reader. next. Double(); System. out. print(“Enter a double value for the height of a rectangle: ”); height 2 = reader. next. Double(); } Lab 12 -53
Top-Down Programming (cont’d) output( ) Lab 12 -54
Top-Down Programming (cont’d) public void output() { } Lab 12 -55
Top-Down Programming (cont’d) public void output() { Decimal. Format df = new Decimal. Format(“#. ##”); } Lab 12 -56
Top-Down Programming (cont’d) public void output() { Decimal. Format df = new Decimal. Format(“#. ##”); System. out. println(“The area of a rectangle with a width of ” + width 1 + “ and a height of ”+ height 1 + “ is ” + area(width 1, height 1) + “. ”); } Lab 12 -57
Top-Down Programming (cont’d) public void output() { Decimal. Format df = new Decimal. Format(“#. ##”); System. out. println(“The area of a rectangle with a width of ” + width 1 + “ and a height of ”+ height 1 + “ is ” + area(width 1, height 1) + “. ”); System. out. println(“The area of a rectangle with a width of ” + width 2 + “ and a height of ” + height 2 + “ is ” + df. format(area(width 2, height 2)) + ". "); } Lab 12 -58
Top-Down Programming (cont’d) area( ) Lab 12 -59
Top-Down Programming (cont’d) area public static int area(int a, int b) { } Lab 12 -60
Top-Down Programming (cont’d) area public static int area(int a, int b) { return a * b; } Lab 12 -61
Top-Down Programming (cont’d) area( ) – overloaded method Lab 12 -62
Top-Down Programming (cont’d) area (overloaded) public static double area(double a, double b) { } Lab 12 -63
Top-Down Programming (cont’d) area (overloaded) public static double area(double a, double b) { return a * b; } Lab 12 -64
Top-Down Programming (cont’d) Run The Program: Enter an integer value for the width of a rectangle: 6 Enter an integer value for the height of a rectangle: 8 Enter a double value for the width of a rectangle: 9. 4 Enter a double value for the height of a rectangle: 7. 25 The area of a rectangle with a width of 6 and a height of 8 is 48. The area of a rectangle with a width of 9. 4 and a height of 7. 25 is 68. 15. Lab 12 -65
The parameters (arguments) are what you need to know to solve the problem! // Area of a circle - You need to know the radius public static double area(double radius) { return Math. PI * Math. pow(r, 2); } // Speed of a falling object - You need to // know the elapsed time in seconds. public static double speed(int time) { return 32 * Math. pow(time, 2); } Lab 12 -66
Java Object Oriented Programming Questions?
Java Object Oriented Programming Begin Lab 12
- Slides: 68