+ All Categories
Home > Documents > Object state: fields

Object state: fields

Date post: 14-Jan-2016
Category:
Upload: jadzia
View: 27 times
Download: 0 times
Share this document with a friend
Description:
Object state: fields. Fields. field : A variable inside an object that represents part of its internal state. Each object will have its own copy of the data fields we declare. Declaring a field, general syntax: public ; or - PowerPoint PPT Presentation
Popular Tags:
40
1 Object state: fields
Transcript
Page 1: Object state: fields

1

Object state: fields

Page 2: Object state: fields

Fields

field: A variable inside an object that represents part of its internal state. Each object will have its own copy of the data fields we declare.

Declaring a field, general syntax:public <type> <name>;orpublic <type> <name> = <value>; (with initialization)

Example:public class Student { public String name; // each student object has a public double gpa; // name and gpa data field}

2

Page 3: Object state: fields

Point3D class: Version 1

public class Point3D { public int x; public int y;

public int z;}

Every object of type Point3D contains three integers.

Point3D objects (so far) do not contain any behavior.

Class declarations are saved in a file of the same name: Point3D.java

3

Page 4: Object state: fields

Accessing and modifying fields Accessing a data field, general syntax:

<variable name>.<field name>

Modifying a data field, general syntax:<variable name>.<field name> = <value>;

Example:System.out.println("the x-coord is " + p1.x); // access

p2.y = 13; // modify

4

Page 5: Object state: fields

Client code

The Point3D class is not an executable Java program. Why not? It does not contain a main method.

client program: Code that uses an object.

5

Page 6: Object state: fields

Client program: Version 1

public class PointMain { public static void main(String[] args) { // create two Point objects Point3D p1 = new Point3D(); p1.x = 5; p1.y = 2; p1.z = -5; Point3D p2 = new Point3D(); p2.x = 4; p2.y = 3; p2.z = 2;

// print each point System.out.println("p1 is (" + p1.x + ", " + p1.y + “, “ + p1.z + ")"); System.out.println("p2 is (" + p2.x + ", " + p2.y + “, “ + p2.z + ")");

// move p2 and then print it again p2.x += 2; p2.y += 4; p2.z + -10; System.out.println("p2 is (" + p2.x + ", " + p2.y + “, “ + p2.z + ")"); }}

Output:p1 is (5, 2, -5)p2 is (4, 3, 2)p2 is (6, 7, -8)

6

Page 7: Object state: fields

Exercise

Write a client program to produce the following output:

p1 is (7, 2, -5)

p1's distance from origin = 8.831760866327846

p2 is (4, 3, 2)

p2's distance from origin = 5.385164807134504

p1 is (18, 8, -2)

p2 is (5, 10, 20)

Recall the formula to compute the distance between points (x1, y1, z1) and (x2, y2, z2) is:

7 212

212

212 )( zzyyxx

Page 8: Object state: fields

Solution

public class PointProgram { public static void main(String[] args) { // create two Point objects Point3D p1 = new Point3D(); p1.x = 7; p1.y = 2; p1.z = -5; Point3D p2 = new Point3D(); p2.x = 4; p2.y = 3; p2.z = 2;

// print each point System.out.println("p1 is (" + p1.x + ", " + p1.y + “, “ + p1.z + ")"); double dist1 = Math.sqrt(p1.x * p1.x + p1.y * p1.y + p1.z * p1.z); System.out.println(“p1’s distance from origin = “ + dist1);

System.out.println("p2 is (" + p2.x + ", " + p2.y + “, “ + p2.z + ")"); double dist2 = Math.sqrt(p2.x * p2.x + p2.y * p2.y); System.out.println("p2's distance from origin = " + dist2);

// move points and then print again p1.x += 11; p1.y += 6; p1.z += 3; System.out.println("p1 is (" + p1.x + ", " + p1.y + “, “ + p1.z + ")"); p2.x += 1; p2.y += 7; p2.z += 22; System.out.println("p2 is (" + p2.x + ", " + p2.y + “, “ + p2.z + ")"); }}

8

Page 9: Object state: fields

9

Object initialization: constructors

Page 10: Object state: fields

Initializing objects

It is tedious to have to construct an object and assign values to all of its data fields manually.

Point3D p = new Point3D();p.x = 3;p.y = 8;p.z = 10; // tedious

We want something more like:

// better!Point3D p = new Point3D(3, 8, 10);

10

Page 11: Object state: fields

Constructor

constructor: A special method that initializes the state of new objects as they are created.

Constructor syntax:public <class name> (<parameter(s)>) { <statement(s)>;}

How does this differ from previous methods?

11

Page 12: Object state: fields

Point3D class: Version 2

public class Point3D { public int x; public int y; public int z;

public Point3D(int initialX, int initialY, int initialZ)

{ x = initialX; y = initialY; z = initialZ; }}

12

Page 13: Object state: fields

Client program: Version 2

public class PointMain2 { public static void main(String[] args) { // create two Point3D objects Point3D p1 = new Point3D(7, 2, -5); Point3D p2 = new Point3D(4, 3, 2);

// print each point System.out.println("p1 is (" + p1.x + ", " + p1.y + ")"); System.out.println("p2 is (" + p2.x + ", " + p2.y + ")");

// move p2 and then print it again p2.x += 2; p2.y += 4; p2.z += -2; System.out.println("p2 is (" + p2.x + ", " + p2.y + ")"); }}

Output:p1 is (7, 2, -5)p2 is (4, 3, 2)p2 is (6, 7, 0)

13

Page 14: Object state: fields

Exercise

Recall our client program that produces this output:p1 is (7, 2)

p1's distance from origin = 7.280109889280518

p2 is (4, 3)

p2's distance from origin = 5.0

p1 is (18, 8)

p2 is (5, 10)

distance from p1 to p2 = 13.152946437965905

Modify the program to use our new constructor.

14

Page 15: Object state: fields

Exercise

Recall the client program from the previous exercise that produced this output:

p1 is (7, 2, -5)

p1's distance from origin = 8.831760866327846

p2 is (4, 3, 2)

p2's distance from origin = 5.385164807134504

p1 is (18, 8, -2)

p2 is (5, 10, 20)

Modify the program to use our constructor

15

Page 16: Object state: fields

Solution

public class PointProgram { public static void main(String[] args) { // create two Point3D objects Point3D p1 = new Point3D(7, 2, -5); Point3D p2 = new Point3D(4, 3, 2);

// print each point System.out.println("p1 is (" + p1.x + ", " + p1.y + ")"); System.out.println("p1's distance from origin = " + p1.distanceFromOrigin());

System.out.println("p2 is (" + p2.x + ", " + p2.y + ")"); System.out.println("p2's distance from origin = " + p2.distanceFromOrigin());

// move points and then print again p1.translate(11, 6); System.out.println("p1 is (" + p1.x + ", " + p1.y + ")"); p2.translate(1, 7); System.out.println("p2 is (" + p2.x + ", " + p2.y + ")"); System.out.println("distance from p1 to p2 = " + p1.distance(p2)); }}

16

Page 17: Object state: fields

Solution

public class PointProgram { public static void main(String[] args) { // create two Point objects Point3D p1 = new Point3D(7, 2, -5); Point3D p2 = new Point3D(4, 3, 2);

// print each point System.out.println("p1 is (" + p1.x + ", " + p1.y + “, “ + p1.z + ")"); double dist1 = Math.sqrt(p1.x * p1.x + p1.y * p1.y + p1.z * p1.z); System.out.println(“p1’s distance from origin = “ + dist1);

System.out.println("p2 is (" + p2.x + ", " + p2.y + “, “ + p2.z + ")"); double dist2 = Math.sqrt(p2.x * p2.x + p2.y * p2.y); System.out.println("p2's distance from origin = " + dist2);

// move points and then print again p1.x += 11; p1.y += 6; p1.z += 3; System.out.println("p1 is (" + p1.x + ", " + p1.y + “, “ + p1.z + ")"); p2.x += 1; p2.y += 7; p2.z += 22; System.out.println("p2 is (" + p2.x + ", " + p2.y + “, “ + p2.z + ")"); }}

17

Page 18: Object state: fields

18

Object behavior: methods

Page 19: Object state: fields

19

The case for methods

How would we translate several points?

p1.x += 11;p1.y += 6;p1.z + 2;

p2.x += 2;p2.y += 4;p2.z += -2;

p3.x += 1;p3.y += 7;p3.z += 17;

What is unsettling about this code?

Page 20: Object state: fields

20

Attempt at eliminating redundancy Write a static method in the client code to translate points.

// Shifts the location of the given point.public static void translate(

Point p, int dx, int dy, int dz) { p.x += dx; p.y += dy; p.z += dz;}

Example:// move p2 translate(p2, 2, 4, 6);

Question: Why doesn't the method need to return the modified point?

Page 21: Object state: fields

21

Why is the static method solution bad? Every client code that wants to translate points

would have to write their own static translate method!

Also, the call syntax doesn't match the way we're used to interacting with objects:

translate(p2, 2, 4);

We want something more like:

p2.translate(2, 4);

Page 22: Object state: fields

22

Classes with behavior

The whole point of writing classes is to put related state and behavior together.

Point translation is closely related to the x/y/z data of the Point3D object, so it belongs in the Point3D class.

Page 23: Object state: fields

23

Instance methods

instance method: A method inside an object that operates on that object.

Declaring an object's method, general syntax:public <type> <name> (<parameter(s)>) {

<statement(s)>;

}

How does this differ from previous methods?

Page 24: Object state: fields

24

Instance methods

An object's instance methods can refer to its fields.

public void translate(int dx, int dy, int dz) {

x += dx;

y += dy;

z += dz;

}

How does the translate method know which x, y, z to modify?

Page 25: Object state: fields

25

The implicit parameter

Each instance method call happens on a particular object. Example: p1.translate(11, 6, 2);

The code for an instance method has an implied knowledge of what object it is operating on.

implicit parameter: The object on which an instance method is called.

Page 26: Object state: fields

26

Point3D object diagrams

Think of each Point3D object as having its own copy of the translate method, which operates on that object's state:

Point3D p1 = new Point3D(7, 2, -2);Point3D p2 = new Point3D(4, 3, 2);

p1:

p2:

public void translate(int dx, int dy, int dz){ ...}

y:x: 7 2 z: -2

public void translate(int dx, int dy, int dz){ ...}

y:x: 4 3 z: 2

Page 27: Object state: fields

18

27

Tracing instance method calls

What happens when the following calls are made?p1.translate(11, 6, 3);

p2.translate(1, 7, -10);

p1:

p2:

public void translate(int dx, int dy, int dz){ ...}

y:x: 7 2 z: -2

public void translate(int dx, int dy, int dz){ ...}

y:x: 4 3 z: 2

8 1

5 10 -8

Page 28: Object state: fields

Point3D class: Version 3

public class Point3D { public int x; public int y;

public Point3D(int initialX, int initialY, int initialZ) {

x = initialX; y = initialY; z = initialZ; }

// Changes the location of this Point3D object. public void translate(int dx, int dy, int dz){ x += dx; y += dy; z += dz; }}

28

Page 29: Object state: fields

29

Client program: Version 3

public class PointMain3 { public static void main(String[] args) { // create two Point3D objects Point3D p1 = new Point3D(5, 2, -2);

Point3D p2 = new Point3D(4, 3, 2);

// print each point System.out.println("p1 is (" + p1.x + ", " + p1.y + ")"); System.out.println("p2 is (" + p2.x + ", " + p2.y + ")");

// move p2 and then print it again p2.translate(2, 4, -2); System.out.println("p2 is (" + p2.x + ", " + p2.y + ")"); }}

Output:p1 is (5, 2, -2)p2 is (4, 3, 2)p2 is (6, 7, 0)

Page 30: Object state: fields

30

Exercises

Write an instance method named distanceFromOrigin that computes and returns the distance between the current Point3D object and the origin, (0, 0, 0).

Write an instance method named distance that accepts a Point3D as a parameter and computes the distance between it and the current Point3D.

Write an instance method named setLocation that accepts x, y, and z values as parameters and changes the Point3D's location to be those values.

Modify the client code to use these new methods as appropriate.

Page 31: Object state: fields

31

Solutions

public class Point3D { public int x; public int y; public int z; // Changes the location of this Point3D object. public void translate(int dx, int dy, int dz) { setLocation(x + dx, y + dy, z + dz); } // Returns the distance from this Point object to the origin public double distanceFromOrigin() { return Math.sqrt(x * x + y * y + z * z); } // Returns the distance from this Point3D object to the given point public double distance(Point3D other) { int dx = x - other.x; int dy = y - other.y; int dz = z – other.z; return Math.sqrt(dx * dx + dy * dy + dz * dz); } // Sets this Point3D object's location public void setLocation(int newX, int newY, int newZ) { x = newX; y = newY; z = newZ; }}

Page 32: Object state: fields

Exercise

Recall the client program from the previous exercise that produced this output:

p1 is (7, 2, -5)

p1's distance from origin = 8.831760866327846

p2 is (4, 3, 2)

p2's distance from origin = 5.385164807134504

p1 is (18, 8, -2)

p2 is (5, 10, 20)

Modify the program to use our new instance methods.

Also add the following output to the program:distance from p1 to p2 = 13.152946437965905

32

Page 33: Object state: fields

33

Solution

public class PointProgram { public static void main(String[] args) { // create two Point objects Point p1 = new Point(); p1.setLocation(7, 2); Point p2 = new Point(); p2.setLocation(4, 3);

// print each point System.out.println("p1 is (" + p1.x + ", " + p1.y + ")"); System.out.println("p1's distance from origin = " + p1.distanceFromOrigin());

System.out.println("p2 is (" + p2.x + ", " + p2.y + ")"); System.out.println("p2's distance from origin = " + p2.distanceFromOrigin());

// move points and then print again p1.translate(11, 6); System.out.println("p1 is (" + p1.x + ", " + p1.y + ")"); p2.translate(1, 7); System.out.println("p2 is (" + p2.x + ", " + p2.y + ")"); System.out.println("distance from p1 to p2 = " + p1.distance(p2)); }}

Page 34: Object state: fields

Solution

public class PointProgram { public static void main(String[] args) { // create two Point3D objects Point3D p1 = new Point3D(7, 2, -5); Point3D p2 = new Point3D(4, 3, 2);

// print each point System.out.println("p1 is (" + p1.x + ", " + p1.y + “, “ + p1.z + ")");

System.out.println(“p1’s distance from origin = “ + p1.distanceFromOrigin());

System.out.println("p2 is (" + p2.x + ", " + p2.y + “, “ + p2.z + ")"); System.out.println("p2's distance from origin = " + p2.distanceFromOrigin());

// move points and then print again p1.translate(11, 6, 3); System.out.println("p1 is (" + p1.x + ", " + p1.y + “, “ + p1.z + ")"); p2.translate(1, 7, 22); System.out.println("p2 is (" + p2.x + ", " + p2.y + “, “ + p2.z + ")");

System.out.println("distance from p1 to p2 = " + p1.distance(p2)); }}

34

Page 35: Object state: fields

35

toString or not toString

Page 36: Object state: fields

36

Latest version of client code

public class PointMain3 { public static void main(String[] args) { // create two Point objects Point p1 = new Point(7, 2, -5); Point p2 = new Point(4, 3, 2);

// print each point System.out.println("p1 is (" + p1.x + ", " + p1.y + ")"); System.out.println("p2 is (" + p2.x + ", " + p2.y + ")");

// move p2 and then print it again p2.translate(2, 4, -2); System.out.println("p2 is (" + p2.x + ", " + p2.y + ")"); }}

Any remaining redundancies?

Page 37: Object state: fields

37

Printing points

Instead of

System.out.println("p1 is (" + p1.x + ", " + p1.y + ")");

It would be nice to have something more like:

System.out.println("p1 is " + p1);

What does this line currently do? Does it even compile?

It will print: p1 is Point@9e8c34

Page 38: Object state: fields

38

toString

When an object is printed or concatenated with a String, Java calls the object's toString method.

System.out.println("p1 is " + p1);

is equivalent to:

System.out.println("p1 is " + p1.toString());

Note: Every class has a toString method.

Page 39: Object state: fields

39

toString

The default toString behavior is to return the class's name followed by gibberish (as far as you are concerned).

You can replace the default behavior by defining a toString method in your class.

Page 40: Object state: fields

40

toString method syntax

The toString method, general syntax:public String toString() { <statement(s) that return a String>;}

NB: The method must have this exact name and signature (i.e., public String toString()).

Example:// Returns a String representing this Point.public String toString() { return "(" + x + ", " + y + “, “ + z + ")";}


Recommended