Recall the Rectangle class you saw in lecture: a “blueprint” class
that represents the coordinates and dimensions of a rectangle. Here’s a
simplified version of the class:
public class Rectangle { private int x; private int y; private int width; private int height; }
Suppose we need a mutator for the Rectangle class that will allow us to
change the values of the x- and y-coordinates of a Rectangle object.
The following proposed mutator has bugs:
public static void setLocation(int newX, int newY) { int x = newX; int y = newY; }
Determine what bugs are present and fix them.
Consider the following class, which contains the beginnings of a blueprint
class for Point objects: Point.java.
Each Point object represents a single point with integer coordinates (x, y).
We will allow each coordinate to take on any integer value, which means that
the constructor and mutator don’t need to perform any error-checking.
Let’s open Point.java in VSCode and read through it to understand what is
already completed. Then, complete the following exercises:
Write a second constructor that takes no parameters and can be used to create a Point with coordinates (0, 0). How can you write it so that this new constructor will call the first constructor to do the work?
Consider the following code fragment, which constructs
some Point objects:
Point p1 = new Point(9, 0); Point p2 = p1; Point p3 = new Point(p1.getY(), p2.getX()); p1 = null;
Remembering that variables “containing” objects actually hold references to the objects, draw the results of this code fragment as a memory diagram. Then, determine what will be printed out, if anything, from the statements below.
System.out.println(p3.getX() + ", " + p3.getY()); System.out.println(p2.getX() + ", " + p2.getY()); System.out.println(p1.getX() + ", " + p1.getY());
Consider the distanceFromOrigin method in the code, which computes the
distance of the called Point object from the origin. Suppose that a client
program creates a Point object as follows:
Point p1 = new Point(-5, 10);
Given this information, which of the following are valid calls to the
distanceFromOrigin method? Try testing your answer(s).
1. double dist = distanceFromOrigin(p1);
2. p1.distanceFromOrigin(-5, 10);
3. double dist = p1.distanceFromOrigin();
4. int dist = Point.distanceFromOrigin();
5. double dist = Point.distanceFromOrigin();
After creating the Point object p1 above, the client prints it as
follows:
System.out.println(p1);
This print statement is equivalent to which of
the following?
1. System.out.println(p1.toString());
2. p1.toString();
3. System.out.println(toString(p1));
4. System.out.println(Point.toString());
5. System.out.println(Point.toString(-5, 10));
Write a mutator called flip that simply exchanges the x-
and y-coordinates of a given point (reflecting the point over the
line y = x).
Write a mutator called scale that takes two
integers, scaleX and scaleY, and scales each of
the Point‘s coordinates by their corresponding integers.
PointExplorer.java contains a client program for our Point
class. Save it in the same folder as your modified Point class. Open
PointExplorer.java in VSCode and try to compile it. If it doesn’t
compile, that means that at least one of your methods has an incorrect
header. Fix your code now — and feel free to ask for help. Once it
compiles, run the client to verify your methods work.
Note
For each of the following static methods, you should add test code
to the main() method after you implement them, just as we did
for testing the non-static methods of Point.
Write an equals method that returns true if two Point objects
are equal. This method should be part of PointExplorer.java.
Decide what parameters are needed, and think
about how you must access the data in the objects differently from
how you might in a non-static version. Your static equals
method should not use the non-static equals() method
for Point objects.
Write a static method in the client PointExplorer class
called distance that takes two Point objects and returns
the distance between them on the Cartesian plane.
Write a static method closestToOrigin that takes two Points
and returns the Point that is closest to the origin.
Hint: make use of the distanceFromOrigin() method that every Point
has!
Last updated on July 13, 2026.