Implement the Shape hierarchy shown in Figure below. Each Two-DimensionalShape should contain method getArea to calculate the area of the two-dimensional shape. Each ThreeDimensionalShape should have methods getArea and getVolume to calculate the surface area and volume, respectively, of the three-dimensional shape. Create a program that uses an array of Shape references to objects of each concrete class in the hierarchy. The program should print a text description of the object to which each array element refers. Also, in the loop that processes all the shapes in the array, determine whether each shape is a TwoDimensionalShape or a ThreeDimensionalShape. If it’s a TwoDimensionalShape, display its area. If it’s a ThreeDimensionalShape, display its area and volume.
ANSWER
Shape.java
package plack6_qn01;
/**
*
* @author Eng.Mghase
* @email mghase.sadick1@gmail.com
* @contact 0768454641
* @web http://qksoftz.com
*/
public class Shape {
@Override
public String toString(){
return String.format("%s\n",getClass().getName());
}
}
TwoDimentionalShape.javapackage plack6_qn01;
/**
*
* @author Eng.Mghase
* @email mghase.sadick1@gmail.com
* @contact 0768454641
* @web http://qksoftz.com
*/
public abstract class TwoDimentionalShape extends Shape{
public abstract double getArea();
}
ThreeDimensionalShape.javapackage plack6_qn01;
/**
*
* @author Eng.Mghase
* @email mghase.sadick1@gmail.com
* @contact 0768454641
* @web http://qksoftz.com
*/
public abstract class ThreeDimensionalShape extends Shape{
public abstract double getVolume();
public abstract double getArea();
}
Circle .javapackage plack6_qn01;
/**
*
* @author Eng.Mghase
* @email mghase.sadick1@gmail.com
* @contact 0768454641
* @web http://qksoftz.com
*/
public class Circle extends TwoDimentionalShape{
private double radius;
public Circle(double radius ) {
this.radius=radius;
}
public void setRadius(double radius){
this.radius=radius;
}
public double getRadius(){
return radius;
}
@Override
public double getArea() {
return Math.PI*Math.pow(getRadius(), 2);
}
@Override
public String toString() {
return String.format("%sRadius: %.2f\n", super.toString(),getRadius());
}
}
Square.javapackage plack6_qn01;
/**
*
* @author Eng.Mghase
* @email mghase.sadick1@gmail.com
* @contact 0768454641
* @web http://qksoftz.com
*/
public class Square extends TwoDimentionalShape{
private double length;
public Square(double length) {
this.length=length;
}
public double getLength() {
return length;
}
public void setLength(double length) {
this.length = length;
}
@Override
public double getArea() {
return Math.pow(getLength(), 2);
}
@Override
public String toString() {
return String.format("%sLength: %.2f\n",super.toString(),getLength());
}
}
Triangle.javapackage plack6_qn01;
/**
*
* @author Eng.Mghase
*/
public class Triangle extends TwoDimentionalShape{
private double base,height;
public Triangle(double base, double height) {
this.base = base;
this.height = height;
}
public void setBase(double base) {
this.base = base;
}
public double getBase() {
return base;
}
public void setHeight(double height) {
this.height = height;
}
public double getHeight() {
return height;
}
@Override
public double getArea() {
return (getBase()*getHeight())/2;
}
@Override
public String toString() {
return String.format("%sBase: %.2f\nHeight: %.2f\n", super.toString(),getBase(),getHeight());
}
}
Sphere.javapackage plack6_qn01;
/**
*
* @author Eng.Mghase
* @email mghase.sadick1@gmail.com
* @contact 0768454641
* @web http://qksoftz.com
*/
public class Sphere extends ThreeDimensionalShape{
private double radius;
public Sphere(double radius) {
this.radius=radius;
}
public void setRadius(double radius) {
this.radius = radius;
}
public double getRadius() {
return radius;
}
@Override
public double getVolume() {
return (4/3)*Math.PI*Math.pow(getRadius(), 3);
}
@Override
public double getArea() {
return (1/3)*Math.PI*Math.pow(getRadius(), 2);
}
@Override
public String toString() {
return String.format("%sRadius: %.2f", super.toString(),getRadius());
}
}
Cube.javapackage plack6_qn01;
/**
*
* @author Eng.Mghase
*/
public class Cube extends ThreeDimensionalShape {
private double edge;
public Cube(double edge) {
this.edge = edge;
}
public void setEdge(double edge) {
this.edge = edge;
}
public double getEdge() {
return edge;
}
@Override
public double getArea() {
return 6 * Math.pow(getEdge(), 2);
}
@Override
public double getVolume() {
return Math.pow(getEdge(), 3);
}
@Override
public String toString() {
return String.format("%sEdge: %.2f\n", super.toString(), getEdge());
}
}
Tetrahedron.javapackage plack6_qn01;
/**
*
* @author Eng.Mghase
* @email mghase.sadick1@gmail.com
* @contact 0768454641
* @web http://qksoftz.com
*/
public class Tetrahedron extends ThreeDimensionalShape {
private double edge;
public Tetrahedron(double edge) {
this.edge = edge;
}
public void setEdge(double edge) {
this.edge = edge;
}
public double getEdge() {
return edge;
}
@Override
public double getArea() {
return Math.sqrt(3) * Math.pow(getEdge(), 2);
}
@Override
public double getVolume() {
return Math.pow(getEdge(), 3) / (6 * Math.sqrt(2));
}
@Override
public String toString() {
return String.format("%sEdge: %.2f\n", super.toString(), getEdge());
}
}
ShapeTest.java
package plack6_qn01;
package plack6_qn01;
/**
*
* @author Eng.Mghase
* @email mghase.sadick1@gmail.com
* @contact 0768454641
* @web http://qksoftz.com
*/
public class ShapeTest {
public static void main(String[] args) {
Shape shape[] = new Shape[6];
shape[0] = new Circle(7.0);
shape[1] = new Square(7.0);
shape[2] = new Triangle(4.0, 5.0);
shape[3] = new Sphere(1.0);
shape[4] = new Cube(1.0);
shape[5] = new Tetrahedron(1.0);
for (Shape curentShape : shape) {
System.out.println(curentShape);
if (curentShape instanceof TwoDimentionalShape) {
TwoDimentionalShape twoDimentionalShape = (TwoDimentionalShape) curentShape;
System.out.printf("%sArea: %.2f\n\n", twoDimentionalShape.toString(), twoDimentionalShape.getArea());
} else if (curentShape instanceof ThreeDimensionalShape) {
ThreeDimensionalShape threeDimensionalShape = (ThreeDimensionalShape) curentShape;
System.out.printf("%sArea: %.2f\n\n", threeDimensionalShape.toString(), threeDimensionalShape.getVolume());
System.out.printf("%sArea: %.2f\n\n", threeDimensionalShape.toString(), threeDimensionalShape.getArea());
}
}
}
}




0 comments:
Post a Comment