Download presentation
Presentation is loading. Please wait.
Published byClaribel Morris Modified over 9 years ago
1
© Xiaoying Gao, Peter Andreae UI methods, Variables, Constants COMP 102 #4 2015T2 Xiaoying Sharon Gao Computer Science Victoria University of Wellington
2
Admin Good news: Additional help for all 1 st year student Helpdesk Monday, Wednesday and Friday 11am-12 in CO242B Night tutorials Monday 5pm-7pm in AM103 Wednesday 5pm-7pm in CO118 Our own COMP102 helpdesk and tutorial starts next week Class rep Candidate: Christopher Parry COMP 102 1:2
3
Menu UI methods Graphical output Variables, Data types, Expressions, Assignment statement Constants Reading: Text book: chapter 2 COMP 102 1:3
4
© Xiaoying Gao, Peter Andreae COMP 102 4: 4 Summary of Java program structure A Class specifies a type of object TemperatureCalculator class describes TemperatureCalculator objects A Class contains a collection of methods each method is an action the objects can perform. TemperatureCalculator objects can do celsiusToFahren, fahrenToCelsius, printFormula If you have an object, you can call its methods on it. A method definition specifies how to perform the action A method contains statements each statement specifies one step of performing the action Declaration and Assignment statements Method call statements
5
© Xiaoying Gao, Peter Andreae COMP 102 4: 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) Run(Call) a Method : a metaphor public void fahrenToCelsius(){ double fahren = UI.askDouble("Fahrenheit:"); double celsius = (fahren – 32) * 5 / 9; UI.println(“ -> " + celsius + “ C"); } 86 0 30 0
6
© Xiaoying Gao, Peter Andreae COMP 102 4: 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
© Xiaoying Gao, Peter Andreae COMP 102 4: 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_2014T2/JavaResources 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!
8
© Xiaoying Gao, Peter Andreae COMP 102 4: 8 Some UI methods: read the documentation 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
© Xiaoying Gao, Peter Andreae COMP 102 4: 9 Method Calls UI.println( "Celsius = (Fahrenheit - 32) *5/9" ); Method call Statement: who. what ( data to use) ; UI. println ( “Celsius = (Fahre…” ) ; Meaning of Statement: Tell the object to perform the method using the argument values provided 〈 object 〉 〈 methodname 〉〈 arguments 〉.();
10
© Xiaoying Gao, Peter Andreae COMP 102 4: 10 Objects and their methods in Java What objects are there? Predefined: find them in the java documentation UIa "User Interface" window with several panes println(….), askString(…), drawRect(…), clearGraphics() Systemrepresenting the computer system currentTimeMillis() Mathmethods for mathematical calculations random(), sin(…) Others The object(s) defined by your program New objects that your program creates Some method calls return a value
11
© Xiaoying Gao, Peter Andreae COMP 102 4: 11 Draw some shapes with colors
12
© Xiaoying Gao, Peter Andreae COMP 102 4: 12 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); }
13
© Xiaoying Gao, Peter Andreae COMP 102 4: 13 Version 2 import ecs100.*; import java.awt.Color; public class DrawShapes2{ public void drawPicture(){ double x = 200; //centre x double y = 200; //centre 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); }
14
© Xiaoying Gao, Peter Andreae COMP 102 4: 14 Data Types text book 2.3 A type is a kind/category of value Every variable must have a type Java has: Primitive types: double-3.495, 6.482E23 int-2358, 45, (integers from -2 billion to 2 billion) longintegers from -2 63 to 2 63 char‘D’, ‘d’, ‘=‘ booleantrue, false byte, short, float: … (see the book for details) Object types: String“Hello John”, “600.45”, “F”, “true” Scanner PrintStream Color…. (lots of predefined types) TemperatureConverter…. Every class defines a type
15
© Xiaoying Gao, Peter Andreae COMP 102 4: 15 Variables int x = 100; UI.println(“x is ”, x); x = x + 1; UI.println(“x is ” +x); 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)
16
© Xiaoying Gao, Peter Andreae COMP 102 4: 16 Assignment Statements double fahren = UI.askDouble("Farenheit:"); double celsius = (fahren - 32) * 5 / 9; Assignment Statement: where = what ; name-of-place = specification-of-value ; double celsius = (fahren - 32) * 5 / 9; Compute the value and put it in the place 〈 variable 〉〈 expression 〉 =;
17
© Xiaoying Gao, Peter Andreae COMP 102 4: 17 Expressions /** Convert from fahrenheit to celsius */ public void fahrenToCelsius(){ double fahren = UI.askDouble("Fahrenheit:" ); double celsius= (fahren - 32) * 5 / 9; UI.println(“ -> " + celsius + “ C"); } Expressions describe how to compute a value. Expressions are constructed from values variables operators (+, -, *, /, %,etc) %: remainder +: also for string concatenation method calls that return a value sub-expressions, using (… ) … + for Strings: "concatenates" the Strings
18
© Xiaoying Gao, Peter Andreae COMP 102 4: 18 A class can have constants Inside a class, outside a method import ecs100.*; public class Numbers{ public static final double minN = 0.0; public static final double maxN = 50.0; public void printNumbers() { double x = 11; x = x/2; UI.println(x); x = (x-minN)/(maxN-minN); UI.println("x is now " + x); }
19
© Xiaoying Gao, Peter Andreae COMP 102 4: 19 int vs double import ecs100.*; public class Numbers{ public static final int minN = 0; public static final int maxN = 50; public void printNumbers() { int x = 11; x = x/2; UI.println(x); x = (x-minN)/(maxN-minN); UI.println("x is now " + x); }
20
© Xiaoying Gao, Peter Andreae COMP 102 4: 20 Signs of Computer Distress stiffness soreness discomfort weakness tightness heavy tingling numbness aching throbbing burning hot Sore ImmobilityNo blood flow No oxygen to tissue Stop moving Staying still makes muscle fatigue worse. Get moving!
21
© Xiaoying Gao, Peter Andreae COMP 102 4: 21 Myths False OOS/muscle fatigue is a crippling injury It will affect the rest of your life Once you have it, you will never get bett Muscle fatigue/OOS is not an injury: it is a lack of blood supply to muscles Get up and get moving! Seek professional advice True When people rested, pain got worse Immobilising sore areas makes pain worse Physical exercise works!
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.