Lecture 5
description
Transcript of Lecture 5
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{
///// Field (Attributes) ///// int length; int position = 100; double size = 2.5;
///// Method ///// …
}
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
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);
}}
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 method
as 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
You can also use
penUp();
penDwon();
moveTo( x, y );
x
y
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);
Main method
public class Test{
public static void main(String[] args){
}
}
• Look at Test.java
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)
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 !!!
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 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
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
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
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 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
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 ); }
}