Presentation is loading. Please wait.

Presentation is loading. Please wait.

© Xiaoying Gao, Peter Andreae Variables, Constants, UI methods COMP 102 #4 2014T2 Xiaoying Sharon Gao Computer Science Victoria University of Wellington.

Similar presentations


Presentation on theme: "© Xiaoying Gao, Peter Andreae Variables, Constants, UI methods COMP 102 #4 2014T2 Xiaoying Sharon Gao Computer Science Victoria University of Wellington."— Presentation transcript:

1 © Xiaoying Gao, Peter Andreae Variables, Constants, UI methods COMP 102 #4 2014T2 Xiaoying Sharon Gao Computer Science Victoria University of Wellington

2 Menu Variables, data types Expressions, assignment statement Constants UI methods Graphical output Admin Class Rep Labs with low numbers: Optional tutorial Optional helpdesk COMP 102 1:2

3 © Xiaoying Gao, Peter Andreae COMP 102 4: 3 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

4 © Xiaoying Gao, Peter Andreae COMP 102 4: 4 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

5 © Xiaoying Gao, Peter Andreae COMP 102 4: 5 Data types There are lots of different kinds of values: Numbers Integers( int or long) 42 -194573203 real numbers ( double or float ) 16.43 6.626e-34 … Characters ( char )'X' '4' Text ( String ) " F -> " yes or no (boolean) true, false Objects …

6 © Xiaoying Gao, Peter Andreae COMP 102 4: 6 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)

7 © Xiaoying Gao, Peter Andreae COMP 102 4: 7 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 〉 =;

8 © Xiaoying Gao, Peter Andreae COMP 102 4: 8 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

9 © Xiaoying Gao, Peter Andreae COMP 102 4: 9 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); }

10 © Xiaoying Gao, Peter Andreae COMP 102 4: 10 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); }

11 © Xiaoying Gao, Peter Andreae COMP 102 4: 11 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?

12 © Xiaoying Gao, Peter Andreae COMP 102 4: 12 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!

13 © Xiaoying Gao, Peter Andreae COMP 102 4: 13 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 )

14 © Xiaoying Gao, Peter Andreae COMP 102 4: 14 Draw some shapes with colors

15 © Xiaoying Gao, Peter Andreae COMP 102 4: 15 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); }

16 © Xiaoying Gao, Peter Andreae COMP 102 4: 16 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); }

17 © Xiaoying Gao, Peter Andreae COMP 102 4: 17 More Details: Types L-DP-C 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 …. (lots of predefined types) TemperatureConverter…. Every class defines a type

18 © Xiaoying Gao, Peter Andreae COMP 102 4: 18 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!

19 © Xiaoying Gao, Peter Andreae COMP 102 4: 19 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!


Download ppt "© Xiaoying Gao, Peter Andreae Variables, Constants, UI methods COMP 102 #4 2014T2 Xiaoying Sharon Gao Computer Science Victoria University of Wellington."

Similar presentations


Ads by Google