Download presentation
Presentation is loading. Please wait.
Published byAmbrose Benson Modified over 9 years ago
1
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Designing with Methods COMP 102 #9 2011
2
© Peter Andreae COMP 102 9:2 Menu Designing a program with multiple methods Methods that call methods from the same class. Administrivia: Test: The 1st test is next Thursday! If you can't get to test: Some planned event: talk/email me or Ambreen beforehand Sick/accident: email/call as soon as possible rooms: MCLT103 and probably CO LT122 and KK LT301 as well Brief Java Documentation for test is on the web. Equity Help sessions Mondays 10, and/or 4pm, sign up with sheets in Faculty Office
3
Read a Story & Get Free Coffee! Take a 15 minute break to participate in our online study exploring readers’ emotional response to interactive stories. You will be rewarded with fun experience and a $5 Coffee Voucher in a Wishbone café! To participate & claim your voucher please email at: interstories@gmail.com
4
© Peter Andreae COMP 102 9:4 Where we have been Designing methods: Statements Variable declarations Assignment statements and expressions Method calls (passing arguments) if ( … ) { … } and if ( … ) { … } else { … } while ( … ) { … } Defining methods (with parameters) (vs calling methods) Input Parameters Scanner and System.in JOptionPane Output System.out DrawingCanvas
5
© Peter Andreae COMP 102 9:5 HouseDrawer Draw a street of houses of different random sizes: assume size is between 100 and 150
6
© Peter Andreae COMP 102 9:6 HouseDrawer Draw a street of houses of different sizes:
7
© Peter Andreae COMP 102 9:7 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 ), ? high // draw a house at ( 100, 400 ), ? high // draw a house at ( 150, 400 ), ? high // draw a house at ( 200, 400 ), ? high // draw a house at ( 250, 400 ), ? high // draw a house at ( 300, 400 ), ? high // draw a house at ( 350, 400 ), ? high // draw a house at ( 400, 400 ), ? high } How do we specify the positions of the houses? How do we work out a random height?
8
© Peter Andreae COMP 102 9:8 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: multiply random number by (max-min) add to min: (100 + Math.random() * 50) gives a value between 100.0 and 150.0 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
9
© Peter Andreae COMP 102 9:9 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), ? high // draw a house at (100,400), ? high // draw a house at (150,400), ? high // draw a house at (200,400), ? high // draw a house at (250,400), ? high // draw a house at (300,400), ? high // draw a house at (350,400), ? high // draw a house at (400,400), ? high } How do we draw a house?
10
© Peter Andreae COMP 102 9:10 “Wishing Programming" public class HouseDrawer{ /** Draw a street of multi-story houses */ public void drawStreet( ){ // draw a house at ( 50,400), ? high this.drawHouse( 50, 400, (100+Math.random()*50) ); // draw a house at (100,400), ? high this.drawHouse( 100, 400, (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!!
11
© Peter Andreae COMP 102 9:11 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 } }
12
© Peter Andreae COMP 102 9:12 Drawing the outline 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 double wd = 46; double left = mid – wd/2; double right = mid + wd/2; double top = bot - ht; double tip = top – wd*2/3; UI.drawRect( left, top, wd, ht); UI.drawLine( left, top, mid, tip); UI.drawLine( right, top, mid, tip); // Draw the windows } } Work out what variables we need, (mid,bot) ht wd Use them. Declare and initialise them,
13
© Peter Andreae COMP 102 9:13 Wishing for windows public class HouseDrawer{ : public void drawHouse(double mid, double bot, double ht){ double wd = 46; double left = mid – wd/2; double right = mid + wd/2; double top = bot – ht; double tip = top – wd*2/3; UI.drawRect( left, top, wd, 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?
14
© Peter Andreae COMP 102 9:14 Wishing for windows public class HouseDrawer{ : public void drawHouse(double mid, double bot, double ht){ double wd = 46; double left = mid – wd/2; double right = mid + wd/2; double top = bot – ht; double tip = top – wd*2/3; UI.drawRect( left, top, wd, ht); UI.drawLine( left, top, mid, tip); UI.drawLine( right, top, mid, tip); // Draw the windows: double winL = mid - wd/4; double winR = mid + wd/4; double lev1 = bot - ht/4; double lev2 = bot – 3* ht/4; this.drawWindow(winL, lev1, wd/3); this.drawWindow(winR, lev1, wd/3); this.drawWindow(winL, lev2, wd/3); this.drawWindow(winR, lev2, wd/3); } (mid,bot) ht wd Now have to deliver on the "Wish"
15
© Peter Andreae COMP 102 9:15 Fulfilling the wish, again public class HouseDrawer{ : public void drawStreet( ){ … : public void drawHouse( double mid, double bot, double ht){ : /** 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); } }
16
© Peter Andreae COMP 102 9:16 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
17
© Peter Andreae COMP 102 9:17 main method 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(); } > java HouseDrawer
18
© Peter Andreae COMP 102 9:18 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.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.