Need help in java data structures. Please show results. Thank you. Objectives: T
ID: 3810990 • Letter: N
Question
Need help in java data structures. Please show results. Thank you. Objectives: To gain experience with using java interface, abstract class, base class and subclass. Documentation: Explain the purpose of the program as detail as possible 8%. 2 Develop a solution for the problem and mention algorithms to be used -12% List data structures to be used in solution. 5%. 4 Give a description of how to use the program and expected input/output 5% 5.Explain the purpose of each class you develop in the program. 5%. Programming: 1.For each method, give the pre and post conditions and invariant, if any 10 2 Program execution according to the requirements given 50% 3 Naming of program as required 5% Description of Program You are 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 are 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 Shapejava (where you must invoke the methods from the subclasses and print values such as area, circumference, volume, perimeter, etc. where possible.)Explanation / Answer
Code:-
import java.util.*;
import java.lang.*;
import java.io.*;
import java.Math.*;
public abstract class Shape{
public abstract double area();
public abstract double circumference();
public abstract double volume();
}
public class Sphere extends Shape{
private double radius;
public Sphere(){
this(1);
}
public Sphere(double radius){
this.radius=radius;
}
@Override
public double area(){
return 4*(3.14)*radius*radius;
}
@Override
public double circumference(){
return 2*(3.14)*radius ;
}
@Override
public double volume(){
return (4*(3.14)*radius*radius*radius)/3;
}
}
public class Cylinder extends Shape{
private double radius,height;
public Cylinder(){
this(1,1);
}
public Cylinder(double radius,double height){
this.radius=radius;
this.height=height;
}
@Override
public double area(){
return 2*(3.14)*radius*(radius+height);
}
@Override
public double circumference(){
return 2*((3.14)*2*radius)+height) ;
}
@Override
public double volume(){
return ((3.14)*radius*radius*height);
}
}
public class Cone extends Shape{
private double radius,height;
public Cone(){
this(1,1);
}
public Cone(double radius,double height){
this.radius=radius;
this.height=height;
}
@Override
public double area(){
return (((3.14)*radius*radius)+((3.14)*radius*(Math.sqrt((radius*radius)+(height*height))));
}
@Override
public double circumference(){
return 2*(3.14)*radius ;
}
@Override
public double volume(){
return ((3.14)*radius*radius*(height/3));
}
}
class TestShape {
public static void main(String[] args){
//Sphere;
double radius=3;
Shape sphere = new Sphere(radius);
System.out.println("Sphere radius: " + radius
+ " Resulting area: " + sphere.area()
+ " Resulting perimeter: " + sphere.circumference() + " Resulting volume: "+ sphere.volume() +" ");
//Cylinder
double radius=3;
double height=5;
Shape cylinder = new Cylinder(radius,height);
System.out.println("Cylinder radius: " + radius + "Cylinder height: " + height + " Resulting area: " + cylinder.area() + " Resulting perimeter: " + cylinder.circumference() + " Resulting volume: "+ cylinder.volume() +" ");
//Cone
double radius=5;
double height=7;
Shape cone = new Cone(radius,height);
System.out.println("Cone radius: " + radius + "Cone height: " + height + " Resulting area: " + cone.area() + " Resulting perimeter: " + cone.circumference() + " Resulting volume: "+ cone.volume() +" ");
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.