Need help in data structures java. Please be descriptive. Thank you To gain expe
ID: 3815610 • Letter: N
Question
Need help in data structures java. Please be descriptive. Thank you To gain experience with using java interface, abstract class, base class and subclass. Explain the purpose of the program as detail as possible 8%. Develop a solution for the problem and mention algorithms to be used -12% List data structures to be used in solution. - 5%. Give a description of how to use the program and expected input/output - 5% Explain the purpose of each class you develop in the program. - 5%. For each method, give the pre and post conditions and invariant, if any - 10% Program execution according to the requirements given 50% Naming of program as required 5% You arc to write a program name Shape.java that will: Write an inheritance hierarchy of three-dimensional shapes. Make a top-level shape interface that has methods for getting information such as volume and surface area of three-dimensional shape. Now make classes that arc subclasses that implement various shapes such as cubes, rectangular prisms, triangular prisms, sphere, cones and cylinders. Place common behavior in super classes whenever possible as well as using abstract classes when appropriate. Add methods to the subclasses to represent unique behavior of each three-dimensional shape such as methods to get a sphere's radius, etc. All of these will be used in the main program rightarrow Shape.java (where you must invoke the methods from the subclasses and print values such as area, circumference, volume, perimeter, etc. where possible.)Explanation / Answer
/**
*
* @author Sam
*/
public interface ThreeD { //structure
double getVolume();
double getSurfaceArea();
}
class RectangularPrism implements ThreeD {
private double length, breadth,height;
public RectangularPrism(double length, double breadth, double height) {
this.length = length;
this.breadth = breadth;
this.height = height;
}
public double getLength() {
return length;
}
public void setLength(double length) {
this.length = length;
}
public double getBreadth() {
return breadth;
}
public void setBreadth(double breadth) {
this.breadth = breadth;
}
public double getHeight() {
return height;
}
public void setHeight(double height) {
this.height = height;
}
@Override
public double getVolume() {
System.out.println("Caulating volume of Cube");
return length*breadth*height;
}
@Override
public double getSurfaceArea() {
System.out.println("Caulating s.area of Cube");
return 2*(length*breadth + breadth*height + height*length);
}
}
class Sphere implements ThreeD { //class sphere
double radius;
public double getRadius() {
return radius;
}
public void setRadius(double radius) {
this.radius = radius;
}
public Sphere(double radius) {
this.radius = radius;
}
@Override
public double getVolume() {
System.out.println("Caulating s.area of Sphere");
return 4*Math.PI*radius*radius*radius/3;
}
@Override
public double getSurfaceArea() {
System.out.println("Caulating s.area of Sphere");
return 4*Math.PI*radius*radius;
}
}
abstract class CircularBase implements ThreeD { //abstract class
double radius, height;
public CircularBase(double radius, double height) {
this.radius = radius;
this.height = height;
}
public double getRadius() {
return radius;
}
public void setRadius(double radius) {
this.radius = radius;
}
public double getHeight() {
return height;
}
public void setHeight(double height) {
this.height = height;
}
}
class Cone extends CircularBase { //class cone as given below
public Cone(double radius, double height) {
super(radius, height);
}
@Override
public double getVolume() {
System.out.println("Caulating volume of Cone");
return Math.PI * getRadius() * getRadius() *getHeight()/3;
}
@Override
public double getSurfaceArea() {
System.out.println("Caulating s.area of Cone");
return Math.PI * getRadius() * (getRadius() + Math.sqrt(Math.pow(getHeight(), 2) + Math.pow(getRadius(), 2)));
}
}
class Cylinder extends CircularBase { //cylinder extedns ircular base, since it uses same structure (radius and height), after that everything is normal
public Cylinder(double radius, double height) {
super(radius, height);
}
@Override
public double getVolume() {
System.out.println("Caulating volume of Cone");
return Math.PI * getRadius() * getRadius() *getHeight();
}
@Override
public double getSurfaceArea() {
System.out.println("Caulating s.area of Cylinder");
return 2 * Math.PI * getRadius() * getHeight();
}
}
class Cube extends RectangularPrism { //cube is rectangular prim with all side equal
public Cube(double length) {
super(length, length, length);
}
}
class TriangularPrism implements ThreeD { //normal subclass
double sideC, sideA, sideB, height;
public TriangularPrism(double baseArea, double sideA, double sideB, double height) {
this.sideC = baseArea;
this.sideA = sideA;
this.sideB = sideB;
this.height = height;
}
public double getSideC() {
return sideC;
}
public void setSideC(double sideC) {
this.sideC = sideC;
}
public double getSideA() {
return sideA;
}
public void setSideA(double sideA) {
this.sideA = sideA;
}
public double getSideB() {
return sideB;
}
public void setSideB(double sideB) {
this.sideB = sideB;
}
public double getHeight() {
return height;
}
public void setHeight(double height) {
this.height = height;
}
@Override
public double getVolume() {
System.out.println("Calulating volume of Triangular Prish...");
return height * Math.sqrt(-Math.pow(sideA, 4) + 2* Math.pow(sideA*sideB, 2) + 2 * Math.pow(sideA*sideC, 2) - Math.pow(sideB, 4) + 2* Math.pow(sideB*sideC, 2) - Math.pow(sideC, 4))/4;
}
@Override
public double getSurfaceArea() {
System.out.println("Calulating s.area of Triangular Prish...");
return Math.random();
}
}
class Shape { //main method class
public static void main(String[] args) {
ThreeD cube = new Cube(15);
System.out.println("Area of cube is: " + cube.getSurfaceArea());
ThreeD sphere = new Sphere(15);
System.out.println("Area of sphere is: " + sphere.getSurfaceArea());
ThreeD cone = new Cone(15, 10);
System.out.println("Area of cone is: " + cube.getSurfaceArea());
}
Here you go champ. The code well comented, just for you. :D let me know if you face any difficulty.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.