CIS247A – Exam Part 2 class Point { private int x, y; public Point() { x = y = 0
ID: 674218 • Letter: C
Question
CIS247A – Exam Part 2
class Point
{
private int x, y;
public Point()
{
x = y = 0;
}
public Point(int xC, int yC)
{
x = xC;
y = yC;
}
public int X
{
get { return x; }
set { x = value; }
}
public int Y
{
get { return y; }
set { y = value; }
}
public string ToString()
{
return "Point: (" + x + ", " + y + ")"; //example: (2,3)
}
}
Use the class above to create a new class called Line. Line will have 2 Point attributes, startPoint and endPoint. Create 2 Line constructors. The default constructor will create 2 Points with the default Point constructor. The overloaded constructor will accept 4 integers an create 2 Points with the Point overloaded constructor. Add a destructor and a ToString method.
Use the Point class to create a new class called ThreeDPoint. This class will inherit from Point. There will be one attribute, z, defined as an integer. Create a default constructor to that calls the Point default constructor and sets z to 0. Create an overloaded constructor that calls the Point overloaded constructor and sets z to the value passed in. Add a destructor and ToString method.
Use the code below for the Main within the Program class to create two Line objects(one with each constructor) and two Three3DPoint objects(one with each constructor):
Line line1 = new Line();
Line line2 = new Line(2,3,3,6);
Console.WriteLine(line1.ToString();
Console.WriteLine(line2.ToString());
ThreeDPoint tdp1 = new ThreeDPoint();
ThreeDPoint tdp2 = new ThreeDPoint(2,2,3);
Console.WriteLine(tdp1.ToString();
Console.WriteLine(tdp2.ToString());
Explanation / Answer
public class ThreeDPoint extends Point{
private int z;
public ThreeDPoint() {
super();
z=0;
}
public ThreeDPoint(int a, int b, int c) {
super(a, b);
z=c;
}
@Override
public String toString() {
// TODO Auto-generated method stub
return "[x="+super.getX()+", y="+super.getY()+", z="+z+"]";
}
@Override
protected void finalize() throws Throwable {
// TODO Auto-generated method stub
super.finalize();
z=0;
}
}