Cube
/* Programmer: Nerissa Agapay
Subject: IT134_Computer Programming 3
Instructor: Mr. Dony Dongiapon
Date Started: 02/04/09
Date Finished: 02/04/09
Purpose: To make a cube class that both constructor and method are private. Also to calculate volume and area of a cube.
import java.util.Scanner;
public class Cube {
Scanner scan = new Scanner(System.in);
//fields
private double width;
private double height;
private double length;
private double volume;
private double area;
//constructor with parameter
public Cube(double w,double h,double l){
width=w;
height=h;
length=l;
this.volume();
this.area();
}
//constructor without parameter
public Cube(){
System.out.print("\nEnter a width of a cube: ");
double a=scan.nextDouble();
System.out.print("\nEnter a height of a cube: ");
double b=scan.nextDouble();
System.out.print("\nEnter a length of a cube: ");
double c=scan.nextDouble();
this.setDimension(a,b,c);
this.volume();
this.area();
}
//method 1
private double volume(){
volume=width*height*length;
return volume;
}
//method 2
private double area(){
area=length*width;
return area;
}
public void setDimension(double wid, double hyt, double lent){
width = wid;
height = hyt;
length = lent;
}
// display the area and volume
public void displayCube(){
System.out.println("\nThe volume of a cube is "+volume);
System.out.println("\nThe area of a cube is "+area);
}
}
CubeTester
Program name: CubeTester
Subject: IT134_Computer Programming 3
Instructor: Mr. Dony Dongiapon
Date Started: 02/04/09
Date Finished: 02/04/09
Purpose: To make a CubeTester main class that would access the constructor and method of Cube
*/
public class CubeTester {
public static void main(String[] args){
Cube myCube = new Cube(3.0,4.0,6.0);
myCube.displayCube();
Cube myNewCube = new Cube();
myNewCube.displayCube();
}
}
/* Programmer: Nerissa Agapay
Program name: CubeTester
Subject: IT134_Computer Programming 3
Instructor: Mr. Dony Dongiapon
Date Started: 02/04/09
Date Finished: 02/04/09
Purpose: To make a CubeTester main class that would access the constructor and method of Cube
*/
public class CubeTester {
public static void main(String[] args){
Cube myCube = new Cube(3.0,4.0,6.0);
myCube.displayCube();
Cube myNewCube = new Cube();
myNewCube.displayCube();
}
}