© xiaoying gao, peter andreae class, method, statements comp 102 #3 2014t2 xiaoying sharon gao...
TRANSCRIPT
© Xiaoying Gao, Peter Andreae
Class, method, statements
COMP 102 #3 2014T2
Xiaoying Sharon GaoComputer Science
Victoria University of Wellington
© Xiaoying Gao, Peter Andreae
COMP 102 3:2
Menu• More examples and a slightly big program• Semantics (meaning) and Syntax (grammar rules)• Class, Methods, Statements• Variables, Types, Expressions
Announcements:• Assignment 1 is due next Wednesday 11am.
• Use lab computer, BlueJ, submit assignments • Go to the labs even you have not signed up.
• Lab A: Thursday 3-4, 4-5, Fri 12-1• Lab B: Monday 4-5, Tuesday 12-1, 2-3, Wed 9-10
• Optional tutorial• Friday 9-10, CO219 with Tim, starting week 3
© Xiaoying Gao, Peter Andreae
Example 2
/** An example for simple string Inout/Output. The way to generate loginName is much more simplified */ import ecs100.*;public class Example2{ public void printMessage(){ String course = "COMP102"; String name = UI.askString("type your first name: "); String family = UI.askString("your family name? "); UI.println("Hello "+ name + ", Welcome to " + course); String loginName = family.substring(0,2) + name; UI.println("your Login name is " + loginName); } }
COMP 102 3:3
© Xiaoying Gao, Peter Andreae
DrawLabel
import ecs100.*;public class DrawLabel {
public void drawSimple() { double x = 100; double y = x + 200; double w = 223; double h = w/2; UI.drawRect(100,200,50,50); UI.drawRect(x, y, w, h); }}
COMP 102 3:4
© Xiaoying Gao, Peter Andreae
COMP 102 3:5
A Java Program
Task: Write a temperature conversion program: C ⇔ F
1: Specification: what is it supposed to do?
• Write a program that will let the user do three things:• print out the conversion formula• let user enter temperature in Fahrenheit, and print out in
Celsius.• let user enter temperature in Celsius, and print out in
Fahrenheit
2: Design:• For calculate actions:
• Ask user for the value to be converted• Calculate new values out of old value (F-32)*5/9 or
C*9/5+32• Print out the answer
© Xiaoying Gao, Peter Andreae
COMP 102 3:6
Designing the Java program
3: Editing
• Need to write this design in the Java language.
• We need an object (a "temperature calculator") to do
the actions
⇒ we need a class to describe the object
• The object needs three actions
⇒ define three methods
⇒ specify what the methods will do
© Xiaoying Gao, Peter Andreae
Writing the Java codeimport ecs100.*;/** Program for converting between temperature scales */public class TemperatureCalculator{
/** Print conversion formula */public void printFormula ( ) {
UI.println("Celsius = (Fahrenheit - 32) *5/9");}/** Convert from fahrenheit to centigrade */public void fahrenToCelsius(){
double fahren = UI.askDouble("Farenheit:");double celsius = (fahren - 32) * 5 / 9;UI.println(" -> " + celsius + " C");
}/** Convert from centigrade to fahrenheit */public void celsiusToFahren(){
double celsius = UI.askDouble("Celsius:");double fahren = celsius * 9 / 5 + 32;UI.println(" -> " + fahren + " F");
}}
COMP 102 3:7
© Xiaoying Gao, Peter Andreae
Compiling and Running
4: Compiling• If there are syntax errors (invalid Java)
then the compiler will complain and list all the errors⇒ read the error message to work out what's wrong⇒ fixing syntax errors until it compiles without complaint
• BlueJ makes this process easier
5: Running and Testing• Must run the program and test it on lots of different
input.
• BlueJ makes it easy to run individual methods.
COMP 102 3:8
© Xiaoying Gao, Peter Andreae
COMP 102 4: 9
Writing your own programs
How?
• Use other programs as models• Very useful strategy• Limiting• Hard to work out what's wrong
• Understand the language – rules and vocabulary• Unlimited• Able to understand and reason about your program
© Xiaoying Gao, Peter Andreae
COMP 102 4: 10
Class syntax pattern
〈 import statements〉
public class
〈method descriptions〉
{
}
〈 classname〉
Comments can be added anywhere
import comp102.*;
import java.awt.Color;
© Xiaoying Gao, Peter Andreae
COMP 102 4: 11
Comments
Three kinds of comments:
• Documentation comments
eg /** Program for converting between temperature scales */
• end-of-line comments
eg double fahren = celsius * 9 / 5 + 32; // compute answer
• anywhere comments
eg /* double fahren = celsius * 9 / 5 + 32; UI.println(" -> " + fahren + " F"); */
/** 〈 text of comment〉 */
// 〈 text of comment〉
Top of class, Before each method
at end of any line
/* 〈 text of comment〉 */
multi-line, or middle of line, or …
© Xiaoying Gao, Peter Andreae
COMP 102 4: 12
Method Definitions
/** Print out the conversion formulas */
public void printFormula ( ) {
UI.println("Celsius = (Fahrenheit - 32) *5/9");
}
〈 Comment〉 〈 Header〉 〈 Body〉{ }
public void ( )〈 name〉
instructions to perform this action
Specifying the information the action needsMaybe empty
© Xiaoying Gao, Peter Andreae
COMP 102 4: 13
“Statements” (instructions)
(Single instructions are called “statements” for silly historical reasons!)
Two important kinds of statements:
• method call statement:• tell some object to perform one of its methods.
eg: tell the UI object to ask the user for a numbereg: tell the UI object to print out a stringeg: tell the UI object to draw an oval
• assignment statement• compute some value and put it in a place in memory.
© Xiaoying Gao, Peter Andreae
COMP 102 4: 14
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〉. ( ) ;
© Xiaoying Gao, Peter Andreae
COMP 102 4: 15Objects and their methods in Java• What objects are there?
Predefined• UI a "User Interface" window with
several panes println(….), askString(…), drawRect(…), clearGraphics()
• System representing the computer system
currentTimeMillis()
• Math methods 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
© Xiaoying Gao, Peter Andreae
COMP 102 4: 16
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 -> "
• Objects
• …
© Xiaoying Gao, Peter Andreae
COMP 102 4: 17
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)
© Xiaoying Gao, Peter Andreae
COMP 102 4: 18
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〉= ;