This is question #4 on the collegeboard AP Comp Sci A practice review multiple choice questions from the AP Central website:
Consider the following declaration for a class that will be used to represent points in the xy-coordinate plane.
public class Point
{
private int myX; // coordinates
private int myY;
public Point ( )
{
myX = 0;
myY = 0;
}
public Point(int a, int b)
{
myX = a;
myY = b;
}
// ... other methods not shown
}
The following incomplete class declaration is intended to extend the above class so that points acn be named.
public class NamedPoint extends Point
{
private
String myName;
// constructors go here
// ... other methods not shown
}
Consider the following proposed constructors for this class.
I. public NamedPoint()
{
myName = "";
}
II. public NamedPoint(int d1, int d2, String name)
{
myX = d1;
myY = d2;
myName = name;
}
III. public NamedPoint(int d1, int d2, String name)
{
super(d1, d2);
myName = name;
}
Which of these constructors would be legal for the NamedPoint class?
(A) I only
(B) II only
(C) III only
(D) I and III
(E) II and III
My analysis:
OK well I'm not familiar with anything that represents points in the xy-coordinate plane, but that doesn't seem to affect my ability to answer this question.
So we have a public class called Point. It has private integer coordinates declared as myX and myY, respectively for the X and Y coordinates. (SIDE NOTE: Why do they declare int myX and myY as private?)
Next we have a public method called Point. It initializes myX and myY to 0.
Next we have the Point method which we're going to make look like int a, int b. We're declaring myX = a and myY = b.
Some other methods apparently are needed to make this run in a compiler.
Now we have an incomplete class declaration which extends the Point class so that points can be named.
We have a public class called NamedPoint that extends Point. (SIDE NOTE: What is the purpose of extending a class? What does it really do? I've been doing it all year and never really understood it.)
Now we declare a private String called myName.
Some constructors and methods are not shown. Looking below, it looks like the purpose of this question is to see if I know what constructos should go below here to make this program work.
OK so I need to figure out which of the three constructos would work.
The first one doesn't seem to do anything.
The second one declares myX and myY to equal d1 and d2, respectively. Then it declares myName = name. Also, it makes NamedPoint look like coordinate X, coordinate Y, name). This seems right to me.
The third one is the same except it does something with super and does not declare what d1 and d2 are equal to. I know in an inheritance hierarchy that super refers to the parent class of a class. But I don't think that has anything to do with this problem, and it makes no sense to me using it in this way. I don't think III works.
So that means II only, C. Is it right? *checks back at answer* No, the answer is D. So that means it's the opposite of what I thought. Wonderful! So apparently I don't know what the heck I'm talking about!
HELP anyone?
Thinking about it some more I think I misread the question. It says which of these constructors would be LEGAL for the NamedPoint class. Apparently I and III are legal, but why? Why isn't II legal?