Lecture 5

23
Lecture 5

description

Lecture 5. Review (Attributes and Methods). Class will contain a set of attributes and methods. public class Turtle { ///// Field (Attributes) ///// … /////// Method /////// … }. Review (Variables). A variable is a symbol representing a value. public class Turtle { - PowerPoint PPT Presentation

Transcript of Lecture 5

Page 1: Lecture 5

Lecture 5

Page 2: Lecture 5

Review (Attributes and Methods)

• Class will contain– a set of attributes and methods

public class Turtle{

///// Field (Attributes) ///// …

/////// Method /////// …

}

Page 3: Lecture 5

Review (Variables)• A variable is a symbol representing a value

public class Turtle{

///// Field (Attributes) ///// int length; int position = 100; double size = 2.5;

///// Method ///// …

}

Page 4: Lecture 5

Review (variable declaration)

• What does this line mean???

• We declare a variable, named length

• The type of value should be int

• Then, initialize the variable with 15

int length = 15;

data type variable name

length

15value

Page 5: Lecture 5

Review (arguments/parameters)• A argument is a variable that will be entered in

a method as input valuepublic class Turtle{

/////// Method /////// public void drawLine(int len)

{ penDown(); forward(len);

}}

Page 6: Lecture 5

Today’s topic

• More about methods Methods in a method (sub-method) Main method

• Arithmetic expression + , -, *, /, %

Page 7: Lecture 5

Methods in a method (“sub-methods”)• You can use (call) methods in a method

as sub-methods public class Turtle {

public void drawLine(int len){forward(len);}

public void drawSquare(int len){drawLine(len);turnRight();}

}

Page 8: Lecture 5

Practice• You already have two method

– drawTriangle, drawRectangle

• drawTriangle receives 1 parameter

drawTriangle(int len)

• drawRectangle receives 2 parameters

drawRectangle(int width, int height)

Page 9: Lecture 5

Practice (drawSimpleHouse)

Please write another method– Name: drawSimpleHouse– Receive 2 parameters, width and height– Use 2 methods in the previous slide

You can also use

penUp();

penDwon();

moveTo( x, y );

x

y

Page 10: Lecture 5

Practice (sample code)public void drawSimpleHouseLine(int w, int h){penUp();moveTo(200, 200);penDown();drawRectangle(w, h);drawTriangle(w);}

x

yFor example, you may type

t.drawSimpleHouse(150, 100);

Page 11: Lecture 5

Main method

public class Test{

public static void main(String[] args){

}

}

• Look at Test.java

Page 12: Lecture 5

Source code

Java compiler Java bytecode

Java interpreter Bytecodecomplier

Machine code

.java file

.class file

Execute!

Execute program from main method

We can test(in Interaction panel)

Page 13: Lecture 5

Main method

public class Test {

public static void main(String[] args){World w = new World();Turtle t = new Turtle(w);t.drawSimpleHouse();}

}

• Let’s write main method and RUN it !!!

Page 14: Lecture 5

How to execute a program?

• When a program is executed, it always starts from a main method

Right click onthe file containinga main method

Page 15: Lecture 5

• Arithmetic Expression!!!

How we can do Math in Java program ???

Page 16: Lecture 5

Arithmetic expression

• We can calculate arithmetic expressione.g., 2 + 3, 5 – 3, 4 * 5, 6 / 2

• An expression is a combination of one or more operators and operands

Addition +Subtraction -Multiplication *Division /Remainder %

5 + 3

Page 17: Lecture 5

Assignment

• The result of expression is assigned toa variable by using = symbol

x = 5 + 3;int x; Variable declaration

Assignment

Note: Somewhat different from math

Variable x contains a value of 8

Page 18: Lecture 5

Assignment (more)

• Variables can be operands• Variables may be re-assigned

x = 5 + 3;

int x; Variable declaration

Assignment

int y; Variable declaration

y = x – 4; Assignment

x = x / y; Assignment

Page 19: Lecture 5

Printout Method

• We can print out the variable value to a screen by calling a method

System.out.println( variable );

Page 20: Lecture 5

Practice (Printout Method) public class Test {

public static void main(String[] args){int x;int y;x = 145 + 25;y = 291 / 3;System.out.println( x );System.out.println( y );}

}Where the results appear ???

Page 21: Lecture 5

Exercise 1By running Test class, (i.e. calling a main method)

Calculate your age in minutesand show the result to a screen

use following conversions (assumptions)

1 year = 365 days1 day = 24 hours1 hour = 60 minutes

Page 22: Lecture 5

Exercise 1 (sample code)

public class Test {

public static void main(String[] args) { int minutes;

minutes = 17 * 365 * 24 * 60;

System.out.println( munutes ); }

}

Page 23: Lecture 5

How about this? (Useful!!!)

public class Test {

public static void main(String[] args) {

int age = 17; int minutes;

minutes = age * 365 * 24 * 60;

System.out.println( munutes ); }

}