daniel_lawson9999

3D shape

Oct 31st, 2016
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.66 KB | None | 0 0
  1. /**
  2. * Created by danie on 10/28/2016.
  3. */
  4. public interface Shape3D {
  5. public double getVolume();
  6. public double getSurfaceArea();
  7. }
  8. /*
  9. Shape3D interface
  10. Things with two bases | sphere | cones
  11. rectangular prism | triangular prism | cylinders|
  12. cube|
  13.  
  14.  
  15.  
  16. */
  17. /**
  18. * Created by danie on 10/30/2016.
  19. */
  20. public class Cone implements Shape3D {
  21. private Circle c;
  22. private double height;
  23. public Cone(Circle circ, double h){
  24. c = circ;
  25. height = h;
  26. }
  27. public double getRadius(){
  28. return c.getRadius();
  29. }
  30. public double getVolume(){
  31. //πr2 h/3
  32. return c.area() + height/3;
  33. }
  34. public double getSurfaceArea(){
  35. //πr(r+ sqrt(h2+r2) )
  36. return c.area() + (Math.PI*c.getRadius())*(Math.sqrt(height*height + c.getRadius()*c.getRadius()));
  37. }
  38. }
  39.  
  40.  
  41. /**
  42. * Created by danie on 10/30/2016.
  43. */
  44. public class Sphere implements Shape3D {
  45. private double radius;
  46. public Sphere(double r){
  47. radius = r;
  48. }
  49. public double getRadius(){
  50. return radius;
  51. }
  52.  
  53. public double getVolume(){
  54. // 4/3 πr3
  55. return radius*radius*radius*Math.PI*4/3;
  56. }
  57. public double getSurfaceArea(){
  58. //4πr2
  59. return radius*radius*Math.PI*4;
  60. }
  61. }
  62.  
  63. /**
  64. * Created by danie on 10/28/2016.
  65. */
  66. public abstract class TwoBases implements Shape3D {
  67. private Shape base;
  68. private double height;
  69. public TwoBases(Shape base, double height){
  70. this.base = base;
  71. this.height = height;
  72. }
  73. public double getVolume(){
  74. return base.area() * height;
  75. }
  76. public double getSurfaceArea(){
  77. return height * base.perimeter() + 2 * base.area();
  78. }
  79. }
  80. /**
  81. * Created by danie on 10/30/2016.
  82. */
  83. public class TriangularPrism extends TwoBases{
  84. public TriangularPrism(Triangle b, double h){
  85. super(b,h);
  86. }
  87. }
  88. /**
  89. * Created by danie on 10/30/2016.
  90. */
  91. public class Cylinder extends TwoBases{
  92. private double radius;
  93. public Cylinder(Circle c, double h){
  94. super(c,h);
  95. radius = c.getRadius();
  96. }
  97. public double getRadius(){
  98. return radius;
  99. }
  100. }
  101. /**
  102. * Created by danie on 10/30/2016.
  103. */
  104. public class RectangularPrism extends TwoBases {
  105. public RectangularPrism(Rectangle base, double height){
  106. super(base,height);
  107. }
  108. }
  109. /**
  110. * Created by danie on 10/30/2016.
  111. */
  112. public class Cube extends RectangularPrism {
  113. public Cube(Square s, double h){
  114. super(s,h);
  115. }
  116. }
Advertisement
Add Comment
Please, Sign In to add comment