KHD

Cube

KHD
Sep 23rd, 2013
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.80 KB | None | 0 0
  1. /* Hugh Gordon
  2.  * Cube set & get Exercise
  3.  * This class file defines the Cube class.
  4.  * It defines the methods and properties of a Cube object.
  5.  */
  6.  
  7. public class Cube {
  8.   // Properties are private so they can only be retrieved via the get methods
  9.   private double side;
  10.   private double volume;
  11.   private double surfaceArea;
  12.   private String color;
  13.   private boolean isFilled;
  14.   private final int FACES = 6; // How many sides (faces) the cube has, used in calcSurfaceArea()
  15.  
  16.   public Cube() { // Constructor for the cube object. Creates a default cube.
  17.     side = 1.0;
  18.     calcVolume(side);
  19.     calcSurfaceArea(side);
  20.     color = "Black";
  21.     isFilled = false;
  22.   }
  23.  
  24.   public double getSide() { return side; } // Return the side property
  25.  
  26.   public void setSide(double s) { side = s; } // Set the side property to the provided value
  27.  
  28.   public String getColor() { return color; } // Return the color property
  29.  
  30.   public void setColor(String str) { color = str.substring(0,1).toUpperCase().concat(str.substring(1).toLowerCase()); } // Sets the color to the provided string with a capital first letter and the rest in lowercase
  31.  
  32.   public void setFill(boolean bool) { isFilled = bool; } // Set whether the cube is filled or not
  33.  
  34.   public boolean isFilled() { return isFilled; } // Return whether the cube is filled ornot
  35.  
  36.   public double getSurfaceArea() { return surfaceArea; } // Return the surface area
  37.  
  38.   public double getVolume() { return volume; } // Return the volume
  39.  
  40.   public void calcVolume(double s) { volume = Math.pow(s, 3); } // Calculate the volume (side^3) of the cube and set it to the volume property
  41.  
  42.   public void calcSurfaceArea(double s) { surfaceArea = Math.pow(s, 2) * FACES; } // Calculate the SA (side^2 * faces) of the cube and set it to the surfaceArea property
  43. }
Advertisement
Add Comment
Please, Sign In to add comment