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 COMP 102 #6 2013 T2

2 © Xiaoying Gao, Peter Andreae COMP102 6:2 Menu Methods with parameters Methods that call other methods in the same program Administrivia: Assignment 2 is due Wed 10am. Tutorial Tuesday 12-1 at VZ103 Lab booking is still open, CO243 is our overflow lab.

3 © Xiaoying Gao, Peter Andreae COMP102 6:3 Reflection.txt #1 Some good answers Categories of commands: "A) Output : Println method B) Input : askString method C) Drawing : setColor, fillOval, drawString, drawArc I guess drawing category can be included in output category." Import commands: Tell the compiler which libraries to look in for the functions used in the code. Input commands: For instance, UI.askString: ask the user for input. Output commands: Fall into two categories, text-based outputs and graphics- based outputs. UI.println() UI.drawString() deal with string output while UI.draw* commands deal with drawing geometric shapes. Declarations of classes and methods - i.e. public class and public void - these commands structure the program.

4 © Xiaoying Gao, Peter Andreae COMP102 6:4 Reflection.txt #2 Checking a Mouse program: "Ensure that the mouse encountered; a dead end, a left turn, a right turn, a 4 way intersection and a 3 way intersection from all the approaches." "I would test his instructions on the mouse in every 9x9 grid combination possible." "I would design mazes that fit certain problematic situations: for instance, T-instersections that the mouse had to navigate, both left and right as well as directly in front. If the mouse solved those mazes I would test it against a maze with a four-way intersection and make sure that it would go down each of the paths at some point. I would also construct a maze with a fork in which both paths end in dead- ends; in order to escape the mouse must go back down the path that it originally came from. For thoroughness, I would make a list of all the possible maze features and test the mouse against each of them."

5 © Xiaoying Gao, Peter Andreae COMP102 6:5 Variables to specify face & hat center top faceWd faceHt hatWd brimYhatHt

6 © Xiaoying Gao, Peter Andreae COMP102 6:6 Improving the program /** Draw an orange face with a brimmed hat*/ public void faceWithHat() { double center = 250;// horizontal center of face double top = 100;// top of face double faceWd = 100; double faceHt = 1.5 * faceWd; double hatHt = 0.3 * faceHt; double hatWd = 0.96 * faceWd; double brimWd = 1.6 * hatWd; double brimY = top + 0.25 * faceHt; UI.setColor(Color.orange); UI.fillOval(center-faceWd/2, top, faceWd, faceHt); // face UI.setColor(Color.green); UI.fillRect(center-hatWd/2, brimY-hatHt, hatWd, hatHt); // hat crown UI.drawLine(center-brimWd/2, brimY, center+brimWd/2, brimY)//brim }

7 © Xiaoying Gao, Peter Andreae COMP102 6:7 Principle of good design Use well named variables where-ever possible, rather than literal values ⇒ easier to understand ⇒ easier to get right ⇒ much easier to modify Choosing the right variables is an art!! why did I choose "center" instead of "faceLeft" or "brimLeft" ? why did I choose "top" to be the top of the face, not the hat? We have effectively parameterised the drawing three values (center, top, faceWd) control the whole thing.

8 © Xiaoying Gao, Peter Andreae COMP102 6:8 Even better design: parameters Every time we want a face of a different size or in a different position, we have to modify the code. How come we don't have to do that with drawRect? drawRect has four parameters: Definition of drawRect: void drawRect(double left, double top, double wd, double ht) Calling drawRect: UI.drawRect(200, 150, 50, 80), ⇒ drawRect can make many different rectangles. Why can't we do that with faceWithHat? Parameters Arguments

9 © Xiaoying Gao, Peter Andreae COMP102 6:9 Giving faceWithHat parameters list the parameters in the header of the method definition: /** Draw an orange face with a brimmed hat*/ public void faceWithHat( double center, double top, double faceWd ){ double faceHt = 1.5 * faceWd; double hatHt = 0.3 * faceHt; double hatWd = 0.96 * faceWd; double brimWd = 1.6 * hatWd; double brimY = top + 0.25 * faceHt; UI.setColor(Color.orange); UI.fillOval(center-faceWd/2, top, faceWd, faceHt); // face UI.setColor(Color.green); UI.fillRect(center-hatWd/2, brimY-hatHt, hatWd, hatHt); // hat crown UI.drawLine(center-brimWd/2, brimY, center+brimWd/2, brimY);//brim }

10 © Xiaoying Gao, Peter Andreae COMP102 6:10 Method Definitions (2) /** Print out the conversion formulas */ public void faceWithHat( double center, double top, double faceWd ){ double faceHt = 1.5 * faceWd; : 〈 Comment 〉〈 Header 〉〈 Body 〉 {} public void 〈 parameters 〉 () 〈 name 〉 〈 type 〉, 〈 name 〉 How do you call a method with parameters from BlueJ?

11 © Xiaoying Gao, Peter Andreae COMP102 6:11 Method Definition: Like a pad of worksheets Calling a Method: Drawer1.faceWithHat(300, 100, 75); ⇒ get a “copy” of the method worksheet ⇒ copy the arguments to the parameter places ⇒ perform each action in the body ⇒ throw the worksheet away (losing all the information on it) Method Calls with parameters public void faceWithHat( double x, double y, double faceWd, double faceHt ){ double faceLeft = x – faceWd / 2; double faceTop= y – faceHt / 2; : UI.fillOval(faceLeft, faceTop, faceWd, faceHt); } public void faceWithHat( double center, double top, double faceWd ){ double faceLeft = x – faceWd / 2; double faceTop= y – faceHt / 2; : UI.fillOval(faceLeft, faceTop, faceWd, faceHt); } 300 0 100 0 75 0

12 © Xiaoying Gao, Peter Andreae COMP102 6:12 Many faces We can write a method that calls faceWithHat multiple times: /** Draw four faces */ public void fourFaces ( ) { UI.clearGraphics(); this.faceWithHat(100, 200, 70); this.faceWithHat(250, 200, 70); this.faceWithHat(400, 200, 50); this.faceWithHat(550, 200, 90); } this is the object that the current method was called on. ????????

13 © Xiaoying Gao, Peter Andreae COMP102 6:13 Principle of good design 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

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

15 © Xiaoying Gao, Peter Andreae COMP102 6:15 ReactionTimeMeasurer /** Measures reaction times for responding to true-false statements */ public class ReactionTimeMeasurer { /** Measure and report the time taken to answer a question */ public void measureTime() { // find out the current time and remember it // ask the question and wait for answer // find out (and remember) the current time // print the difference between the two times } } Write the method body in comments first, (to plan the method without worrying about syntax) Work out what information needs to be stored (ie, variables)

16 © Xiaoying Gao, Peter Andreae COMP102 6:16 ReactionTimeMeasurer /** Measures reaction times for responding to a true-false statement */ public class ReactionTimeMeasurer { /** Measure and report the time taken to answer a question */ public void measureTime() { long startTime = System.currentTimeMillis(); String ans =UI.askString("Is it true that the BE is a 4 year degree"); long endTime = System.currentTimeMillis(); UI.println("You took " + (endTime - startTime) +" milliseconds"); } } currentTimeMillis() is a method that returns a long value Better design: make measureTime work on any fact: very big integers

17 © Xiaoying Gao, Peter Andreae COMP102 6:17 Improving ReactionTimeMeasurer (1) /** Measures reaction times for responding to true-false statements */ public class ReactionTimeMeasurer { /** Measure and report the time taken to answer a question */ 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"); } } Just asking one question is not enough for an experiment.  need to ask a sequence of questions.

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


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