Coordinate2D
class
The demonstrations on this page use the Coordinate2D
class. There are no tricks in the Coordinate2D
class. Each constructor and method does what its header suggests.
Selected parts of Coordinate2D
are shown below.
private int x, y;
public Coordinate2D()
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.
Object construction demo
public class Demo
{
public static void main(String[] args)
{
Coordinate2D c1 = new Coordinate2D(5, 6);
System.out.println(c1);
}
}
public class Coordinate2D
{
private int x, y;
// Other constructor not shown
public Coordinate2D(int initX, int initY)
{
x = initX;
y = initY;
}
// Additional methods not shown
}
See the step by step trace of this code.
The same trace is available with each step on its own page, starting with Step 1.
Additional classes & objects resources
- Class writing order
- Primitive types vs references exercises
- Primitive types vs references exercises with calls
- ZombiePlant free response
Help & comments
Get help from AP CS Tutor Brandon Horn