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.
Chapter 2: Using Objects Part 1. To learn about variables To understand the concepts of classes and objects To be able to call methods To learn about.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Designing with Methods COMP.
Lecture 2 Introduction to C Programming
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.
Computer Science 1620 Functions. Given a number n, the factorial of n, written n!, is computed as follows: note: 0! = 1 examples: n! = n x (n-1) x (n-2)
Graphical Tree-Based Scientific Calculator: CalcuWiz Will Ryan Christian Braunlich.
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.
David Streader Computer Science Victoria University of Wellington Copyright: David Streader, Victoria University of Wellington GUI and the UI API COMP.
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.
Goals of Course Introduction to the programming language C Learn how to program Learn ‘good’ programming practices.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Methods with Parameters COMP.
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.
Chapter 1 Object Orientation: Objects and Classes.
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.
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.
1 Principles of Computer Science I Prof. Nadeem Abdul Hamid CSC 120 – Fall 2005 Lecture Unit 2 - Using Objects.
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,
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
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,
Variables and Functions Chapter Variables Named storage location in computer’s memory Programs may need to store data when running Types of data.
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 Designing with Classes and.
Xiaoying Gao Computer Science Victoria University of Wellington Copyright: Xiaoying Gao, Peter Andreae, Victoria University of Wellington Fields, Constructors.
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 Types and Interfaces COMP.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Methods Calling Methods Return.
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.
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 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,
1 UMBC CMSC 104, Section Fall 2002 Functions, Part 1 of 3 Topics Top-down Design The Function Concept Using Predefined Functions Programmer-Defined.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Creating and using Objects.
1.1: Objects and Classes msklug.weebly.com. Agenda: Attendance Let’s get started What is Java? Work Time.
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.
CMSC 104, Section 301, Fall Lecture 18, 11/11/02 Functions, Part 1 of 3 Topics Using Predefined Functions Programmer-Defined Functions Using Input.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Recursion COMP 102 # T1.
Event 1 Principles of Programming Hugh Davis ECS The University of Southampton, UK users.ecs.soton.ac.uk/hcd.
Programs with graphics output
Loop Structures.
Subroutines Idea: useful code can be saved and re-used, with different data values Example: Our function to find the largest element of an array might.
Presentation transcript:

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

© Peter Andreae COMP102 6:2 Menu Reflection.txt Designing Methods with parameters Methods that call other methods in the same program Administrivia: If you are missing Reflection.txt, get a new copy. Optional Tutorial, Monday 1-2 CO LT118 Help desk / make-up lab: today 2-4 CO 242 Today is the deadline for adding or dropping with refund

© Peter Andreae COMP102 6:3 Reflection.txt #1 Some good answers Categories of commands: "A) Output : Println method B) Input : ask.String 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.

© Peter Andreae COMP102 6:4 Reflection.txt #1 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."

© Peter Andreae COMP102 6:5 Variables to specify face & hat x y faceWd faceHt hatWd hatBothatHt

© Peter Andreae COMP102 6:6 Improving the Face program /** Draw an orange face with a brimmed hat*/ public void faceWithHat() { double x = 250;// horizontal center of face double y= 180;// vertical center of face double faceWd = 100; double faceHt = 150; double faceLeft = x–faceWd/2,faceTop = y–faceHt/2; double hatHt = 0.3 * faceHt, hatWd = 0.96 * faceWd; double hatLeft = x–hatWd/2; double hatBot = y–0.25*faceHt,hatTop = hatBot – hatHt; double brimLt = x–0.8*hatWd,brimRt = x+0.8*hatWd; UI.setColor(Color.orange); UI.fillOval(faceLeft, faceTop, faceWd, faceHt);// face UI.setColor(Color.green); UI.fillRect(hatLeft, hatTop, hatWd, hatHt); // hat crown UI.drawLine(brimLt, hatBot, brimRt, hatBot) //brim }

© Peter Andreae COMP102 6:7 Principle of good design Use well named variables wherever 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 "x" instead of "faceLeft" or "brimLeft" ? why did I choose "y" to be the center of the face, not the hat? We have effectively parameterised the drawing Four values (x, y, faceWd, faceHt) control the whole thing.

© 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

© 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 x, double y, double faceWd, double faceHt ){ double faceLeft = x – faceWd / 2,faceTop = y – faceHt / 2; double hatHt = 0.3 * faceHt, hatWd = 0.96 * faceWd; double hatLeft = x – hatWd / 2; double hatBot = y – 0.25 * faceHt,hatTop = hatBot – hatHt; double brimLt = x – 0.8 * hatWd,brimRt = x * hatWd; UI.setColor(Color.orange); UI.fillOval(faceLeft, faceTop, faceWd, faceHt);// face UI.setColor(Color.green); UI.fillRect(hatLeft, hatTop, hatWd, hatHt); // hat crown UI.drawLine(brimLt, hatBot, brimRt, hatBot) //brim } Can call method with different arguments. Do it

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

© Peter Andreae COMP102 6:11 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: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, 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: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 do the same thing in different ways Allows us to reuse the same bit of code

© 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?

© 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)

© 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

© 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.

© 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.