230 likes | 393 Views
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 {
E N D
Review (Attributes and Methods) • Class will contain • a set of attributes and methods public class Turtle { ///// Field (Attributes) ///// … /////// Method /////// … }
Review (Variables) • A variableis a symbol representing a value public class Turtle { ///// Field (Attributes) ///// int length; int position = 100; double size = 2.5; ///// Method ///// … }
data type variable name value 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; 15 length
Review (arguments/parameters) • A argumentis a variable that will be entered in a method as input value public class Turtle { … /////// Method /////// public void drawLine(int len) { penDown(); forward(len); } }
Today’s topic • More about methods • Methods in a method (sub-method) • Main method • Arithmetic expression • + , -, *, /, %
Methods in a method (“sub-methods”) • You can use (call) methods in a methodas sub-methods public class Turtle { public void drawLine(int len) { forward(len); } public void drawSquare(int len) { drawLine(len); turnRight(); } }
Practice • You already have two method • drawTriangle, drawRectangle • drawTriangle receives 1 parameter drawTriangle(int len) • drawRectangle receives 2 parameters drawRectangle(int width, int height)
Practice (drawSimpleHouse) Please write another method • Name: drawSimpleHouse • Receive 2 parameters, width and height • Use 2 methods in the previous slide x You can also use penUp(); penDwon(); moveTo( x, y ); y
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);
Main method • Look at Test.java public class Test { public static void main(String[] args) { } }
Java bytecode Java compiler Java interpreter Bytecodecomplier Machine code Execute program from main method .java file Source code .class file We can test(in Interaction panel) Execute!
Main method • Let’s write main method and RUN it !!! public class Test { public static void main(String[] args) { World w = new World(); Turtle t = new Turtle(w); t.drawSimpleHouse(); } }
How to execute a program? • When a program is executed, it always starts from a main method Right click onthe file containinga main method
Arithmetic Expression!!! How we can do Math in Java program ???
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
Assignment • The result of expression is assigned toa variable by using = symbol int x; Variable declaration x = 5 + 3; Assignment Variable x contains a value of 8 Note: Somewhat different from math
Assignment (more) • Variables can be operands • Variables may be re-assigned int x; Variable declaration int y; Variable declaration x = 5 + 3; Assignment y = x – 4; Assignment x = x / y; Assignment
Printout Method • We can print out the variable value to a screen by calling a method System.out.println( variable );
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 ???
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
Exercise 1 (sample code) public class Test { public static void main(String[] args) { int minutes; minutes = 17 * 365 * 24 * 60; System.out.println( munutes ); } }
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 ); } }