0% found this document useful (0 votes)
9 views4 pages

Classes and Objects Quiz #2

The document defines three PlanePoint objects a, b, and c with initial x and y coordinates. It then demonstrates how passing these objects into a change function does not modify the original objects due to Java's pass by reference behavior. The change function creates new PlanePoint objects, changing the references but not the original values. This results in the original a, b, c values being printed after each call to change.

Uploaded by

Sreevatsan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views4 pages

Classes and Objects Quiz #2

The document defines three PlanePoint objects a, b, and c with initial x and y coordinates. It then demonstrates how passing these objects into a change function does not modify the original objects due to Java's pass by reference behavior. The change function creates new PlanePoint objects, changing the references but not the original values. This results in the original a, b, c values being printed after each call to change.

Uploaded by

Sreevatsan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Sreevatsan Srinivas

CSC-162
Classes and Objects Quiz #2

PlanePoint a = new PlanePoint(6.2, 1.9);


PlanePoint b = new PlanePoint(2.8, 5.6);
PlanePoint c = new PlanePoint(4.9, 6.4);

a
x = 6.2
y = 1.9

b
x = 2.8
y = 5.6

c
x = 4.9
y = 6.4

Output: (6.2,1.9) (2.8,5.6) (4.9,6.4)


u = new PlanePoint(3.2, 74);

A reference of a is passed into change as parameter u. In the first line of change, setting the
value of u to a new PlanePoint object will fully change the reference to which u points to.
Therefore, when change(a, b, c) is run, a remains unchanged since u no longer points to the
same object as a

u
x = 3.2
y = 74

a
x = 6.2
y = 1.9

v.setX(8.4)

b
x = 8.4
y = 5.6

w = u;

w will now point to the same object as u, which is the newly created object. Similar to what
happens to a, this will completely change the object that w points to. w no longer is pointing to c,
since its reference is changed to object u. Therefore, c will remain unchanged

u, w
x = 3.2
y = 74

c
x = 4.9
y = 6.4

Output: (6.2,1.9) (8.4,5.6) (4.9,6.4)


change(b, c, a)

u = new PlanePoint(b, c, a);

A reference of b is passed into change as parameter u. In the first line of change, setting the
value of u to a new PlanePoint object will fully change the reference to which u points to.
Therefore, when change(a, b, c) is run, b remains unchanged since u no longer points to the
same object as b and therefore the code does not cause any changes to b

u
x = 3.2
y = 74

b
x = 8.4
y = 5.6

v.setX(8.4)

Sets the X value of c to 8.4

c
x = 8.4
y = 6.4

w = u;

w will now point to the same object as u, which is the newly created object. Similar to what
happens to a, this will completely change the object that w points to. w no longer is pointing to c,
since its reference is changed to object u. Therefore, a will remain unchanged

u, w
x = 3.2
y = 74
a
x = 6.2
y = 1.9

Output: (6.2,1.9) (8.4,5.6) (8.4,6.4)

FINAL OUTPUT:

(6.2,1.9) (2.8,5.6) (4.9,6.4)


(6.2,1.9) (8.4,5.6) (4.9,6.4)
(6.2,1.9) (8.4,5.6) (8.4,6.4)

You might also like