Presentation is loading. Please wait.

Presentation is loading. Please wait.

Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington UI methods, Graphical Output,

Similar presentations


Presentation on theme: "Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington UI methods, Graphical Output,"— Presentation transcript:

1 Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington UI methods, Graphical Output, Choice COMP 102 #5 2011

2 © Peter Andreae COMP102 5:2 Menu A model for the computer: calling methods What methods can you call on the UI object? Using documentation to find out about classes Programs with Graphical output[L-D-C: Appendix F] Good design with variables Methods with parameters[L-D-C: 4.1 – 4.3 ] Administrivia: Class Rep Assig 2 is available 2 programs, based on TemperatureConverter and Drawer Optional Tutorial, Friday 2-3 AM 104

3 © Peter Andreae COMP102 5:3 Class Rep Stefan Koetsier I completed the Certificate of University Preparation course at Victoria over the summer, so I know what the role entails and I'm confident that having held similar class representative positions previously, I'd be a good liaison between the COMP 102 students and their lecturers. Joshua Scott I may not be as qualified to be a representative as some other candidates, but I'm willing to take the role head on. Currently started on my Bsc in computer science and am planning on a Master's in Network Engineering, so I would like this chance to improve both my leadership skills and knowledge of the subject as a whole. As for what I can bring to the role, I have an above par understanding of computers and learn things very quickly. I'm both helpful and polite as well as easy to approach if you have any questions or need any help.

4 © Peter Andreae COMP102 5:4 A model/metaphor for the computer If you are giving instructions to someone/something, it helps to understand how they think and what they can do! Your program is run by the "Java Virtual Machine" The JVM is like a clerk with alzheimers – only remembers what he writes down with a clipboard for the instructions he is currently working on looking out at the user through an opaque window (the UI window) which he can write on (but the writing only appears on the outside) can see a stream of characters that the user types on the keyboard

5 © Peter Andreae COMP102 5:5 Method Definition: Like a pad of worksheets public void fahrenToCelsius(){ double fahren = UI.askDouble("Fahrenheit:"); double celsius = (fahren – 32) * 5 / 9; UI.println(“ -> " + celsius + “ C"); } Calling a Method: tempCalc1.fahrenToCelsius(); ⇒ get a “copy” of the method worksheet ⇒ perform each action in the body. ⇒ throw the worksheet away (losing all the information on it) Method Calls: worksheet metaphor public void fahrenToCelsius(){ double fahren = UI.askDouble("Fahrenheit:"); double celsius = (fahren – 32) * 5 / 9; UI.println(“ -> " + celsius + “ C"); } 86 0 30 0

6 © Peter Andreae COMP102 5:6 What can the UI do? UI is a predefined object (strictly, it is a class with static methods, which acts like a predefined object ) Has methods for text input from the user eg UI.askString("What is your name?") text output eg UI.println(" * " + name + " * "; graphical output eg UI.drawRect(100, 100, 300, 150) making buttons, sliders, etc How do you find out about all the methods? How do your find out what arguments you need to provide?

7 © Peter Andreae COMP102 5:7 Read the Documentation! Full documentation for all the standard Java library code (the "API" : Application Programming Interface) Version of Java API documentation on course web site: "Java Documentation" in side bar http://ecs.victoria.ac.nz/Courses/COMP102_2011T1/JavaResources Tailored for Comp 102 Includes documentation of the comp102 library: (UI, Trace, etc,) puts most useful classes at the top of the list. Use the documentation while you are programming!

8 © Peter Andreae COMP102 5:8 Some UI methods Text: UI.clearText()UI.print(anything ) UI.println(anything ) UI.println() UI.printf( format-string, values…) UI.askString(prompt-string ) UI.askDouble(prompt-string ) UI.askInt(prompt-string ) UI.askBoolean(prompt-string ) UI.next()UI.nextInt()UI.nextDouble UI.nextLine() UI.hasNext() UI.hasNextIntUI.hasNextDouble() Graphics: UI.clearGraphics() UI.setColor(color ) UI.drawRect(left, top, wd, ht )UI.fillRect(left, top, wd, ht ) … UI.drawOval(left, top, wd, ht )UI.fillOval(left, top, wd, ht ) … UI.drawLine(x1, y1, x2, y2 )UI.drawImage(file, left, top )

9 © Peter Andreae COMP102 5:9 Programs with graphics output Write a program to draw a face with a hat: Design What shapes can we draw? UI has methods to draw rectangles, ovals, lines, arcs,… ⇒ Draw one green rectangle, one green line, one orange oval, How do we draw them? Need to set the color first then call the draw/fill methods: must specify the positions and size rectangles/ovals: left, top, width, height lines: x and y of each end. one orange oval, one green rectangle, one green line x y (0,0) Shapes drawn on top of previous shapes Coordinates measured from left and top

10 © Peter Andreae COMP102 5:10 FaceWithHat program Design: Method faceWithHat(): set color to orange draw oval set color to green draw rectangle draw line Must work out the coordinates: oval: rectangle: line: x y ≈ 600 ≈500

11 © Peter Andreae COMP102 5:11 Writing the program Need import statements Need a class (with a descriptive comment) what name? Need a method (with a descriptive comment) what name? what actions? import comp102.*; import awt.Color*; /** Draws little pictures on the graphics pane */ public class Drawer { /** Draw an orange face with a brimmed hat */ public void faceWithHat() { // actions } } Write the method body as comments first

12 © Peter Andreae COMP102 5:12 Writing the program: using comments import comp102.*; import awt.Color*; /** Draws little pictures on the graphics pane */ public class Drawer { /** Draw an orange face with a brimmed hat*/ public void faceWithHat() { // set color to orange // draw oval @(200,100) 100x150 // set color to green // draw rectangle @(200,90) 100x50 // draw line (180,140) to (320, 140) } Now express each comment in Java (look up the documentation if necessary) Do it in BlueJ!

13 © Peter Andreae COMP102 5:13 Writing the program import comp102.*; import awt.Color*; /** Draws little pictures on the graphics pane */ public class Drawer { /** Draw an orange face with a brimmed hat*/ public void faceWithHat() { UI.setColor(Color.orange);// set color to orange UI.fillOval(200, 100, 100, 150);// draw oval UI.setColor(Color.green);// set color to green UI.fillRect(200, 90, 100, 50);// draw rectangle UI.drawLine(180, 140, 320, 140);// draw line } } Express each comment in Java (look up the documentation if necessary)

14 © Peter Andreae COMP102 5:14 Compile, Run, and Test the program. Fix the errors!

15 © Peter Andreae COMP102 5:15 Improving the design This program is very inflexible: What if We want the face & hat to be in a different position? We want the face & hat to be bigger or smaller? We want the hat to be taller? …. We want to draw two of them? Current design is filled with literal values ⇒ difficult to change Better design: Use variables ⇒ easier to write and easier to change

16 © Peter Andreae COMP102 5:16 Variables to specify face & hat center top faceWd faceHt hatWd brimYhatHt

17 © Peter Andreae COMP102 5:17 Improving the program /** Draw an orange face with a brimmed hat*/ public void faceWithHat() { double center = 250;// horizontal center of face double top = 100;// top of face double faceWd = 100; double faceHt = 1.5 * faceWd; double hatHt = 0.3 * faceHt; double hatWd = 0.96 * faceWd; double brimWd = 1.6 * hatWd; double brimY = top + 0.25 * faceHt; UI.setColor(Color.orange); UI.fillOval(center-faceWd/2, top, faceWd, faceHt); // face UI.setColor(Color.green); UI.fillRect(center-hatWd/2, brimY-hatHt, hatWd, hatHt); // hat crown UI.drawLine(center-brimWd/2, brimY, center+brimWd/2, brimY)//brim }

18 © Peter Andreae COMP102 5:18 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 "center" instead of "faceLeft" or "brimLeft" ? why did I choose "top" to be the top of the face, not the hat? We have effectively parameterised the drawing three values (center, top, faceWd) control the whole thing.


Download ppt "Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington UI methods, Graphical Output,"

Similar presentations


Ads by Google