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

Quiz J1-1

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
}

Which of the following implementations achieves this?

    // Option 1
    public void reflect(){
        x = -x;
        y = -y;
    }

    // Option 2
    public void reflect(){
        this.x = -this.x;
        this.y = -this.y;
    }

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

Question 2

If we add this constructor to CustomPoint:

    public CustomPoint(){
        setXY(10,10); // Line 1
        super(0,0); // Line 2
    }

…and then run this program, what is the output?

    public static void main(final String args[]){
        CustomPoint p = new CustomPoint();
        System.out.println(p.toString());
    }

Question 3

What if we switch line 1 and 2 as marked above?

Question 4

If we want to re-implement sum_x_y in our custom point, but first reflect the point before returning 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 super.sum_x_y();
    }

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