1 / 17

Lecture 4 Review: Attributes and Methods

This lecture covers the concept of attributes and methods in Java programming, including declaring variables, data types, and using arguments in methods. Examples are provided to illustrate these concepts.

kroeker
Download Presentation

Lecture 4 Review: Attributes and Methods

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 4

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

  3. me 15 Review (Attributes and Methods) • ExampleTurtle.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() { } } … …

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

  5. Today • Variables • Let’s write a method with arguments

  6. 5 28 2 13 2 13 What is a variable • A variableis 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 X Y Replaced!

  7. data type variable name Variables • In Java program • A variable must be declared before using it • We need to declare a variable by specifying the variable's nameand thedata type that it will hold int int length; length 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. data type variable name value 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; 15 length

  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 squarewith the size of 125 (pixels)

  12. In order to write different size of squarewe 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 • 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, wherex 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…

More Related