S-111
  • Home
  • Lectures
  • Problem Sets
  • Sections
  • Syllabus
  • Schedule
  • Staff
  • Resources
  • Canvas
  • Ed Discussion
  • Gradescope

Section 12

  • Bug of the Day
  • “Blueprint” classes, part II
  • More non-static methods
  • Writing a client

Bug of the Day

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.

“Blueprint” classes, part II

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:

  1. 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? 1

  2. 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. 2

    System.out.println(p3.getX() + ", " + p3.getY());
    System.out.println(p2.getX() + ", " + p2.getY());
    System.out.println(p1.getX() + ", " + p1.getY());
    
  3. 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). 3

    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();
  4. 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? 4

    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));

More non-static methods

  1. 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). 5

  2. Write a mutator called scale that takes two integers, scaleX and scaleY, and scales each of the Point‘s coordinates by their corresponding integers. 6

Writing a client

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.

  1. 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. 7

  2. 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. 8

  3. 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! 9


  1. Here’s the second constructor:

    public Point() {
        this(0, 0);
    }
    

    ↩

  2. Here’s the memory diagram:

    The output would be the following:

    0, 9
    9, 0
    

    The last line would cause a NullPointerException. ↩

  3. The only equivalent or valid statement is c. ↩

  4. a. This is because Java automatically calls an object’s toString method when converting the object to a string in the context of concatenation with another string. ↩

  5. Here’s one implementation:

    public void flip() {
        int temp = this.x;
        this.x = this.y;
        this.y = temp;
    }
    

    Note that we must use a temporary variable to store the value of x before we overwrite it on the next line with the value of y. ↩

  6. Here’s one implementation:

    public void scale(int scaleX, int scaleY) {
        this.x *= scaleX;
        this.y *= scaleY;
    }
    

    ↩

  7. Here’s one implementation:

    public static boolean equals(Point p1, Point p2) {
        return p1.getX() == p2.getX() && p1.getY() == p2.getY();
    }
    

    Note that we must use accessors here, since the x and y fields have private access in Point, rendering them inaccessible from a client class. ↩

  8. Here’s one implementation:

    public static double distance(Point p1, Point p2) {
        int oneX = p1.getX();
        int twoX = p2.getX();
    
        int oneY = p1.getY();
        int twoY = p2.getY();
    
        int xDiff = oneX - twoX;
        int yDiff = oneY - twoY;
    
        return Math.sqrt(xDiff * xDiff + yDiff * yDiff);
    }
    

    As above, we must use accessors here. ↩

  9. Here’s one implementation:

    public static Point closestToOrigin(Point p1, Point p2) {
        if (p1.distanceFromOrigin() < p2.distanceFromOrigin()) {
            return p1;
        } else {
            return p2;
        }
    }
    

    ↩

Last updated on July 14, 2025.