Presentation is loading. Please wait.

Presentation is loading. Please wait.

© Xiaoying Gao, Peter Andreae Class, method, statements COMP 102 #3 2014T2 Xiaoying Sharon Gao Computer Science Victoria University of Wellington.

Similar presentations


Presentation on theme: "© Xiaoying Gao, Peter Andreae Class, method, statements COMP 102 #3 2014T2 Xiaoying Sharon Gao Computer Science Victoria University of Wellington."— Presentation transcript:

1 © Xiaoying Gao, Peter Andreae Class, method, statements COMP 102 #3 2014T2 Xiaoying Sharon Gao Computer Science Victoria University of Wellington

2 © 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

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

4 © 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

5 © 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

6 © 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

7 © Xiaoying Gao, Peter Andreae Writing the Java code import 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

8 © 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

9 © 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

10 © 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;

11 © 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 …

12 © 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 needs Maybe empty

13 © 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 number eg: tell the UI object to print out a string eg: tell the UI object to draw an oval assignment statement compute some value and put it in a place in memory.

14 © 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 〉.();

15 © Xiaoying Gao, Peter Andreae COMP 102 4: 15 Objects and their methods in Java What objects are there? Predefined 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

16 © 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 …

17 © 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)

18 © 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 〉 =;

19 © Xiaoying Gao, Peter Andreae COMP 102 4: 19 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) method calls that return a value sub-expressions, using (… ) … + for Strings: "concatenates" the Strings

20 © Xiaoying Gao, Peter Andreae COMP 102 4: 20 Declarations and Assignments Variable must be declared before you use it Must be declared only once in a method (strictly, once in a block) Must be assigned a value before it is used in an expression Can declare variable before first assignment or Declare variable in the first assignment statement: /** Print out the conversion formulas (version 2) */ public void printFormula () { String formula = " Celsius = (Fahrenheit - 32) *5/9" ; UI.println( formula ); } "

21 © Xiaoying Gao, Peter Andreae COMP 102 4: 21 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) Method Calls: a (good) metaphor public void fahrenToCelsius(){ double fahren = UI.askDouble("Fahrenheit:"); double celsius = (fahren – 32) * 5 / 9; UI.println(“ -> " + celsius + “ C"); } 86 0 30 0

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

23 © Xiaoying Gao, Peter Andreae COMP 102 4: 23 More details: Expressions L-DP-C 2.3 Literal values numbers:9, 5, 76.0034 Strings:“ F ->”, "centigrade = (fahrenheit - 32) *5/9“ … Variables: cent Numerical expressions using arithmetic operators: +, -, *, /, (, ),6 + cent * 45 ordinary arithmetic (except “/” : truncates integers) % “remainder” String expressions using concatenate operator: + Concatenate values into a single String (converts numbers etc into strings)

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

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

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

27 © Xiaoying Gao, Peter Andreae COMP 102 4: 27 More Details: Expressions Creating a new object: new TemperatureConverter() Methods that return a value... (we will see more soon…) Expressions out of sub expressions (3 * size) + (weight / 2.5)


Download ppt "© Xiaoying Gao, Peter Andreae Class, method, statements COMP 102 #3 2014T2 Xiaoying Sharon Gao Computer Science Victoria University of Wellington."

Similar presentations


Ads by Google