Download presentation
Presentation is loading. Please wait.
1
Lecture 5
2
Review (Attributes and Methods) Class will contain –a set of attributes and methods public class Turtle { ///// Field (Attributes) ///// … /////// Method /////// … }
3
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 ///// … }
4
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 15 value
5
Review (arguments/parameters) A argument is a variable that will be entered in a method as input value public class Turtle { … /////// Method /////// public void drawLine(int len) { penDown(); forward(len); } }
6
Today’s topic More about methods Methods in a method (sub-method) Main method Arithmetic expression +, -, *, /, %
7
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(); } }
8
Practice You already have two method –drawTriangle, drawRectangle drawTriangle receives 1 parameter drawTriangle(int len) drawRectangle receives 2 parameters drawRectangle(int width, int height)
9
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
10
Practice (sample code) public void drawSimpleHouseLine(int w, int h) { penUp(); moveTo(200, 200); penDown(); drawRectangle(w, h); drawTriangle(w); } x y For example, you may type t.drawSimpleHouse(150, 100);
11
Main method public class Test { public static void main(String[] args) { } Look at Test.java
12
Source code Java compiler Java bytecode Java interpreter Bytecode complier Machine code.java file.class file Execute! Execute program from main method We can test (in Interaction panel)
13
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 !!!
14
How to execute a program? When a program is executed, it always starts from a main method Right click on the file containing a main method
15
Arithmetic Expression!!! How we can do Math in Java program ???
16
Arithmetic expression We can calculate arithmetic expression e.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
17
Assignment The result of expression is assigned to a variable by using = symbol x = 5 + 3; int x; Variable declaration Assignment Note: Somewhat different from math Variable x contains a value of 8
18
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
19
Printout Method We can print out the variable value to a screen by calling a method System.out.println( variable );
20
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 ???
21
Exercise 1 By running Test class, (i.e. calling a main method) Calculate your age in minutes and show the result to a screen use following conversions (assumptions) 1 year = 365 days 1 day = 24 hours 1 hour = 60 minutes
22
Exercise 1 (sample code) public class Test { public static void main(String[] args) { int minutes; minutes = 17 * 365 * 24 * 60; System.out.println( munutes ); }
23
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 ); }
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.