Download presentation
Presentation is loading. Please wait.
Published byRodney Spencer Modified over 9 years ago
1
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington UI methods, Graphical Output COMP 102 2013T1 #5
2
© Peter Andreae COMP102 5:2 Menu More syntax rules 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 Administrivia: Help Desk: Friday 2-4, CO 242 Optional Tutorial, Monday 1-2 CO 118
3
© Peter Andreae COMP102 5:3 Variables /** Print out the conversion formulas (version 2) */ public void printFormula () { String formula; formula = "Celsius = (Fahrenheit - 32) *5/9" ; UI.println( formula ); } A variable is a place that can hold a value. Must specify the type of value that can be put in the variable ⇒ “Declare” the variable. Must put a value into a variable before you can use it ⇒ “assign” to the variable Can use the value by specifying the variable’s name Can change the value in a variable (unlike mathematical variable) "
4
© Peter Andreae COMP102 5:4 Assignment Statements /** Print out the conversion formulas (version 2) */ public void printFormula () { String formula; formula = "Celsius = (Fahrenheit - 32) *5/9" ; UI.println( formula ); } Assignment Statement: where = what ; name-of-place= specification-of-value ; formula= " Celsius = (Fahrenheit - 32) *5/9" Compute the value and put it in the place 〈 variable 〉〈 expression 〉 =; "
5
© Peter Andreae COMP102 5:5 Declarations and Assignments Variable must be declared before you use it Must be declared only once in a method Must be assigned a value before it is used in an expression Can declare variable before first assignment or Declare variable in the first assignment statement: /** Print out the conversion formulas (version 2) */ public void printFormula () { String formula = " Celsius = (Fahrenheit - 32) *5/9" ; UI.println( formula ); } " (strictly, once in a block)
6
© Peter Andreae COMP102 5:6 Expressions /** Convert from fahrenheit to celsius */ public void fahrenheitToCelsius(){ double fahren = UI.askDouble("Fahrenheit:" ); double celsius = (fahren - 32) * 5 / 9; UI.println(fahren + “F is " + celsius + “ C"); } Expressions describe how to compute a value. Expressions are constructed from values variables operators (+, -, *, /, etc) method calls that return a value sub-expressions, using (… ) … + for Strings: "concatenates" the Strings
7
© Peter Andreae COMP102 5:7 Method Definition: Like a pad of worksheets public void fahrenToCelsius(){ double fahren = UI.askDouble("Fahrenheit:"); double celsius = (fahren – 32) * 5 / 9; UI.println(fahren + “F is " + celsius + “ C"); } Calling a Method: tempCalc1.fahrenheitToCelsius(); ⇒ 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: a (good) metaphor public void fahrenheitToCelsius(){ double fahren = UI.askDouble("Fahrenheit:"); double celsius = (fahren – 32) * 5 / 9; UI.println(fahren + “ is " + celsius + “ C"); } 86 0 30 0 Fahrenheit:86 86.0F is 30.0C
8
© Peter Andreae COMP102 5:8 What can the UI do? UI is 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? Actually, it is a class with static methods, which acts like a predefined object
9
© Peter Andreae COMP102 5:9 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/foswiki/pub/Main/JavaResources/javaAPI- 102.htmlhttp://ecs.victoria.ac.nz/foswiki/pub/Main/JavaResources/javaAPI- 102.html 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!
10
© Peter Andreae COMP102 5:10 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(x 1, y 1, x 2, y 2 )UI.drawImage(file, left, top ) …… Eg: Color.red
11
© Peter Andreae COMP102 5:11 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
12
© Peter Andreae COMP102 5:12 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
13
© Peter Andreae COMP102 5:13 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 java.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 Please fix the error on your handout!!
14
© Peter Andreae COMP102 5:14 Writing the program: using comments import comp102.*; import java.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) 100x160 // set color to green // draw rectangle @(200,100) 100x40 // draw line (180,140) to (320, 140) } Now express each comment in Java (look up the documentation if necessary) Do it in BlueJ!
15
© Peter Andreae COMP102 5:15 Writing the program import comp102.*; import java.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, 160);// draw oval UI.setColor(Color.green);// set color to green UI.fillRect(200, 100, 100, 40);// draw rectangle UI.drawLine(180, 140, 320, 140);// draw line } }
16
© Peter Andreae COMP102 5:16 Compile, Run, and Test the program. Fix the errors!
17
© Peter Andreae COMP102 5:17 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 understand ⇒ difficult to change (have to find all the places and redo all the arithmetic Better design: Use named constants and variables ⇒ easier to write and easier to change ⇒ get the computer to do the arithmetic
18
© Peter Andreae COMP102 5:18 Values to specify face & hat centerX top width height hatBot hatHt centerY left
19
© Peter Andreae COMP102 5:19 Improving the program: constants import comp102.*; import java.awt.Color; /** Draws face with a hat on the graphics pane */ public class Drawer { public static final double centerX = 250;// horizontal center of face public static final double centerY= 180;// vertical center of face public static final double width = 100; public static final double height = 160; public static final double left = centerX–width/2; public static final double top = centerY–height/2; public static final double hatHt = 0.25 * height; public static final double hatBot = top +hatHt; public static final double brimLt = centerX–0.8*width; public static final double brimRt = centerX+0.8*width;
20
© Peter Andreae COMP102 5:20 Improving the program: constants ⋮ public static final double brimLt = centerX – 0.8 * width; public static final double brimRt = centerX + 0.8 * width; /** Draw an orange face with a brimmed hat*/ public void faceWithHat() { UI.setColor(Color.orange); UI.fillOval(left, top, width, faceHt);// face UI.setColor(Color.green); UI.fillRect(left, top, width, hatHt); // hat crown UI.drawLine(brimLt, hatBot, brimRt, hatBot) //brim
21
© Peter Andreae COMP102 5:21 Syntax rules: Program structure 2nd version 〈import statements 〉 public class 〈 method descriptions 〉 { } 〈 classname 〉 〈 constants 〉
22
© Peter Andreae COMP102 5:22 Improving the program: variables /** Draw an orange face with a brimmed hat*/ public void faceWithHat() { double centerX = 250;// horizontal center of face double centerY= 180;// vertical center of face double faceWd = 100; double faceHt = 150; double faceLeft = centerX–faceWd/2,faceTop = centerY–faceHt/2; double hatHt = 0.3 * faceHt, hatWd = 0.96 * faceWd; double hatLeft = centerX–hatWd/2; double hatBot = centerY–0.25*faceHt,hatTop = hatBot – hatHt; double brimLt = centerX–0.8*hatWd,brimRt = centerX+0.8*hatWd; UI.setColor(Color.orange); UI.fillOval(faceLeft, faceTop, faceWd, faceHt);// face UI.setColor(Color.green); UI.fillRect(hatLeft, hatTop, hatWd, hatHt); // hat crown UI.drawLine(brimLt, hatBot, brimRt, hatBot) //brim } Can declare multiple variables on one line
23
© Peter Andreae COMP102 5:23 Principle of good design Use well named constants or variables whereever possible, rather than literal values ⇒ easier to understand ⇒ easier to get right ⇒ much easier to modify Choosing the right constants or variables is an art!! why did I choose "centerX" instead of "faceLeft" or "brimLeft" ? why did I choose "centerY" to be the center of the face, not the hat? We have effectively parameterised the drawing Four values (centerX, centerY, width, faceHt) control the whole thing.
24
© Peter Andreae COMP102 5:24 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 Alzheimer's – only remembers what he writes down with a clipboard for the instructions he is currently working on looking at the back of the UI window which he can write/paint on, but the writing/painting only appears on the outside – he can't see what he has written/drawn can see a stream of characters that the user types on the keyboard
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.