5.The design for class and
inheritance
- 1.
Inheritance progrmming
(1).Create class Point, with two private instance variables x and y that representd for the coodinates of a point.
Provideconstructor for initializing two instance variables.
Provide
set and
get methods for each instance variable,
Provide
toString
method to return formatted string for a point coodinates.
(2). Create class
Circle,it inheritance from Point.
Provide a integer private
redius instance variable.
Provide
constructor to initialize the center coodinates and redius for a circle,.In the constructor it must calls its supperclass
constructor
by using
super(x,y);
Provideset and
getfor redius instance variable.
Provide two methods for calculating its
area and
perimeter.
Provide
toString method to return a formatted string for a circle features.In the method, it must calls its supperclass
toString
methodby using
super.toString().
(3)Similar to the above class
Circle definitionto,Design
Rectangle class.
(4)Similar to the above class
Rectangle definition ,Design
Cube class,
It has a
hight instance variable.
Calculate the surface
area and
volumn for a Cube.
(5)Design a test-class to test above four classes functionality.
It creates four objects for these classes and call the corresponding methods to test entire functionality for each class.
C
omplete
the following code:
……
class GeometricTest
{
publicstaticvoid main (String[] args)
{
System.
out
.println(new Point(1,1));
Circle circle =
new Circle(3,-1,6);
System.
out
.println(circle.toString());
System.
out
.printf("Area of Circle = %.2f\n",circle.getArea());
Rectangle r =
new Rectangle(1,2,4,5);
System.
out
.println(r.toString());
System.
out
.printf("Area of Rectangle = %.2f\n",r.getArea());
Cube c =
new Cube(4,5,2);
System.
out
.println(c.toString());
System.
out
.printf("Area of cube = %.2f\n",c.getArea());
System.
out
.printf("Volume of cube = %.2f\n",c.getVolume());
}
}
The running output:
Point: 1,1
Circle : Center:3,-1 radius:6
Area of Circle = 113.04
Rectangle : Top left 1,2 length:4 width:5
Area of Rectangle = 20.00
Cube : Top left Point:4,5 depth :2
Area of cube = 24.00
Volume of cube = 8.00