Xiaoying Gao Computer Science Victoria University of Wellington Copyright: Xiaoying Gao, Peter Andreae, Victoria University of Wellington Methods with Parameters, methods return a value COMP 102 # T2
© Xiaoying Gao, Peter Andreae COMP102 6:2 Assignment 2 Template code uses Buttons The buttons are set up in a constructor (a special method)
© Xiaoying Gao, Peter Andreae COMP102 6:3 Menu Arguments and Parameters Methods that return a value
© Xiaoying Gao, Peter Andreae COMP102 6:4 A summary Class Constants Methods Statements Variables Assignment statements Method call statements Call a method in library UI.drawRect(20, 40, 100, 400); Call a method in the same class this.drawPicture(200, 100, 50);
© Xiaoying Gao, Peter Andreae COMP102 5:5 Version 3 import ecs100.*; import java.awt.Color; public class DrawShapes3{ public void drawMany(){ this.drawPicture(300, 100, 70); this.drawPicture(300,200, 100); } public void drawPicture(double x, double y, double d){ UI.setColor(Color.green); UI.fillRect(x-d,y-d/2,2*d,d); UI.setColor(Color.red); UI.fillOval(x-d/2, y-d/2, d, d); UI.setColor(Color.orange); UI.drawLine(x-2*d, y, x+2*d, y); UI.fillArc(x-d/2, y-d/2, d, d, 30, 120);} }
© Xiaoying Gao, Peter Andreae COMP102 6:6 Method with parameters Parameterising a method makes it more flexible and general Allows us to call the same method with different arguments to do the same thing in different ways Allows us to reuse the same bit of code this.drawPicture(100, 100, 40); this.drawPicture(300,200, 150);
© Xiaoying Gao, Peter Andreae COMP102 6:7 Arguments can be variables with values import ecs100.*; public class ParameterExample{ public void method1(){ int m = 1; int n = 5; this.method2(m, n+3); m = m + 1; this.method2(n, m); } public void method2(int x, int y){ UI.println(x + ", "+ y); UI.println("result: " + (x + y/2)); }
© Xiaoying Gao, Peter Andreae COMP102 6:8 Arguments and parameters Must match Type of data Number of data Order of data Do not need to match name
© Xiaoying Gao, Peter Andreae COMP102 6:9 Bad example, bad style, important concept import ecs100.*; public class ParameterEx2{ public void method1(){ int x = 5; int y = 10; this.method2(x, y); UI.println("method1, x: " + x + ", y: "+ y); this.method2(y, x); UI.println("method1, x: " + x +", y: "+ y); } public void method2(int x, int y){ UI.println("method2, x: " + x + ", y: " + y); x = x+1; y = y*y; UI.println("method 2, x is now:"+ x +", y is now:" + y); } }
© Xiaoying Gao, Peter Andreae COMP102 6:10 Local variables and parameters Local variables and parameters are only accessible within the same method The variables with the same name in different methods are completely separated things Data is past from one method to another by argument and parameter match Match by type, order, number Not by name (Two or more methods to share the same data without using parameters: data fields) Later in the course
© Xiaoying Gao, Peter Andreae COMP102 6:11 Method returns a value import ecs100.*; public class ReturnEx{ public void method1(){ double w = 10.0; double h = 2.0; double s = this.rectArea(w, h); UI.println("the area is " + s); } public double rectArea(double width, double height){ double area = width * height; return area; }
© Xiaoying Gao, Peter Andreae COMP102 6:12 Method returns a value import ecs100.*; public class ReturnEx2{ public void TestArea(){ double radius = 10.0; UI.println("the area is " + this.circleArea(radius)); } public double circleArea(double radius){ double area = Math.PI * Math.pow(radius,2); return area; }
© Xiaoying Gao, Peter Andreae COMP102 6:13 More examples import ecs100.*; public class ReturnEx4{ public void rectCheck(){ double w = 10.0; double h = 2.0; boolean q = this.isSquare(w, h); UI.println("is it a square? " + q); } public boolean isSquare(double width, double height){ return width==height; }
© Xiaoying Gao, Peter Andreae COMP102 7:14 Method Definitions (3) /** Method returns a value*/ public double rectArea(double width, double height){ double area = width * height; return area; } 〈 Comment 〉〈 Header 〉〈 Body 〉 {} public 〈 type 〉 〈 parameters 〉 () 〈 name 〉 〈 type 〉, 〈 name 〉
© Xiaoying Gao, Peter Andreae COMP102 6:15 An exercises Define a class with two methods First method Ask the user for a name Ask the user for an ID number Call the second method to generate a password based on name and ID Show the user his/her password Second method Generate a password using the first character of name and all digits in ID Return the password
© Xiaoying Gao, Peter Andreae COMP102 6:16 Password Example
© Xiaoying Gao, Peter Andreae COMP102 7:17 Methods that return values Some methods just have "side effects": UI.println("Hello there!"); UI.printf("%4.2f miles is the same as %4.2f km\n", mile, km); UI.fillRect(100, 100, wd, ht); UI.sleep(1000); Some methods just return a value: long now = System.currentTimeMillis(); double distance = 20 * Math.random(); double ans = Math.pow(3.5, 17.3); Some methods do both: double height = UI.askDouble("How tall are you"); Color col =JColorChooser.showDialog(null, "paintbrush", Color.red);
© Xiaoying Gao, Peter Andreae COMP102 7:18 Call a method objectName.methodName(arguments) Objects Predefined: UI, Math, System (they are in fact Class names) Default: this Arguments, data to use Check documentation (or your own code), to find out parameters Must match the type, order, number of parameters If the method returns a value Assign it to a variable (store it) Print it out (use it for output) Do nothing (throw it away)
© Xiaoying Gao, Peter Andreae COMP102 6:19 Another Java Program Design a Java program to measure reaction time of users responding to true and false "facts". Ask the user about a fact: "Is it true that the BE is a 4 Year degree?" Measure the time they took Print out how much time. Need a class what name? Need a method what name? what parameters? what actions?
© Xiaoying Gao, Peter Andreae COMP102 6:20 ReactionTimeMeasurer (Version 1) Need method that calls the measureTime method repeatedly public class ReactionTimeMeasurer { public void askFourQuestions() { this.measureTime("John Quay is the Prime Minister"); this.measureTime("11 x 13 = 143"); this.measureTime("Summer is warmer than Winter"); this.measureTime("Wellington has over 1,000,000 people"); } public void measureTime(String fact) { long startTime = System.currentTimeMillis(); String ans =UI.askString("Is it true that" + fact); long endTime = System.currentTimeMillis(); UI.println("You took " + (endTime - startTime) +" milliseconds"); } } this is the object that the current method was called on. ????????
© Xiaoying Gao, Peter Andreae COMP102 7:21 Defining methods to return values (v2) Improving ReactionTime: Make measureTime return the time, then add them all up and print out the average. public void askFourQuestions() { long time = this.measureTime("John Quay is the Prime Minister"); time = time + this.measureTime("11 x 13 = 143"); time = time + this.measureTime("Summer is warmer than Winter"); time = time + this.measureTime("Wellington has 1,000,000 people"); UI.printf("Average time = %d milliseconds\n", (time / 4)); } public void measureTime(String fact) { long startTime = System.currentTimeMillis(); …… } long Specifies the type of value returned. void means "no value returned" If measureTime returns a value instead of just printing it out.
© Xiaoying Gao, Peter Andreae COMP102 7:22 Defining methods to return values (v2) If you declare that a method returns a value, then the method body must return one! public long measureTime(String fact) { long startTime = System.currentTimeMillis(); String ans = UI.askString("Is it true that " + fact); long endTime = System.currentTimeMillis(); UI.printf("You took %d milliseconds\n", (endTime - startTime) ); } return (endTime - startTime) ; Return statement Means: exit the method and return the value The value must be of the right type