Need some help with computer science homework. I greatly appreciate the help!! T
ID: 3800337 • Letter: N
Question
Need some help with computer science homework. I greatly appreciate the help!!
The bolded is what I think is the answer
1. 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.
2.
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 3-5 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
}
3. 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 );
}
4. 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;
5. 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
1. 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.
Answer:-
It must be the first line of the constructor in the child class.
Explanation:-
super() call must be placed as first statement only in the constructor,
else it leads to Compiler Error:"call to super must be first statement in a constructor".
2.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
Answer:-
getArea()
Explanation:-
The class members which have private keyword in it's creation are called private members. Those members are only accessible with in that class.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.