Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture 4. Review (Attributes and Methods) Class will contain –a set of attributes and methods public class Turtle { ///// Field (Attributes) ///// …

Similar presentations


Presentation on theme: "Lecture 4. Review (Attributes and Methods) Class will contain –a set of attributes and methods public class Turtle { ///// Field (Attributes) ///// …"— Presentation transcript:

1 Lecture 4

2 Review (Attributes and Methods) Class will contain –a set of attributes and methods public class Turtle { ///// Field (Attributes) ///// … /////// Method /////// … }

3 Review (Attributes and Methods) Example Turtle.java public class Turtle { ///// Field (Attributes) ///// private String name = “me”; private int height = 15; private Pen pen = new Pen(); /////// Method /////// public void forwrad() { } public void turnRight() { } } … … me 15

4 Source code Java compiler Java bytecode Java interpreter Bytecode complier Machine code.java file.class file Execute! Review (compile) We can test (in Interaction panel)

5 Today – Variables – Let’s write a method with arguments

6 What is a variable A variable is a name for a location It holds a data value In mathematics, X and Y are variable Y = 5X + 3 when X=2, Y= 13 when X=5, Y= 28 XY 213 2 528 Replaced!

7 Variables In Java program –A variable must be declared before using it –We need to declare a variable by specifying the variable's name and the data type that it will hold int length; data typevariable name length int What is data type???

8 Data type Data type is a type of value that variable holds For example int  integers positive and negative numbers e.g. 3, 21, -18, 0, etc … double  decimal numbers positive and negative numbers with floating point e.g. 2.5, 3.0, -9.2, 0.0, etc … Note: There are 8 other primitive types in Java (we will come back this topic later)

9 Therefore … 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 15 value

10 Example of drawSquare() public void drawSquare() { int length = 100; forward(50); turnRight(); forward(50); turnRight(); forward(50); turnRight(); forward(50); turnRight(); } Let’s declare a variable!!! Name: length Type: int Value: 100

11 Example of drawSquare() public void drawSquare() { int length = 100; forward(length); turnRight(); forward(length); turnRight(); forward(length); turnRight(); forward(length); turnRight(); } Once a variable is declared, we can use the variable Efficient to modify source code !!! Exercise: Draw a square with the size of 125 (pixels)

12 In order to write different size of square we have to modify source code and compile it again and again and again… Any more efficient way … ?

13 Arguments What is arguments? A method can receive values as input variables –They are called arguments (or parameters) For example ( do you remember this method? ) forward( 100 ); turn( 31.5 ); …

14 Example of drawSquare() public void drawSquare(int length) { forward(length); turnRight(); forward(length); turnRight(); forward(length); turnRight(); forward(length); turnRight(); } We can declare a variable inside parenthesis Method will receive the value for length whenever it is called For example t.drawSquare(25);

15 Method with more arguments A method can receive one or more arguments public void drawRectangle(int base, int height) { forward( height ); turnRight(); forward( base ); turnRight(); forward( height ); turnRight(); forward( base ); turnRight(); }

16 Exercise 1)Write a method, named drawTriangle which will draw a triangle with the user-defined size hint: you will call t.drawTriangle( 50 ); 2) Write a method to draw a square at the position of (x, y) in a window, where x and y are provided as input variables hint: you will call t.drawSquare(250, 140);

17 Challenge!!! 3) Write a method, named drawLine which will draw a line with the user-defined length toward the user-defined direction hint: you will call t.drawLine( 25, 45 ); t.drawLine( 50, 200 ); t.drawLine( 30, 100 ); etc…


Download ppt "Lecture 4. Review (Attributes and Methods) Class will contain –a set of attributes and methods public class Turtle { ///// Field (Attributes) ///// …"

Similar presentations


Ads by Google