Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* Hugh Gordon
- * Cube set & get Exercise
- * This class file defines the Cube class.
- * It defines the methods and properties of a Cube object.
- */
- public class Cube {
- // Properties are private so they can only be retrieved via the get methods
- private double side;
- private double volume;
- private double surfaceArea;
- private String color;
- private boolean isFilled;
- private final int FACES = 6; // How many sides (faces) the cube has, used in calcSurfaceArea()
- public Cube() { // Constructor for the cube object. Creates a default cube.
- side = 1.0;
- calcVolume(side);
- calcSurfaceArea(side);
- color = "Black";
- isFilled = false;
- }
- public double getSide() { return side; } // Return the side property
- public void setSide(double s) { side = s; } // Set the side property to the provided value
- public String getColor() { return color; } // Return the color property
- 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
- public void setFill(boolean bool) { isFilled = bool; } // Set whether the cube is filled or not
- public boolean isFilled() { return isFilled; } // Return whether the cube is filled ornot
- public double getSurfaceArea() { return surfaceArea; } // Return the surface area
- public double getVolume() { return volume; } // Return the volume
- 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
- 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
- }
Advertisement
Add Comment
Please, Sign In to add comment