Coordinate2D
class
The exercises on this page use the Coordinate2D
class. There are no tricks in the Coordinate2D
class. Each method does what its header suggests.
Selected parts of Coordinate2D
are shown below.
private int x, y;
public Coordinate2D(int initX, int initY)
public void setX(int newX)
andpublic void setY(int newY)
public String toString()
returns aString
in the form:(x, y)
A link to the complete class is below.
For each exercise, give the output of the code segment.
Primitive exercise
int x = 5;
int y = 6;
x = y;
y = 7;
System.out.println(x);
System.out.println(y);
Primitive exercise solution & explanation
Reference exercise 1
Coordinate2D referenceOne = new Coordinate2D(1, 1);
Coordinate2D referenceTwo = new Coordinate2D(2, 2);
System.out.println(referenceOne);
System.out.println(referenceTwo);
Reference exercise 1 solution & explanation
Reference exercise 2
Coordinate2D referenceOne = new Coordinate2D(1, 1);
Coordinate2D referenceTwo = referenceOne;
System.out.println(referenceOne);
System.out.println(referenceTwo);
Reference exercise 2 solution & explanation
Reference exercise 3
Coordinate2D referenceOne = new Coordinate2D(1, 1);
Coordinate2D referenceTwo = referenceOne;
referenceOne.setX(3);
referenceTwo.setY(4);
System.out.println(referenceOne);
System.out.println(referenceTwo);
Reference exercise 3 solution & explanation
Reference exercise 4
Coordinate2D referenceOne = new Coordinate2D(1, 1);
Coordinate2D referenceTwo = referenceOne;
System.out.println(referenceOne);
System.out.println(referenceTwo);
referenceTwo = new Coordinate2D(2, 2);
System.out.println(referenceOne);
System.out.println(referenceTwo);
Reference exercise 4 solution & explanation
Review the primitive types vs references exercises with AP CS Tutor Brandon Horn.
Additional classes & objects resources
- Constructing objects
- Primitive types vs references exercises with calls
- Class writing order
- ZombiePlant FR
Help & comments
Get help from AP CS Tutor Brandon Horn