Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Created by danie on 10/28/2016.
- */
- public interface Shape3D {
- public double getVolume();
- public double getSurfaceArea();
- }
- /*
- Shape3D interface
- Things with two bases | sphere | cones
- rectangular prism | triangular prism | cylinders|
- cube|
- */
- /**
- * Created by danie on 10/30/2016.
- */
- public class Cone implements Shape3D {
- private Circle c;
- private double height;
- public Cone(Circle circ, double h){
- c = circ;
- height = h;
- }
- public double getRadius(){
- return c.getRadius();
- }
- public double getVolume(){
- //πr2 h/3
- return c.area() + height/3;
- }
- public double getSurfaceArea(){
- //πr(r+ sqrt(h2+r2) )
- return c.area() + (Math.PI*c.getRadius())*(Math.sqrt(height*height + c.getRadius()*c.getRadius()));
- }
- }
- /**
- * Created by danie on 10/30/2016.
- */
- public class Sphere implements Shape3D {
- private double radius;
- public Sphere(double r){
- radius = r;
- }
- public double getRadius(){
- return radius;
- }
- public double getVolume(){
- // 4/3 πr3
- return radius*radius*radius*Math.PI*4/3;
- }
- public double getSurfaceArea(){
- //4πr2
- return radius*radius*Math.PI*4;
- }
- }
- /**
- * Created by danie on 10/28/2016.
- */
- public abstract class TwoBases implements Shape3D {
- private Shape base;
- private double height;
- public TwoBases(Shape base, double height){
- this.base = base;
- this.height = height;
- }
- public double getVolume(){
- return base.area() * height;
- }
- public double getSurfaceArea(){
- return height * base.perimeter() + 2 * base.area();
- }
- }
- /**
- * Created by danie on 10/30/2016.
- */
- public class TriangularPrism extends TwoBases{
- public TriangularPrism(Triangle b, double h){
- super(b,h);
- }
- }
- /**
- * Created by danie on 10/30/2016.
- */
- public class Cylinder extends TwoBases{
- private double radius;
- public Cylinder(Circle c, double h){
- super(c,h);
- radius = c.getRadius();
- }
- public double getRadius(){
- return radius;
- }
- }
- /**
- * Created by danie on 10/30/2016.
- */
- public class RectangularPrism extends TwoBases {
- public RectangularPrism(Rectangle base, double height){
- super(base,height);
- }
- }
- /**
- * Created by danie on 10/30/2016.
- */
- public class Cube extends RectangularPrism {
- public Cube(Square s, double h){
- super(s,h);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment