Download presentation
Presentation is loading. Please wait.
Published bySage Stear Modified over 9 years ago
1
Xiaoying Gao Computer Science Victoria University of Wellington Copyright: Xiaoying Gao, Peter Andreae, Victoria University of Wellington Graphical Output, Call a method in the same class COMP 102 #5 2014T2
2
© Xiaoying Gao, Peter Andreae COMP102 5:2 Menu Programs with Graphical output Good design with variables Call a method in the same class this parameters Administrivia: A2 is handed out Class Rep: Shaika, Elroy This Friday: last day for withdrawal with refund
3
© Xiaoying Gao, Peter Andreae COMP 102 5: 3 Version 1 import ecs100.*; import java.awt.Color; public class DrawShapes{ public void drawPicture(){ UI.setColor(Color.green); UI.fillRect(100,150,200,100); UI.setColor(Color.red); UI.fillOval(150,150, 100, 100); UI.setColor(Color.orange); UI.drawLine(0, 200, 400, 200); UI.fillArc(150, 150, 100, 100, 30, 120); }
4
© Xiaoying Gao, Peter Andreae COMP 102 5: 4 Version 2 import ecs100.*; import java.awt.Color; public class DrawShapes2{ public void drawPicture(){ double x = 200; //centr x double y = 200; //center y double d = 100; //diameter UI.setColor(Color.green); UI.fillRect(x-d, y-d/2, 2*d, d); UI.setColor(Color.red); UI.fillOval(x-d/2, y-d/2, d, d); UI.setColor(Color.orange); UI.drawLine(x-2*d, y, x+2*d, y); UI.fillArc(x-d/2, y-d/2, d, d, 30, 120); }
5
© Xiaoying Gao, Peter Andreae COMP102 5:5 Principle of good design Use well named variables where-ever 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 "centerX" instead of “boxLeft" or “lineLeft" ? why did I choose “diameter" to be the size? We have effectively parameterised the drawing three values (x, y, d) control the whole thing.
6
© Xiaoying Gao, Peter Andreae COMP102 5:6 Call another method in the same class import ecs100.*; public class Example4{ public void printMessages(){ this.message(); } public void message(){ UI.println("Hello "); //can do complicated things instead }
7
© Xiaoying Gao, Peter Andreae COMP102 5:7 Call a method with parameters import ecs100.*; public class Example5{ public void printMessages(){ this.message("Alex"); this.message("Linda"); this.message("Sam"); } public void message(String name){ UI.println("Hello " + name); }
8
© Xiaoying Gao, Peter Andreae COMP102 5:8 Call a method with more parameters import ecs100.*; public class Example6{ public void printMessages(){ UI.println("room booking"); this.message("Alex", 20); this.message("Linda", 12); this.message("Sam", 5); } public void message(String name, int x){ UI.println("Hello " + name); UI.println("room no." + x); }
9
© Xiaoying Gao, Peter Andreae COMP102 5:9 Call a method in the same class In the same class, normally this.methodName(arguments) this is the default object for the current class Arguments: the values to initialise the parameters Parameters: In the method to be called Method header (type name, type name, type name) Arguments and parameters have to match Type Order
10
© Xiaoying Gao, Peter Andreae COMP102 5:10 Method Definitions (2) /** A method with parameters*/ public void message( String name, int x ){ ….; 〈 Comment 〉〈 Header 〉〈 Body 〉 {} public void 〈 parameters 〉 () 〈 name 〉 〈 type 〉, 〈 name 〉 How do you call a method with parameters from BlueJ?
11
© Xiaoying Gao, Peter Andreae COMP102 5:11 More Examples import ecs100.*; public class Triangle{ public void ManyTriangles(){ this.drawTri(100, 100, 40, 50, 200, 400); this.drawTri(300,200, 150, 100, 200, 200); } public void drawTri(double x1, double y1, double x2, double y2, double x3, double y3){ UI.drawLine(x1, y1, x2, y2); UI.drawLine(x2, y2, x3, y3); UI.drawLine(x3, y3, x1, y1); }
12
© Xiaoying Gao, Peter Andreae COMP102 5:12 More examples import ecs100.*; import java.awt.Color; public class DrawShapes3{ public void drawMany(){ this.drawPicture(300, 100, ); this.drawPicture(300,200, 100); } public void drawPicture(double x, double y, double d){ UI.setColor(Color.green); UI.fillRect(x-d,y-d/2,2*d,d); UI.setColor(Color.red); UI.fillOval(x-d/2, y-d/2, d, d); UI.setColor(Color.orange); UI.drawLine(x-2*d, y, x+2*d, y); UI.fillArc(x-d/2, y-d/2, d, d, 30, 120);} }
13
© Xiaoying Gao, Peter Andreae COMP102 5:13 Method Definition: Like a pad of worksheets Calling a Method: this.drawPicture(300, 100, 75); ⇒ 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 drawPicture( double x, double y, double d){ UI.setColor(Color.green); UI.fillRect(x-d,y-d/2,2*d,d); public void drawPicture( double x, double y, double d){ UI.setColor(Color.green); UI.fillRect(x-d,y-d/2,2*d,d); 300 0 100 0 75 0
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.