The software I use is Eclipse.If possible, please explain some important things
ID: 3711303 • Letter: T
Question
The software I use is Eclipse.If possible, please explain some important things in the programme. Thanks.
Implement the shape hierarchy show in the figure below. Each TwoDimensionalShape 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 array of Shape references to objects of each 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. Shape TwoDimensionalShape ThreeDimensionalShape Circle Square Triangle Sphere Cube TetrahedronExplanation / Answer
The required code for all 10 classes including Driver class are defined below. You can add more details if you want, but the code is running perfectly and is organized very well. Go through the comments included and get a clear idea about how things work. Thanks
// Shape.java
public abstract class Shape {
/**
* Defined as a super class for all shapes, no need to have any special
* attributes or methods
*/
}
// TwoDimensionalShape.java
/**
* Representing a 2D shape
*/
public abstract class TwoDimensionalShape extends Shape{
/** method prototype for area */
public abstract double getArea();
}
// ThreeDimensionalShape.java
/**
* Representing a 3D shape
*/
public abstract class ThreeDimensionalShape extends Shape{
/** method prototype for surface area */
public abstract double getArea();
/** method prototype for volume */
public abstract double getVolume();
}
// Circle.java
public class Circle extends TwoDimensionalShape {
private double radius;
/**
* constructor with argument
* @param radius - radius of the circle
*/
public Circle(double radius) {
this.radius = radius;
}
@Override
public double getArea() {
/**
* area of circle = PI*r*r
*/
return Math.PI * radius * radius;
}
@Override
public String toString() {
return "Shape: Circle with radius: "+radius;
}
/*getters and setters*/
public double getRadius() {
return radius;
}
public void setRadius(double radius) {
this.radius = radius;
}
}
// Square.java
public class Square extends TwoDimensionalShape {
private double side;
/**
* @param side
* - side length of square
*/
public Square(double side) {
this.side = side;
}
@Override
public double getArea() {
/**
* area of square=side*side
*/
return side * side;
}
@Override
public String toString() {
return "Shape: Square with side length: "+side;
}
/* getters and setters */
public double getSide() {
return side;
}
public void setSide(double side) {
this.side = side;
}
}
// Triangle.java
public class Triangle extends TwoDimensionalShape {
private double sideLength;
public Triangle(double sideLength) {
this.sideLength = sideLength;
}
@Override
public double getArea() {
/**
* Area of triangle= ( square root (3) / 4 ) * side^2
*/
return (Math.sqrt(3) / 4) * sideLength * sideLength;
}
public double getSideLength() {
return sideLength;
}
public void setSideLength(double sideLength) {
this.sideLength = sideLength;
}
@Override
public String toString() {
return "Shape: Triangle with edge: "+sideLength;
}
}
// Sphere.java
public class Sphere extends ThreeDimensionalShape {
private double radius;
public Sphere(double radius) {
this.radius = radius;
}
@Override
public double getVolume() {
/**
* Volume= 4/3 * PI * r^3
* */
return (4.0 / 3.0) * Math.PI * (radius * radius * radius);
}
@Override
public double getArea() {
/**
* Surface area=4 * PI * r^2
*/
return 4*Math.PI*radius*radius;
}
/**
* getter and setter
*/
public double getRadius() {
return radius;
}
public void setRadius(double radius) {
this.radius = radius;
}
@Override
public String toString() {
return "Shape: Sphere with radius: "+radius;
}
}
// Cube.java
public class Cube extends ThreeDimensionalShape {
private double side;
public Cube(double side) {
this.side=side;
}
@Override
public double getArea() {
/**
* surface area of cube= 6*side*side
*/
return 6*side*side;
}
@Override
public double getVolume() {
/**
*volume of cube= side*side*side
*/
return side*side*side;
}
public void setSide(double side) {
this.side = side;
}
public double getSide() {
return side;
}
@Override
public String toString() {
return "Shape: Cube with side length: "+side;
}
}
// Tetrahedron.java
public class Tetrahedron extends ThreeDimensionalShape {
private double edge;
public Tetrahedron(double edge) {
this.edge = edge;
}
@Override
public double getArea() {
/**
* surface area of tetrahedron= sq root of 3 * edge^2
*/
return Math.sqrt(3) * edge * edge;
}
@Override
public double getVolume() {
/**
* volume of tetrahedron= edge^3 / 6 * sq root of 2
*/
return Math.pow(edge, 3) / 6.0 * (Math.sqrt(2));
}
@Override
public String toString() {
return "Shape: Tetrahedron with edge: "+edge;
}
}
//Driver.java
public class Driver {
public static void main(String[] args) {
/**
* Defining an array of shapes
*/
Shape shapes[]=new Shape[6];
/**
* Creating all the 6 types of 2d,3d shapes
*/
shapes[0]=new Circle(5.6);
shapes[1]=new Square(10);
shapes[2]=new Triangle(10.6);
shapes[3]=new Sphere(15.6);
shapes[4]=new Cube(10);
shapes[5]=new Tetrahedron(7.6);
/**
* looping through the array and printing details
*/
for(int i=0;i<shapes.length;i++){
if(shapes[i] instanceof TwoDimensionalShape){
/**
* 2d shape
*/
System.out.println(shapes[i]);
System.out.printf("Area: %.2f ",((TwoDimensionalShape) shapes[i]).getArea());
}else if(shapes[i] instanceof ThreeDimensionalShape){
/**
* 3d shape
*/
System.out.println(shapes[i]);
System.out.printf("Surface Area: %.2f ",((ThreeDimensionalShape) shapes[i]).getArea());
System.out.printf("Volume: %.2f ",((ThreeDimensionalShape) shapes[i]).getVolume());
}
System.out.println();
}
}
}
/*OUTPUT*/
Shape: Circle with radius: 5.6
Area: 98.52
Shape: Square with side length: 10.0
Area: 100.00
Shape: Triangle with edge: 10.6
Area: 48.65
Shape: Sphere with radius: 15.6
Surface Area: 3058.15
Volume: 15902.39
Shape: Cube with side length: 10.0
Surface Area: 600.00
Volume: 1000.00
Shape: Tetrahedron with edge: 7.6
Surface Area: 100.04
Volume: 103.47
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.