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

Slides:



Advertisements
Similar presentations
Xiaoying Gao Computer Science Victoria University of Wellington Copyright: Xiaoying Gao, Peter Andreae, Victoria University of Wellington Graphical Output,
Advertisements

MiniDraw Testing COMP 102 # T1
Chapter 2: Using Objects Part 1. To learn about variables To understand the concepts of classes and objects To be able to call methods To learn about.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Designing with Methods COMP.
 2005 Pearson Education, Inc. All rights reserved Introduction.
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.
Java An introduction. Example 1 public class Example1 { public static void main (String [] args) { System.out.println (“This is the first example”); int.
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 #
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Program Elements and Syntax.
Introduction to Java Appendix A. Appendix A: Introduction to Java2 Chapter Objectives To understand the essentials of object-oriented programming in Java.
Xiaoying Gao Computer Science Victoria University of Wellington Copyright: Xiaoying Gao, Peter Andreae, Victoria University of Wellington Conditionals.
© Xiaoying Gao, Peter Andreae Class, method, statements COMP 102 #3 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 Summary and Exam COMP 102.
David Streader Computer Science Victoria University of Wellington Copyright: David Streader, Victoria University of Wellington GUI and the UI API COMP.
David Streader Computer Science Victoria University of Wellington Copyright: David Streader, Victoria University of Wellington While loops and the UI API.
© 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.
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.
Input, Output, and Processing
David Streader Computer Science Victoria University of Wellington Copyright: David Streader, Victoria University of Wellington Java Programing Basics COMP.
Chapter 3 Syntax, Errors, and Debugging Fundamentals of Java.
Xiaoying Gao Computer Science Victoria University of Wellington Copyright: Xiaoying Gao, Peter Andreae, Victoria University of Wellington Call a Method,
David Streader Computer Science Victoria University of Wellington Copyright: David Streader, Victoria University of Wellington Java Programing Basics COMP.
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 Methods with Parameters COMP.
C++ Basics C++ is a high-level, general purpose, object-oriented programming language.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
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 Designing with Methods COMP.
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 Classes, Objects, Fields,
Xiaoying Gao Computer Science Victoria University of Wellington Copyright: Xiaoying Gao, Peter Andreae, Victoria University of Wellington Creating Objects.
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.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Types and Interfaces COMP.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Methods Calling Methods Return.
Xiaoying Gao Computer Science Victoria University of Wellington Copyright: Xiaoying Gao, Peter Andreae, Victoria University of Wellington Creating Objects.
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.
1 Project 2: Using Variables and Expressions. 222 Project 2 Overview For this project you will work with three programs Circle Paint Ideal_Weight What.
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,
SourceAnatomy1 Java Source Anatomy Barb Ericson Georgia Institute of Technology July 2008.
Variables in C Topics  Naming Variables  Declaring Variables  Using Variables  The Assignment Statement Reading  Sections
CMSC 104, Version 8/061L09VariablesInC.ppt Variables in C Topics Naming Variables Declaring Variables Using Variables The Assignment Statement Reading.
© 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.
David Streader Computer Science Victoria University of Wellington Copyright: David Streader, Victoria University of Wellington Java Programing Basics COMP.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Program Elements and Syntax.
Introduction to Computer Program Design COMP T1 .
Topic: Python’s building blocks -> Variables, Values, and Types
Chapter 3 Syntax, Errors, and Debugging
Introduction to Computer Program Design COMP T1 .
Topic: Python’s building blocks -> Variables, Values, and Types
Introduction to Computer Science / Procedural – 67130
Programs with graphics output
Computer Programming Methodology File Input
Variables in C Topics Naming Variables Declaring Variables
Presentation transcript:

Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington UI methods, Graphical Output COMP T1 #5

© 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 Administration: Help Desks: TBA: CO 242b On-line help: read the “Getting Help and FAQ” page. (Please don’t me directly for assignment help problems) Optional Tutorial, Tue 4-5 in AM 104 (from next week)

© Peter Andreae COMP102 5:3 Assignment 2 Calculator programs Flag drawing programs

© Peter Andreae COMP102 5:4 Values / Data There are lots of different kinds or “Types” of values: Numbers Integers( int or long) real numbers ( double or float ) e-34 … Characters ( char )'X' '4' Text ( String ) " F -> " Colours ( Color )Color.red Color.green Method namesthis::fahrenheitToCelsius Other Objects …

© Peter Andreae COMP102 5:5 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) "

© Peter Andreae COMP102 5:6 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 〉 =; "

© Peter Andreae COMP102 5:7 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 its first assignment statement : /** Print out the conversion formulas (version 2) */ public void printFormula () { String formula = " Celsius = (Fahrenheit - 32) *5/9" ; UI.println( formula ); } " Telling computer to make a space for a value (strictly, once in a block)

© Peter Andreae COMP102 5:8 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

© Peter Andreae COMP102 5:9 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"); } Fahrenheit: F is 30.0C

© Peter Andreae COMP102 5:10 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 eg UI.addButton("Quit", UI::quit); How do you find out about all the methods? How do your find out what arguments you need to provide?

© Peter Andreae COMP102 5:11 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 htmlhttp://ecs.victoria.ac.nz/foswiki/pub/Main/JavaResources/javaAPI- 102.html Tailored for Comp 102 Includes documentation of the ecs100 library: (UI, Trace, etc,) puts most useful classes at the top of the list. Use the documentation while you are programming!

© Peter Andreae COMP102 5:12 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

© Peter Andreae COMP102 5:13 Programs with graphics output Write a program to draw a lollipop: Design What shapes can we draw? UI has methods to draw rectangles, ovals, lines, arcs,… ⇒ Draw one thick black line one red oval, How do we draw them? Need to set the color first (initially black) 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. x y (0,0) Shapes drawn on top of previous shapes Coordinates measured from left and top

© Peter Andreae COMP102 5:14 Lollipop program Design: Method drawLollipop(): set line width to 10 draw line set line width back to 1 set color to red fill oval Must work out the coordinates: line: oval: x y ≈ 600 ≈500 (300) (200)

© Peter Andreae COMP102 5:15 Writing the program Need import statements Need a class (with a descriptive comment) Need a constructor Need a method (with a descriptive comment) import ecs100.*; import java.awt.Color; /** Draws little shapes on the graphics pane */ public class Drawer { /** Constructor: Set up interface */ public Drawer() { UI.addButton("Draw it", this::drawLollipop); } /** Draw an red lollipop with a stick */ public void drawLollipop() { // actions } } Write the method body as comments first Button that will call the drawLollipop method of this class

© Peter Andreae COMP102 5:16 Writing the program: using comments import ecs100.*; import java.awt.Color ; /** Draws little pictures on the graphics pane */ public class Drawer { public Drawer() { UI.addButton("Draw it", this::drawLollipop); } /** Draw an orange face with a brimmed hat */ public void drawLollipop() { // set line width to 10 // draw line (300,200) to (300, 400) // set line width to 1 // set color to red // fill 80x80 } Do it in BlueJ! Now express each comment in Java (look up documentation as necessary)

© Peter Andreae COMP102 5:17 Writing the program import ecs100.*; import java.awt.Color ; /** Draws little pictures on the graphics pane */ public class Drawer { public Drawer() { UI.addButton("Draw it", this::drawLollipop); } /** Draw a lollipop */ public void drawLollipop() { UI.setLineWidth(10);// set line width to 10 UI.drawLine(200, 300, 200, 400);// draw line UI.setLineWidth(1);// set line width back to 1 UI.setColor(Color.red);// set color to red UI.fillOval(260, 160, 80, 80);// draw blob } }

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

© Peter Andreae COMP102 5:19 Improving the design This program is very inflexible: What if We want the lollipop to be in a different position? We want the lollipop to be bigger or smaller? We want the stick to be longer? …. 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

© Peter Andreae COMP102 5:20 Values to specify lollipop & stick x size y top left stickLength

© Peter Andreae COMP102 5:21 Improving the program: Constants import ecs100.*; import java.awt.Color; /** Draws face with a hat on the graphics pane */ public class Drawer { public static final double x = 300;// horizontal center of lollipop public static final double y = 180; // vertical center of lollipop public static final double size = 80;// diameter of lollipop public static final double stick = 200;// length of lollipop stick /** Draw a lollipop */ public void drawLollipop() { Defining a value that can’t be changed while the program is running.

© Peter Andreae COMP102 5:22 Improving the program: constants import ecs100.*; import java.awt.Color; /** Draws face with a hat on the graphics pane */ public class Drawer { public static final double x = 300;// horizontal center of lollipop public static final double y = 180; // vertical center of lollipop public static final double size = 80;// diameter of lollipop public static final double stick = 200;// length of lollipop stick /** Draw a lollipop */ public void drawLollipop() { UI.setLineWidth(10); UI.drawLine(x, y, x, y+stick); UI.setLineWidth(1); UI.setColor(Color.red); UI.fillOval(x-size/2, y-size/2, size, size); } Easy to change: one place!

© Peter Andreae COMP102 5:23 Syntax rules: Program structure 2nd version 〈import statements 〉 public class 〈 method descriptions 〉 { } 〈 classname 〉 〈 constants 〉 public static final 〈 type 〉〈 name 〉

© Peter Andreae COMP102 5:24 Improving the program: variables ⋮ public static final double x = 300;// horizontal center of lollipop public static final double y = 180; // vertical center of lollipop public static final double size = 80;// diameter of lollipop public static final double stick = 200;// length of lollipop stick /** Draw a lollipop */ public void drawLollipop() { double left = x – size/2;// left of lollipop double top = y – size/2;// top of lollipop double bot = y + stick; // bottom of stick UI.setLineWidth(10); UI.drawLine(x, y, x, bot); UI.setLineWidth(1); UI.setColor(Color.red); UI.fillOval(left, top, size, size); }

© Peter Andreae COMP102 5:25 Principle of good design Use well named constants or variables where-ever 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 “x" instead of “left" ? why did I choose “y" instead of stick bottom? We have effectively parameterised the drawing Four values (x, y, size, stick) control the whole thing.

© Peter Andreae COMP102 5:26 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