Presentation is loading. Please wait.

Presentation is loading. Please wait.

Xiaoying Gao Computer Science Victoria University of Wellington Copyright: Xiaoying Gao, Peter Andreae, Victoria University of Wellington Methods with.

Similar presentations


Presentation on theme: "Xiaoying Gao Computer Science Victoria University of Wellington Copyright: Xiaoying Gao, Peter Andreae, Victoria University of Wellington Methods with."— Presentation transcript:

1 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 #6 2014 T2

2 © Xiaoying Gao, Peter Andreae COMP102 6:2 Menu Arguments and Parameters Methods that return a value Administrivia: Assignment 2 is due next Wed 11am. Lab booking is still open

3 © Xiaoying Gao, Peter Andreae COMP102 6:3 A summary Class Constants Methods Statements Variables Assignment statements Call another method

4 © Xiaoying Gao, Peter Andreae COMP102 5:4 Examples import ecs100.*; public class Triangle{ public void ManyTriangles(){ this.drawTri(100, 100, 40, 50, 200, 400); this.drawTri(300,200, 150, 100, 200, 200); } public void drawTri(double x1, double y1, double x2, double y2, double x3, double y3){ UI.drawLine(x1, y1, x2, y2); UI.drawLine(x2, y2, x3, y3); UI.drawLine(x3, y3, x1, y1); }

5 © Xiaoying Gao, Peter Andreae COMP102 6:5 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.drawTri(100, 100, 40, 50, 200, 400); this.drawTri(300,200, 150, 100, 200, 200);

6 © Xiaoying Gao, Peter Andreae COMP102 6:6 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)); }

7 © Xiaoying Gao, Peter Andreae COMP102 6:7 Arguments and parameters Must match Type of data Number of data Order of data Do not need to match name

8 © Xiaoying Gao, Peter Andreae COMP102 6:8 Bad example, bad style import ecs100.*; public class ParameterEx2{ public void method1(){ int x = 1; int y = 5; this.method2(x, y+3); x = x + 1; this.method2(y, x); } public void method2(int x, int y){ UI.println(x + ", "+ y); UI.println("result: " + (x + y/2)); }

9 © Xiaoying Gao, Peter Andreae COMP102 6:9 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

10 © Xiaoying Gao, Peter Andreae COMP102 6:10 Method return 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; }

11 © Xiaoying Gao, Peter Andreae COMP102 6:11 Method returns a value import ecs100.*; public class ReturnEx2{ public double circleArea(double radius){ double area = Math.PI * Math.pow(radius,2); return area; } public void TestArea(){ double radius = 10.0; UI.println("the area is " + this.circleArea(radius)); }

12 © Xiaoying Gao, Peter Andreae COMP102 7:12 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 〉

13 © Xiaoying Gao, Peter Andreae COMP102 6:13 An exercises Define a class with two methodes 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 ID Return the password

14 © Xiaoying Gao, Peter Andreae COMP102 6:14 Password Example

15 © Xiaoying Gao, Peter Andreae COMP102 7:15 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);

16 © Xiaoying Gao, Peter Andreae COMP102 7:16 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)

17 © Xiaoying Gao, Peter Andreae COMP102 6:17 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?

18 © Xiaoying Gao, Peter Andreae COMP102 6:18 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. ????????

19 © Xiaoying Gao, Peter Andreae COMP102 7:19 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.

20 © Xiaoying Gao, Peter Andreae COMP102 7:20 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


Download ppt "Xiaoying Gao Computer Science Victoria University of Wellington Copyright: Xiaoying Gao, Peter Andreae, Victoria University of Wellington Methods with."

Similar presentations


Ads by Google