Advertisement
JordanFarnell

Cube

Nov 10th, 2016
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.43 KB | None | 0 0
  1. package cube;
  2.  
  3. public class Cube {
  4.  
  5.     private double length = 1, width = 1, depth = 1;
  6.    
  7.     public Cube() {
  8.        
  9.     }
  10.    
  11.     public Cube(double l, double w, double d) {
  12.         length = l;
  13.         width = w;
  14.         depth = d;
  15.     }
  16.    
  17.     public void setLength(double l) {
  18.         length = l;
  19.         getLength();
  20.     }
  21.    
  22.     public void setWidth(double w) {
  23.         width = w;
  24.         getWidth();
  25.     }
  26.    
  27.     public void setDepth(double d) {
  28.         depth = d;
  29.         getDepth();
  30.     }
  31.    
  32.     public double getLength() {
  33.         return(length);
  34.     }
  35.    
  36.     public double getWidth() {
  37.         return(width);
  38.     }
  39.    
  40.     public double getDepth() {
  41.         return(depth);
  42.     }
  43.    
  44.     public double volume() {
  45.         return(length * width * depth);
  46.     }
  47.    
  48.     @Override
  49.     public String toString() {
  50.         String cubeString;
  51.         cubeString = "Cube has volume: " + volume();
  52.         return(cubeString);
  53.     }
  54.    
  55. }
  56.  
  57.  
  58. package cube;
  59.  
  60. public class TestCube {
  61.    
  62.     public static void main(String[] args) {
  63.         Cube cube1 = new Cube(5,5,5);
  64.        
  65.         System.out.println("The length of the cube is: " + cube1.getLength());
  66.         System.out.println("The width of the cube is: " + cube1.getWidth());
  67.         System.out.println("The depth of the cube is: " + cube1.getDepth());
  68.        
  69.         System.out.println(cube1.toString());
  70.     }
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement