Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lec 10 Using Objects II.

Similar presentations


Presentation on theme: "Lec 10 Using Objects II."— Presentation transcript:

1 Lec 10 Using Objects II

2 Agenda Review objects and classes Using static variables of a class
Demo the Island class moving on an island declaring several islands TreasureHunter game design

3 Review: Classes and Objects

4 Object (reference) variables

5

6 Memory Maps – Primitives vs Objects
int x = 24 double y = 34.4 Point p; p= new Point(14,35); Point q = new Point(-10,29); (p contains "null")

7 Notice Field Summary in Point Class
both x and y are declared int: means they can change. These are called "instance variables" Point q = new Point(-10,29); // construct a pt q.translate(20,30); // move a pt

8 Access Restricted! Not allowed direct access to fields x and y
Point q = new Point(-10,29); // construct a pt q.x = 5; // illegal attempt to access x System.out.println(q.x); // illegal too. Users must use method: q.translate(20,30); // move a pt q.setX( 5 ); // what we tried above System.out.println( q.getX() ); // print out pt

9 Unless the fields are declared "static"
Check out Math class: PI and E Area = Math.PI * r * r; // area of circle radius r z = Math.pow( Math.E, 4 ); // e raised to 4th pwr static fields represent unchanging values in a class refer to them by "Class Name" <dot> Field Name just like static methods of a Class tied to Class not instances of the Class

10 More Static Fields Color class holds three values R,G,B
Note the static fields for different Color names We can declare a color and initialize Color c = new Color(0,0,255); We can check if a color is "Blue" if ( c.equals(Color.BLUE) { // and so on...

11 Today's Lab: Island Class
Island class is a world of fun Create an island Island desert = new Island(5); // 5x5 playing grid Check location: ...println("I'm at: " + desert.currentLocation() );

12 "I'm at 0" ??? Not very helpful. Try:
if ( desert.currentLocation() == Island.PIRATE){ System.out.println( "location is: Pirate! "); } Note the use of static variables for readability

13 TreatureHunt.java make Lab10 folder, TreasureHunt class
Download Island.class into Lab10 folder Read problem statement, Have a design discussion with a partner


Download ppt "Lec 10 Using Objects II."

Similar presentations


Ads by Google