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

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

Embed Size (px)

Citation preview

More on a First Java Program COMP 102 #4 17 July 2006

UI methods, Variables, ConstantsCOMP 102 #4 2015T2

Xiaoying Sharon GaoComputer ScienceVictoria University of Wellington Xiaoying Gao, Peter Andreae1AdminGood news: Additional help for all 1st year studentHelpdesk Monday, Wednesday and Friday 11am-12 in CO242B Night tutorials Monday 5pm-7pm in AM103Wednesday 5pm-7pm in CO118

Our own COMP102 helpdesk and tutorial starts next weekClass repCandidate: Christopher ParryCOMP 102 1:2MenuUI methodsGraphical outputVariables, Data types,Expressions, Assignment statementConstants

Reading:Text book: chapter 2

COMP 102 1:3Summary of Java program structureA Class specifies a type of objectTemperatureCalculator class describes TemperatureCalculator objects

A Class contains a collection of methodseach method is an action the objects can perform.TemperatureCalculator objects can do celsiusToFahren, fahrenToCelsius, printFormulaIf you have an object, you can call its methods on it.A method definition specifies how to perform the actionA method contains statementseach statement specifies one step of performing the actionDeclaration and Assignment statementsMethod call statements Xiaoying Gao, Peter AndreaeCOMP 102 4: #Method Definition: Like a pad of worksheetspublic 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 Xiaoying Gao, Peter AndreaeCOMP 102 4: #5What 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 fortext input from the usereg UI.askString("What is your name?")text outputeg UI.println(" * " + name + " * ";graphical outputeg UI.drawRect(100, 100, 300, 150)making buttons, sliders, etcHow do you find out about all the methods?How do your find out what arguments you need to provide? Xiaoying Gao, Peter AndreaeCOMP 102 4: #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 barhttp://ecs.victoria.ac.nz/Courses/COMP102_2014T2/JavaResources

Tailored for Comp 102Includes 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! Xiaoying Gao, Peter AndreaeCOMP 102 4: #Some UI methods: read the documentationText: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 ) Xiaoying Gao, Peter AndreaeCOMP 102 4: #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 providedobjectmethodnamearguments.(); Xiaoying Gao, Peter AndreaeCOMP 102 4: #Objects and their methods in JavaWhat objects are there?Predefined: find them in the java documentationUIa "User Interface" window with several panes println(.), askString(), drawRect(), clearGraphics() Systemrepresenting the computer system currentTimeMillis()

Mathmethods for mathematical calculations random(), sin()

OthersThe object(s) defined by your programNew objects that your program creates Some method calls return a value Xiaoying Gao, Peter AndreaeCOMP 102 4: #Draw some shapes with colors Xiaoying Gao, Peter AndreaeCOMP 102 4: #Version 1import 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); }} Xiaoying Gao, Peter AndreaeCOMP 102 4: #Version 2import 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); }} Xiaoying Gao, Peter AndreaeCOMP 102 4: #Data Types text book 2.3A type is a kind/category of valueEvery variable must have a typeJava has:Primitive types: double-3.495, 6.482E23int-2358, 45, (integers from -2 billion to 2 billion)longintegers from -263 to 263charD, d, =booleantrue, falsebyte, short, float: (see the book for details)Object types:StringHello John, 600.45, F, true ScannerPrintStreamColor. (lots of predefined types)TemperatureConverter. Every class defines a type Xiaoying Gao, Peter AndreaeCOMP 102 4: #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 variableCan use the value by specifying the variables name Can change the value in a variable (unlike mathematical variable)

Xiaoying Gao, Peter AndreaeCOMP 102 4: #Assignment Statementsdouble 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 placevariableexpression=; Xiaoying Gao, Peter AndreaeCOMP 102 4: #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 valuesvariablesoperators (+, -, *, /, %,etc) %: remainder +: also for string concatenationmethod calls that return a valuesub-expressions, using ( ) + for Strings: "concatenates" the Strings Xiaoying Gao, Peter AndreaeCOMP 102 4: #A class can have constantsInside 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); }}

Xiaoying Gao, Peter AndreaeCOMP 102 4: #int vs doubleimport 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); }} Xiaoying Gao, Peter AndreaeCOMP 102 4: #Signs of Computer Distress stiffnesssorenessdiscomfortweaknesstightnessheavy

tinglingnumbnessachingthrobbingburninghotSoreImmobilityNo blood flowNo oxygen to tissueStop movingStaying still makes muscle fatigue worse. Get moving! Xiaoying Gao, Peter AndreaeCOMP 102 4: #Myths FalseOOS/muscle fatigue is a crippling injuryIt will affect the rest of your lifeOnce you have it, you will never get bettMuscle fatigue/OOS is not an injury: it is a lack of blood supply to musclesGet up and get moving!Seek professional adviceTrueWhen people rested, pain got worseImmobilising sore areas makes pain worsePhysical exercise works! Xiaoying Gao, Peter AndreaeCOMP 102 4: #