1 / 23

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 {

beata
Download Presentation

Lecture 5

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  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 variableis a symbol representing a value public class Turtle { ///// Field (Attributes) ///// int length; int position = 100; double size = 2.5; ///// Method ///// … }

  4. 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

  5. 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); } }

  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 methodas 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 x You can also use penUp(); penDwon(); moveTo( x, y ); 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 • Look at Test.java public class Test { public static void main(String[] args) { } }

  12. 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!

  13. 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(); } }

  14. How to execute a program? • When a program is executed, it always starts from a main method Right click onthe file containinga 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 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

  18. 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

  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 ); } }

More Related