In this assignment you will practice using inheritance. It is slightly shorter t
ID: 3805009 • Letter: I
Question
In this assignment you will practice using inheritance. It is slightly shorter than usual because Unit 5's programming homework is longer.
Q1: Design and then implement three different classes that work together to define shapes: RoundShape, Cylinder, and Cone. For each class, store fundamental data about its size and provide methods to access and modify this data. In addition, provide appropriate methods to compute the area, and volume, for Cylinder and Cone. In your design, consider how shapes are related and thus where inheritance can be implemented. Don't create duplicate instance variables. Use the UML diagram below as a guide:
Create a main method which instantiates 2 Cylinder objects (any parameters), 2 Cone objects (any parameters), display them with ToString(), change one parameter (your choice) in each, and display them again. Attached is a base file that will help you define the different classes in the same file. Although it is not best practice, for this assignment we ask that you combine your classes into a single file to keep your files together. Remember that you MUST rename the base file and class to contain your last name. [25 points]
Sample Output:
RoundShape height double radius double RoundShape(r double, h double) get Radius() double getHeight double 0 setHeight(h double) void set Radius(r: double) void getArea0 double get Volum 0 double Cylinder Cone RoundShape(r double, h double RoundShape(r double, h double getArea0 double getArea0 double getVolume() double getVolume() double toString(): String toString(): StringExplanation / Answer
Here is working solution-
//RoundShape.java
public class RoundShape {
private double height;
private double radius;
public RoundShape(double r,double h){
this.height=h;
this.radius=r;
}
/**
* @return the height
*/
public double getHeight() {
return height;
}
/**
* @param height the height to set
*/
public void setHeight(double height) {
this.height = height;
}
/**
* @return the radius
*/
public double getRadius() {
return radius;
}
/**
* @param radius the radius to set
*/
public void setRadius(double radius) {
this.radius = radius;
}
public double getArea(){
return Math.PI*radius*radius;
}
public double getVolume(){
return height* getArea();
}
}
//Cylinder.java
public class Cylinder extends RoundShape{
public Cylinder(double r, double h) {
super(r, h);
}
/**method to calculate area of cyclinder
*
*/
public double getArea(){
double r=getRadius();
double h=getHeight();
double t=Math.sqrt(r*r+h*h);
return 2*Math.PI*r*h+2*Math.PI*r*r;
}
/**method to calculate volume of cyclinder
*
*/
public double getVolume(){
double r=getRadius();
double h=getHeight();
return Math.PI*r*r*h;
}
@Override
public String toString() {
return "A Cylinder of radius "+ getRadius() + ", area " + getArea() + ", and volume " + getVolume()+".";
}
}
//Cone.java
public class Cone extends RoundShape{
public Cone(double r,double h){
super(r,h);
}
/**method to calculate area of cone
*
*/
public double getArea(){
double r=getRadius();
double h=getHeight();
double t=Math.sqrt(r*r+h*h);
return Math.PI*r*r+Math.PI*r*t;
}
/**method to calculate volume of cone
*
*/
public double getVolume(){
double r=getRadius();
double h=getHeight();
return Math.PI*r*r*h/3;
}
@Override
public String toString() {
return "A Cone of radius "+ getRadius() + ", area " + getArea() + ", and volume " + getVolume()+".";
}
}
//Main.java
public class Main{
public static void main(String[] args) {
Cylinder cylinder1=new Cylinder(5,7);
System.out.println(cylinder1);
Cylinder cylinder2=new Cylinder(10,12);
System.out.println(cylinder2);
Cone cone1=new Cone(5,10);
System.out.println(cone1);
Cone cone2=new Cone(10,15);
System.out.println(cone2);
}
}