Was wondering if anyone could help me get started on this problem for my Java cl
ID: 3687307 • Letter: W
Question
Was wondering if anyone could help me get started on this problem for my Java class. I've hit a wall and am having issues coming up with working methods for the three differenct classes, any help would be greatly appreciated.
Design and then implement three different classes that work together to define shapes: Round Shape, Cone, and Sphere. 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 Sphere and Cone. In your design, consider how shapes are related and thus where inheritance can be implemented. Don't create duplicate instance variables. Create a main method which instantiates 2 Sphere 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 an optional basefile that will help you define the different classes in the same file. Remember that you MUST rename the base file and class to contain your last name.
Explanation / Answer
class RoundShape{
double shape = 10;
double radius = 5;
int cone1 = 3;
int sphere1;
public String toString(){
return " the man" + cone1 + "this also" + sphere1;
}
}
class Sphere extends RoundShape{
double getArea(){
double area = 4 * Math.PI * Math.pow(radius, 2);
return area;
}
double getVolume(){
double volume = (4/3) * Math.PI * Math.pow(radius, 3);
return volume;
}
public String toString() {
return radius + super.toString();
}
class Cone extends RoundShape{
double height = 8;
double getArea(){
double area = Math.PI * radius * (radius + Math.sqrt(Math.pow(height, 2) + Math.pow(radius, 2)));
return area;
} // end of getArea
double getVolume(){
double volume = Math.PI * Math.pow(radius, 2) * (height/3);
return volume;
} // end of getVolume for Cone
public String toString() {
return height + super.toString();
}
} // end of the class Cone
public class Demonstration{
public static void main(String[] args)
{
Sphere sphere1 = new Sphere();
sphere1.getArea();
sphere1.getVolume();
sphere1. toString();
System.out.println(sphere1);
Cone cone1 = new Cone();
cone1.getArea();
cone1.getVolume();
cone1. toString();
System.out.println(cone1);
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.