Wednesday, February 4, 2009

cube solution

/* Programmer:       Maribelle Lacno

 * Program name:    cube

 * Subject:               IT134_Computer Programming 3

 * Instructor:            Mr. Dony Dongiapon

 * Date Started:        02/04/09

 * Date Finished:       02/04/09

 * Purpose:              To create a class that will calculate the volume

 *                                 and area of a  cube inside a private methods.

*/

import javax.swing.JOptionPane;

public class cube{

  private double width;
  private double height;
  private double length;
  private double totalVolume;
  private double totalArea;
    
  //declare a constructor cube with a parameters.
  public cube(double newWidth, double newHeight, double newLength){
   width = newWidth;
   length = newLength;
   height = newHeight;
      
   this.volume();
   this.area();
  }
   
  //declare a constructor cube without a parameters.
  public cube(){
   double newWidth = Integer.parseInt(JOptionPane.showInputDialog("Enter a new width:"));
   double newHeight = Integer.parseInt(JOptionPane.showInputDialog("Enter a new height:"));
   double newLength = Integer.parseInt(JOptionPane.showInputDialog("Enter a new length:"));
        
   this.setDimension(newWidth, newHeight,newLength);
   this.volume();
   this.area();
  }
   
  //create a private method that will calculate and return the volume of a cube.
  private double volume(){
   return totalVolume = length*width*height;
  }
    
  //create a private method that will calculate and return the area of a cube.
  private double area(){
   return totalArea = length*width;
  }
    
  //set a new dimension of your cube
  public void setDimension(double newWidth, double newHeight, double newLength){
   width = newWidth;
   height = newHeight;
   length = newLength;
  }
    
  //display the volume and area of your cube
  public void displayCube(){
   JOptionPane.showMessageDialog(null,"The volume of your cube is "+totalVolume+"\n                  and the area of your cube is "+totalArea);
  }  
   
}

cubeTester

/* Programmer:     Maribelle Lacno
  *Program name:  cubeTester
  *Subject:               IT134_Computer Programming 3
  *Instructor:          Mr. Dony Dongiapon
  *Date Started:      02/04/09
  *Date Finished:    02/04/09
  *Purpose:              To create a cubeTester class that will access                                                             *                               the private methods of the cube class.
*/

public class cubeTester{

 public static void main(String[] args){
      
  cube myCube = new cube(5.0,5.0,5.0);
  myCube.displayCube();
        
  cube myNewCube = new cube();
  myNewCube.displayCube();
 }
   
}

No comments:

Post a Comment