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: