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

Slides:



Advertisements
Similar presentations
MiniDraw Testing COMP 102 # T1
Advertisements

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 UI methods, Graphical Output,
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington UI methods, Graphical Output.
Chapter 2 storing numbers and creating objects Pages in Horstmann.
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 Arrays COMP 102 # T1.
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 Summary and Exam COMP 102.
Beginning C++ Through Game Programming, Second Edition by Michael Dawson.
Lecture 2: Static Methods, if statements, homework uploader.
© 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,
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 Recursion COMP 102 # T1.
Xiaoying Gao Computer Science Victoria University of Wellington Copyright: Xiaoying Gao, Peter Andreae, Victoria University of Wellington Call a Method,
Xiaoying Gao Computer Science Victoria University of Wellington Copyright: Xiaoying Gao, Peter Andreae, Victoria University of Wellington Methods with.
2011-T1 Lecture 13 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 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.
Java Programming The Language of Now & the Future* Lecture 0 An Introduction to Java Syntax for Non-C Programmers John Morris Department of Electrical.
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.
1 Building Java Programs Chapter 3: Introduction to Parameters and Objects These lecture notes are copyright (C) Marty Stepp and Stuart Reges, They.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Classes, Objects, Fields,
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Recursion COMP 103 #
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Designing with Classes and.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington More on Files COMP 102 # 14.
Xiaoying Gao Computer Science Victoria University of Wellington Copyright: Xiaoying Gao, Peter Andreae, Victoria University of Wellington Fields, Constructors.
Methods. Methods also known as functions or procedures. Methods are a way of capturing a sequence of computational steps into a reusable unit. Methods.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington More on Files COMP 102 # 14.
Xiaoying Gao Computer Science Victoria University of Wellington Copyright: Xiaoying Gao, Peter Andreae, Victoria University of Wellington Methods with.
CSE 110 Review Session Hans Hovanitz, Kate Kincade, and Ian Nall.
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 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 Summary and Exam COMP 102.
Topic 8Classes, Objects and Methods 1 Topic 8 l Class and Method Definitions l Information Hiding and Encapsulation l Objects and Reference Classes, Objects,
1 Project 2: Using Variables and Expressions. 222 Project 2 Overview For this project you will work with three programs Circle Paint Ideal_Weight What.
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,
A High Flying Overview CS139 – Fall 2006 How far we have come.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Creating and using Objects.
Lecture 6: Methods MIT-AITI Kenya © 2005 MIT-Africa Internet Technology Initiative In this lecture, you will learn… What a method is Why we use.
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.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Arrays COMP 102 # T1.
CS305j Introduction to Computing Parameters and Methods 1 Topic 8 Parameters and Methods "We're flooding people with information. We need to feed it through.
David Streader Computer Science Victoria University of Wellington Copyright: David Streader, Victoria University of Wellington Java Programing Basics COMP.
ITM 3521 ITM 352 Functions. ITM 3522 Functions  A function is a named block of code (i.e. within {}'s) that performs a specific set of statements  It.
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.
Fields, Constructors COMP 102 # T2
Information and Computer Sciences University of Hawaii, Manoa
4. Java language basics: Function
CSE / ENGR 142 Programming I
Programs with graphics output
Methods/Functions.
Introduction to Computer Science and Object-Oriented Programming
Presentation transcript:

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

© Peter Andreae COMP 102 9:2 Menu Designing a program with multiple methods Administrivia: First test is Monday 8 th 5-6pm, MC LT103, MC LT102, KK LT303 If you can't get to test: Some planned event: talk/ me or Ambreen beforehand Sick/accident: /call as soon as possible Brief Java Documentation for test is on the web. Help Desk Sessions Mondays ??? 11-12, 3-4, 4-5

© Peter Andreae COMP 102 9:3 HouseDrawer Draw a street of houses of different random sizes: assume size is between 100 and 150

© Peter Andreae COMP 102 9:4 Designing a program: filling in details public class HouseDrawer{ /** Draw a street of multi-story houses */ public void drawStreet( ){ // draw a house at ( 50, 400 ), random height // draw a house at ( 100, 400 ), random height // draw a house at ( 150, 400 ), random height // draw a house at ( 200, 400 ), random height // draw a house at ( 250, 400 ), random height // draw a house at ( 300, 400 ), random height // draw a house at ( 350, 400 ), random height // draw a house at ( 400, 400 ), random height } ?x How do we specify the positions of the houses? How do we work out a random height? ?y

© Peter Andreae COMP 102 9:5 Random numbers Math.random() computes and returns a random double between 0.0 and 1.0 To get a random number between min and max: min + random number * (max-min) ( Math.random() * 50.0) gives a value between and This is an expression: can assign it to a variable to remember it can use it inside a larger expression can pass it directly to a method

© Peter Andreae COMP 102 9:6 Designing a program: filling in details public class HouseDrawer{ /** Draw a street of multi-story houses */ public void drawStreet( ){ // draw a house at ( 50,400), random height // draw a house at (100,400), random height // draw a house at (150,400), random height // draw a house at (200,400), random height // draw a house at (250,400), random height // draw a house at (300,400), random height // draw a house at (350,400), random height // draw a house at (400,400), random height } How do we draw a house?

© Peter Andreae COMP 102 9:7 “Wishing Programming" public class HouseDrawer{ /** Draw a street of multi-story houses */ public void drawStreet( ){ double road = 400; // vertical position of bottom of houses // draw a house at ( 50, 400), of random height this.drawHouse( 50, road, (100+Math.random()*50) ); // draw a house at (100, 400), of random height this.drawHouse( 100, road, (100+Math.random()*50) ); : } } When there isn't a method to do what you want, wish for one! Make up a descriptive method name Work out what values it will need Call it on the same object Then go and define the method Have to be your own fairy godmother!!

© Peter Andreae COMP 102 9:8 Fulfilling the wish: defining the method public class HouseDrawer{ : public void drawStreet( ){ … : } /** Draw a multi-story house with windows */ public void drawHouse(double mid, double bot, double ht){ // Draw the rectangle of the house and the roof UI.drawRect(…….); UI.drawLine(…….); // Draw the windows } }

© Peter Andreae COMP 102 9:9 Drawing the outline public class HouseDrawer{ public static final double houseWd = 46; : public void drawStreet( ){ … : /** Draw a multi-story house with windows */ public void drawHouse(double mid, double bot, double ht){ // Draw the rectangle of the house and the roof double left = mid – houseWd/2.0; double right = mid + houseWd/2.0; double top = bot - ht; double tip = top – houseWd*0.7; UI.drawRect( left, top, houseWd, ht); UI.drawLine( left, top, mid, tip); UI.drawLine( right, top, mid, tip); // Draw the windows } } Work out what variables and constants we need, (mid,bot) ht wd Use them. Declare and initialise them,

© Peter Andreae COMP 102 9:10 Wishing for windows public class HouseDrawer{ : public void drawHouse(double mid, double bot, double ht){ double left = mid – houseWd/2; double right = mid + houseWd/2; double top = bot – ht; double tip = top – houseWd*2/3; UI.drawRect( left, top, houseWd, ht); UI.drawLine( left, top, mid, tip); UI.drawLine( right, top, mid, tip); // Draw the windows: this.drawWindow(….); } } (mid,bot) ht wd "Wish" for a drawWindow method How do we specify a window? - what could vary from window to window?

© Peter Andreae COMP 102 9:11 Wishing for windows public class HouseDrawer{ : public void drawHouse(double mid, double bot, double ht){ double left = mid – houseWd/2; double right = mid + houseWd/2; double top = bot – ht; double tip = top – houseWd*2/3; UI.drawRect( left, top, houseWd, ht); UI.drawLine( left, top, mid, tip); UI.drawLine( right, top, mid, tip); double winL = mid – houseWd*0.25; // Draw the windows: double winR = mid + houseWd*0.25; double winWd = houseWd*0.33; double lev1 = bot - ht*0.25; double lev2 = bot – ht*0.75; this.drawWindow(winL, lev1, winWd); this.drawWindow(winR, lev1, winWd); this.drawWindow(winL, lev2, winWd); this.drawWindow(winR, lev2, winWd); } (mid,bot) ht wd Now have to deliver on the "Wish"

© Peter Andreae COMP 102 9:12 Fulfilling the wish, again public class HouseDrawer{ : public void drawStreet( ){ … : this.drawHouse(50, road, (100+Math.random()*50) ); : public void drawHouse( double mid, double bot, double ht){ : this.drawWindow(winL, lev1, winWd); : /** Draw a window */ public void drawWindow(double midX, double midY, double sz){ double rad = sz/2; UIdrawRect(midX-rad, midY-rad, sz, sz); UI.drawLine(midX-rad, midY, midX+rad, midY); UI.drawLine(midX, midY-rad, midX, midY+rad); } }

© Peter Andreae COMP 102 9:13 Where have we been? Structured Design / “Wishful programming” Break the problem into managable, separable chunks One method for each chunk Work out the “interface” to each chunk: what information the method needs (the parameters) what information the method will return, if any. Use the chunk by calling the method, specifying arguments calls to a method in the same class usually called on the this object. Define the chunk by defining the method, specifying the parameters and the return type. specifying the actions in the body

© Peter Andreae COMP 102 9:14 main method (Side track) Running a program independently of BlueJ Don't have an object! Only have the class. Need a method that doesn't need an object, has a standard name can take any number of arguments!!! public static void main(String[ ] args){ HouseDrawer hd = new HouseDrawer(); hd.drawStreet(); } Can then create a jar file: set the main class include the comp102 library Does the actions that you currently do using Bluej to call a method What’s this? Magic incantation To be called when the class is run

© Peter Andreae COMP 102 9:15 Programs that make decisions Programs that perform the same action every time are boring! You can vary the action in a program By calling method with different arguments: UI.setColor(col); UI.drawRect(left, top, width, height); By getting input from the user: String name = UI.askString("name:"); : UI.printf(“Hello %s, how are you?”, name);

© Peter Andreae COMP 102 9:16 Programs that make decisions But this just changes the values, not the action itself. To vary the action inside a method: Need a conditional, or choice, statement: If some condition is true Then do this action Else to that action Eg If the height > 150 Then draw three storeys Else draw two storeys

© Peter Andreae COMP 102 9:17 Decisions in Java Java has an if … else statement: Can do an action only in some circumstances: if ( action.equals("reset") ) { UI.clearGraphics(); this.drawBoard(10, 10, 100); } Can choose between different actions: if ( width > height ) { UI.drawRect(left, top, width, height); } else { UI.fillOval(left, top, width, height); }

© Peter Andreae COMP 102 9:18 Java: if and if … else LDC 4.2 Forms of the if statement: if (〈condition 〉) { 〈actions to perform if condition is true 〉 } ⇒ just skip the actions when the condition is not true ! and if (〈condition 〉 ) { 〈actions to perform if condition is true 〉 } else { 〈actions to perform if condition is false 〉 } Note: the { … } represent a “Block” – a sequence of actions that are wrapped up together into a single statement.

© Peter Andreae COMP 102 9:19 if statement syntax if(boolean valued expression{ } else statements ) { }

© Peter Andreae COMP 102 9:20 Method with a condition /** Ask for amount and currency; print note if –ve, print value.*/ public void convertMoney( ) { ; double amt = UI.askDouble(“Enter amount $NZ”); if ( amt < 0 ) { UI.println(“Note: you have entered a debt!”); } String cur = UI.askString (“Enter currency (US or Aus)”); if ( cur.equals(“US”) ) { UI.printf(“$NZ%4.2f = $US%4.2f\n”, amt, (amt * 0.815)); } else { UI.printf(“$NZ%4.2f = $AUS%4.2f\n”, amt, (amt * 0.779)); } }

© Peter Andreae COMP 102 9:21 Calling Methods: How does it work? An object is like a filing card in a filing box, marked with its category (class) and an identifier A method definition is like a master copy of a worksheet Every time you call a method on an object, you make a copy of the worksheet, and work through it. The identifier of the object is copied into the box called “this”. Parameters of the method are like boxes at the top of the worksheet: you copy the arguments into the boxes. When you have finished the worksheet, you throw it away, along with any information stored in its boxes.

© Peter Andreae COMP 102 9:22 HouseDrawer Draw a street of houses of different sizes: