Quiz J1-2 | CS 2113 Software Engineering - Spring 2021

Quiz J1-2

Submit your answers via this Google Form

For the below questions, when the class Point is referenced, we are talking about the below class, which you can assume is fully implemented and working as described:

public class Point{
   private double x,y; //the x,y fields
   public Point(double x,double y); //construct a point from an x,y
   public Point(Point other); //construct a point from another point
   public double getX(); //return the x component
   public double getY(); //return the y component
   public double setXY(double x, double y); //return the x component
   public String toString(); //return the string representation
   private double sum_x_y(); // Returns the sum of X and Y
}

Question 1

Say we want to make a class that extends Point with a method that can reflect a point across the X and Y axis:

public class CustomPoint extends Point{
    public void reflect(); // Reflects point about the X and Y axis (e.g., -X and -Y values)
}

Which of the following implementations achieves this?

    // Option 1
    public void reflect(){
        x = -x;
        y = -y;
    }
    
    // Option 2
    public void reflect(){
        double x = -this.getX();
        double y =-this.getY();
        this.setXY(x,y);
    }

    
    // Option 3
    public void reflect(){
        double x = -this.getX();
        double y =-this.getY();
        this.x = x;
        this.y = y;
    }

    // Option 4
    public void reflect(){
        this = Point(-x,-y);
    }
    
    // Option 5
    public void reflect(){
        x = -this.getX();
        y = -this.getY();
    }

Question 2

If we want to re-implement sum_x_y in our custom point as a public method that first reflects the points within CustomPoint and then returns the sum, which of the following implementations are valid? (Note: assume that reflect has a valid implementation)

    //Option 1
    public double sum_x_y(){
        this.reflect()
        return super.sum_x_y();
    }

    //Option 2
    public double sum_x_y(){
        this.reflect();
        return this.getX() + this.getY();
    }

    //Option 3
    public double custom_sum_x_y(){
        this.reflect();
        return this.getX() + this.getY();
    }

    //Option 4
    public double custom_sum_x_y(){
        this.reflect()
        return this.x + this.y;
    }

Question 3

Consider a valid implementation CustomPoint with an implementation of sum_x_y as described above, what is the output of the program below?

    public static void main(final String args[]){
        CustomPoint[] points = new CustomPoint[10];
        points[0] = new CustomPoint();
        points[0].setXY(10, 10);

        int total_sum = 0;
        for (CustomPoint p : points) {
            p = points[0];
            // Make sure you understand what this function does!!!
            total_sum += p.sum_x_y();
        }
        System.out.println(total_sum);
    }

Question 4

Again considering the main() method from Question 3, if Point defined sum_x_y() as a protected method, like

public class Point{
   private double x,y; //the x,y fields
   //...
   protected double sum_x_y();

And CustomPoint re-implemented sum_x_y() like so:

public double sum_x_y(){
   this.reflect;
   return super.sum_x_y();
}

What is the output of the program in Question 3 now?

Question 5

Again considering the main() method from Question 3, and that Point implements sum_x_y() as a proected method (like in Question 4). If CustomPoint implements sum_x_y() like below

protected double sum_x_y(){
   this.reflect;
   return super.sum_x_y();
}

What is the output of the program in Question 3 now?