Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

What is the rule for a super reference in a constructor? It must be in the paren

ID: 3797863 • Letter: W

Question

What is the rule for a super reference in a constructor?

It must be in the parent class' constructor.

It must be the last line of the constructor in the child class.

It must be the first line of the constructor in the child class.

Only one child class can use it.

You cannot use super in a constructor.

Consider the following class definition.

public class WhatsIt {
    private int length;
    private int width;


    public int getArea () {
        // implementation not shown
    }

    private int getPerimeter () {
        // implementation not shown
    }
}

A child class Thingy that extends WhatsIt would have access to:

getArea()

getPerimeter()

width, length, getPerimeter()

width, length, getArea()

all of the above

Questions 18 - 20 pertain to the following class, Point:

public class Point {
    private double x;
    private double y;

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

    public Point(double a, double b) {
        /* missing code */
    }
    // ... other methods not shown
}

Which of the following correctly implements the equals method?

public boolean equals(Point p) {
    return (x == Point.x && y == Point.y);
}

public void equals(Point p) {
    System.out.println(x == p.x && y == p.y);
}

public boolean equals(Point p) {
    System.out.println(x == p.x && y == p.y);
}

public boolean equals(Point p) {
    return (x == p.x && y == p.y );
}

public void equals(Point p) {
    return (x == p.x && y == p.y );
}

The default constructor sets x and y to (0, 0) by calling the second constructor. What could be used to replace /* missing code */ so that this works as intended?

a = 0;
b = 0;

this(0, 0);

this (x, y);

a = x;
b = y;

x = a;
y = b;

Which of the following correctly implements a mutator method for Point?

public double getX() {
    return x;
}

public double getX() {
    return a;
}

public void setCoordinates (double a, double b) {
    x = a;
    y = b;
}

public void setCoordinates (double a, double b) {
    Point p = new Point(a,b);
}

None of the above

Explanation / Answer

Rule for super reference is:

It must be the first line of the constructor in the child class.

Consider the following class definition.

public class WhatsIt {
    private int length;
    private int width;


    public int getArea () {
        // implementation not shown
    }

    private int getPerimeter () {
        // implementation not shown
    }
}

A child class Thingy that extends WhatsIt would have access to:

getArea() only.

Private members are not accessible by child class objects.

Questions 18 - 20 pertain to the following class, Point:

public class Point {
    private double x;
    private double y;

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

    public Point(double a, double b) {
        /* missing code */
    }
    // ... other methods not shown
}

Which of the following correctly implements the equals method?

public boolean equals(Point p) {

    return (x == p.x && y == p.y );
}

The missing code is:

a = x;
b = y;

The mutator is:

public void setCoordinates (double a, double b) {
    x = a;
    y = b;

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote