Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Driver Class: import java.util.ArrayList; import java.util.List; public class Project2 { public static void main(String[] args) { /*ThreeDimensionalShape sphere = new Sphere(2.0); ThreeDimensionalShape cube = new
Driver Class:
import java.util.ArrayList; | |
import java.util.List;
| |
public class Project2 { | |
public static void main(String[] args) {
| |
/*ThreeDimensionalShape sphere = new Sphere(2.0); | |
ThreeDimensionalShape cube = new Cube(5.0); | |
ThreeDimensionalShape cylinder = new Cylinder(4.0, 1.0); | |
List | |
shapes.add(sphere); | |
shapes.add(cube); | |
shapes.add(cylinder); | |
shapes.forEach(System.out::println);*/ | |
Sphere sphere = new Sphere(2.0); | |
System.out.println(sphere); | |
} | |
} |
Sphere Class:
public class Sphere { | |
private double radius; | |
public Sphere() { | |
super(); | |
this.radius = 0.0; | |
} | |
public Sphere(double v) { | |
super(); | |
this.radius = v; | |
} | |
public double getRadius() { | |
return radius; | |
} | |
public void setRadius(double radius) { | |
this.radius = radius; | |
} | |
public double surfaceArea() { | |
return 4.0 * Math.PI * Math.pow(radius, 2); | |
} | |
public double volume() { | |
return (4.0/3.0) * Math.PI * Math.pow(radius, 3); | |
} | |
@Override | |
public String toString() { | |
final StringBuilder sb = new StringBuilder("Sphere {"); | |
sb.append("radius=").append(radius); | |
sb.append(", surface area=").append(surfaceArea()); | |
sb.append(", volume=").append(volume()); | |
sb.append('}'); | |
return sb.toString(); | |
} | |
} |
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started