Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Methods with Parameters COMP.

Slides:



Advertisements
Similar presentations
Xiaoying Gao Computer Science Victoria University of Wellington Copyright: Xiaoying Gao, Peter Andreae, Victoria University of Wellington Graphical Output,
Advertisements

MiniDraw Testing COMP 102 # T1
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Creating and using Objects.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Designing with Methods COMP.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington UI methods, Graphical Output,
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington UI methods, Graphical Output.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Java Programs COMP 102 #3.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Review COMP 102 #
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Program Elements and Syntax.
Xiaoying Gao Computer Science Victoria University of Wellington Copyright: Xiaoying Gao, Peter Andreae, Victoria University of Wellington Conditionals.
© Xiaoying Gao, Peter Andreae Class, method, statements COMP 102 #3 2014T2 Xiaoying Sharon Gao Computer Science Victoria University of Wellington.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Summary and Exam COMP 102.
Karel J Robot An introduction to BlueJ and Object- Oriented Programming.
David Streader Computer Science Victoria University of Wellington Copyright: David Streader, Victoria University of Wellington While loops and the UI API.
© Xiaoying Gao, Peter Andreae UI methods, Variables, Constants COMP 102 #4 2015T2 Xiaoying Sharon Gao Computer Science Victoria University of Wellington.
Xiaoying Gao Computer Science Victoria University of Wellington Copyright: Xiaoying Gao, Peter Andreae, Victoria University of Wellington Create Objects,
©Xiaoying Gao, Peter Andreae First Java Program COMP 102 #2 2014T2 Xiaoying Sharon Gao Computer Science Victoria University of Wellington.
First Java Program COMP 102 #2 2015T2 Xiaoying Sharon Gao Computer Science Victoria University of Wellington.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Recursion COMP 102 # T1.
David Streader Computer Science Victoria University of Wellington Copyright: David Streader, Victoria University of Wellington Java Programing Basics COMP.
Xiaoying Gao Computer Science Victoria University of Wellington Copyright: Xiaoying Gao, Peter Andreae, Victoria University of Wellington Call a Method,
David Streader Computer Science Victoria University of Wellington Copyright: David Streader, Victoria University of Wellington Java Programing Basics COMP.
Xiaoying Gao Computer Science Victoria University of Wellington Copyright: Xiaoying Gao, Peter Andreae, Victoria University of Wellington Methods with.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington 2D arrays COMP 102 # T1.
A First Look at Java Chapter 2 1/29 & 2/2 Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010.
Introduction to Computers and Programming Lecture 14: User defined methods (cont) Professor: Evan Korth New York University.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Methods with Parameters COMP.
Xiaoying Gao Computer Science Victoria University of Wellington Copyright: Xiaoying Gao, Peter Andreae, Victoria University of Wellington Conditionals.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Designing with Methods COMP.
Xiaoying Gao Computer Science Victoria University of Wellington Copyright: Xiaoying Gao, Peter Andreae, Victoria University of Wellington Exercise, printf,
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington More Event-driven Input TextFields,
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Recursion COMP 102 # T1.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Classes, Objects, Fields,
Xiaoying Gao Computer Science Victoria University of Wellington Copyright: Xiaoying Gao, Peter Andreae, Victoria University of Wellington Creating Objects.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Loops and Input COMP 102 #
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Designing with Classes and.
Chapter 5 Classes and Methods II Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition) by S.N. Kamin, D. Mickunas, E.
Xiaoying Gao Computer Science Victoria University of Wellington Copyright: Xiaoying Gao, Peter Andreae, Victoria University of Wellington Fields, Constructors.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Loops and Input COMP 102 #
Xiaoying Gao Computer Science Victoria University of Wellington Copyright: Xiaoying Gao, Peter Andreae, Victoria University of Wellington Methods with.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Methods Calling Methods Return.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington 2D arrays COMP 102 # T1.
Xiaoying Gao Computer Science Victoria University of Wellington Copyright: Xiaoying Gao, Peter Andreae, Victoria University of Wellington Creating Objects.
Xiaoying Gao Computer Science Victoria University of Wellington Copyright: Xiaoying Gao, Peter Andreae, Victoria University of Wellington UI methods, Graphical.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Designing with Classes and.
2011-T1 Lecture 10 School of Engineering and Computer Science, Victoria University of Wellington  Rashina Hoda and Peter Andreae COMP 102 Rashina Hoda.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Java Programs COMP 102 #3.
© Xiaoying Gao, Peter Andreae Variables, Constants, UI methods COMP 102 #4 2014T2 Xiaoying Sharon Gao Computer Science Victoria University of Wellington.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Methods with Parameters COMP.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Event-driven Input COMP 102.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Summary and Exam COMP 102.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington UI methods, Graphical Output.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Programs with Choice Booleans,
Review Expressions and operators Iteration – while-loop – for-loop.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Creating and using Objects.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Designing with While loops.
© Peter Andreae Java Programs COMP 102 # T1 Peter Andreae Computer Science Victoria University of Wellington.
Xiaoying Gao Computer Science Victoria University of Wellington Copyright: Xiaoying Gao, Peter Andreae, Victoria University of Wellington Methods with.
David Streader Computer Science Victoria University of Wellington Copyright: David Streader, Victoria University of Wellington Java Programing Basics COMP.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Recursion COMP 102 # T1.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington More Event-driven Input COMP.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Program Elements and Syntax.
Fields, Constructors COMP 102 # T2
Methods Chapter 6.
Programs with graphics output
Lecture 8:The For Loop AP Computer Science Principles.
Chapter 2 Programming Basics.
Variables & getting info from the user
Presentation transcript:

Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Methods with Parameters COMP 102 #6 2013

© Peter Andreae COMP102 6:2 Menu Reflection.txt Designing Methods with parameters Methods that call other methods in the same program Administration: ECS Girls: 1-2 today (did you get the ?) Optional Tutorial, Monday 12-1 MY 632 Make-up lab: today 3-5 CO 242/243 Today is the deadline for adding or dropping with refund Online help: forum vs /web form Working together.

© Peter Andreae COMP102 6:3 Reflection.txt for Assig 1 Categories of commands: User input dataegString name = UI.askString(""); Text outputegUI.println(""); Graphical controllersegUI.clearGraphics, UI.draw____ Checking a MouseMaze solution: I would check over their proposal through trying to discover where it would fail, rather than ways it would work. I would look for circumstances as to when the mouse would loop. It would be important to see how the mouse dealt with different combinations of turns; especially when there are several options the mouse could take. A dead end would also have to be incorporated into these turns. Checking that the mouse could cope with a larger open space would also be included.

© Peter Andreae COMP102 6:4 Values to specify face & hat centerX top width height hatBot hatHt centerY left

© Peter Andreae COMP102 6:5 Improving the program: constants import comp102.*; import java.awt.Color; /** Draws face with a hat on the graphics pane */ public class Drawer { public static final double centerX = 250;// horizontal center of face public static final double centerY= 180;// vertical center of face public static final double width = 100; public static final double height = 160; public static final double left = centerX–width/2; public static final double top = centerY–height/2; public static final double hatHt = 0.25 * height; public static final double hatBot = top +hatHt; public static final double brimLt = centerX–0.8*width; public static final double brimRt = centerX+0.8*width;

© Peter Andreae COMP102 6:6 Improving the program: constants import comp102.*; import java.awt.Color; /** Draws face with a hat on the graphics pane */ public class Drawer { public static final double centerX = 250;// horizontal center of face ⋮ public static final double brimLt = centerX – 0.8 * width; public static final double brimRt = centerX * width; /** Draw an orange face with a brimmed hat*/ public void faceWithHat() { UI.setColor(Color.orange); UI.fillOval(left, top, width, height);// face UI.setColor(Color.green); UI.fillRect(left, top, width, hatHt); // hat crown UI.drawLine(brimLt, hatBot, brimRt, hatBot) //brim

© Peter Andreae COMP102 6:7 Syntax rules: Program structure 2nd version 〈import statements 〉 public class 〈 method descriptions 〉 { } 〈 classname 〉 〈 constants 〉

© Peter Andreae COMP102 6:8 Improving the program: variables /** Draw an orange face with a brimmed hat*/ public void faceWithHat() { double centerX = 250;// horizontal center of face double centerY= 180;// vertical center of face double width= 100; double height = 150; double left = centerX–width/2, double top = centerY–height/2; double hatHt = 0.25 * height; double hatBot = centerY – 0.25 * height; double brimLt = centerX – 0.8 * width,brimRt = centerX * width; UI.setColor(Color.orange); UI.fillOval(left, top, width, height);// face UI.setColor(Color.green); UI.fillRect(left, top, width, hatHt); // hat crown UI.drawLine(brimLt, hatBot, brimRt, hatBot) //brim } Can declare and initialise multiple variables on one line

© Peter Andreae COMP102 6:9 Principle of good design Use well named constants or variables whereever possible, rather than literal values ⇒ easier to understand ⇒ easier to get right ⇒ much easier to modify Choosing the right constants or variables is an art!! why did I choose "centerX" instead of “right" or "brimLt" ? why did I choose "centerY" to be the center of the face, not the hat? We have effectively parameterised the drawing Four values (centerX, centerY, width, height) control the whole thing.

© Peter Andreae COMP102 6:10 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

© Peter Andreae COMP102 6:11 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 x, double y, double width, double height){ double left = x – width / 2; double top = y – height/ 2; double hatHt = 0.25 * height; double hatBot = y – 0.25 * height; double brimLt = x – 0.8 * width; double brimRt = x * width; UI.setColor(Color.orange); UI.fillOval(left, top, width, height);// face UI.setColor(Color.green); UI.fillRect(left, top, width, hatHt); // hat crown UI.drawLine(brimLt, hatBot, brimRt, hatBot) //brim } Can call method with different arguments. Do it

© Peter Andreae COMP102 6:12 Syntax: Method Definitions (v2) /** Draw an orange face with a brimmed hat */ public void faceWithHat(double x, double y, double width, double height ){ double left = x – width / 2; : 〈 Comment 〉〈 Header 〉〈 Body 〉 {} public void 〈 parameters 〉 () 〈 name 〉 〈 type 〉, 〈 name 〉 How do you call a method with parameters from BlueJ?

© Peter Andreae COMP102 6:13 Method Definition: Like a pad of worksheets Calling a Method: Drawer1.faceWithHat(300, 100, 75, 95); ⇒ 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 x, double y, double faceWd, double faceHt ){ double faceLeft = x – faceWd / 2; double faceTop= y – faceHt / 2; : UI.fillOval(faceLeft, faceTop, faceWd, faceHt); }

© Peter Andreae COMP102 6:14 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, 110); this.faceWithHat(250, 200, 70, 90); this.faceWithHat(400, 200, 50, 130); this.faceWithHat(550, 200, 90, 150); } this is the object that the current method was called on. ????????

© Peter Andreae COMP102 6:15 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

© Peter Andreae COMP102 6:16 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?

© Peter Andreae COMP102 6:17 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)

© Peter Andreae COMP102 6:18 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

© Peter Andreae COMP102 6:19 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.

© Peter Andreae COMP102 6:20 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.