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 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.
Chapter 2 storing numbers and creating objects Pages in Horstmann.
COMP More About Classes Yi Hong May 22, 2015.
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 #
© 2007 Lawrenceville Press Slide 1 Chapter 7 Top-Down Development  Problem-solving approach  Breaking a task down into smaller subtasks  First level.
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.
© 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.
Week 3 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
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.
© Xiaoying Gao, Peter Andreae Introduction to Computer Program Design COMP T2. 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.
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.
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, Peter Andreae Introduction to Computer Program Design COMP T2. 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.
Xiaoying Gao Computer Science Victoria University of Wellington Copyright: Xiaoying Gao, Peter Andreae, Victoria University of Wellington Conditionals.
Xiaoying Gao Computer Science Victoria University of Wellington Copyright: Xiaoying Gao, Peter Andreae, Victoria University of Wellington More Interfaces.
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,
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.
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.
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 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,
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.
Invoking methods in the Java library. Jargon: method invocation Terminology: Invoking a method = executing a method Other phrases with exactly the same.
© 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.
A High Flying Overview CS139 – Fall 2010 How far we have come.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Recursion COMP 102 # T1.
CCSA 221 Programming in C CHAPTER 3 COMPILING AND RUNNING YOUR FIRST PROGRAM 1 ALHANOUF ALAMR.
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
Program with two classes COMP 102 # T2
Programming Language Concepts (CIS 635)
Chapter 2 Using Objects.
Week 4 Lecture-2 Chapter 6 (Methods).
Methods/Functions.
Parameters and Arguments
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 2011

© 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/ me or Ambreen beforehand Sick/accident: /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

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 at:

© 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

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

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

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

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

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

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

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

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

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

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

© 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

© 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

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