/*Programmer Name: Jay S. Gil
Program Title: Cube ManipulationDate:
February 2009
Purpose: To classes using Modifiers
*/
import java.util.Scanner;
public class Cube{
private double width;
private double length;
private double height;
private double volume;
private double area;
public Cube(double w, double h, double l) {
width=w; height=h; length=l;
} public Cube()
{
} private double volume()
{ return (width*length*height);
}
private double area()
{ return (width*length);
} public void setDimension(double newwidth, double newheight, double newlength)
{ width=newwidth;
height=newheight;
length=newlength; }
public String displayCube()
{ return String.format(volume()+" and "+area());
}
}
---------------------------------------------------------------------------------------
/*Programmer Name: Jay S. Gil
Program Title: Cube Manipulation
Date: February 2009
Purpose: To create a tester that test the cube class
*/
import java.util.Scanner;
class CubeTester{
public static void main(String args[]){
double l;
double w;
double h;
System.out.println("Constructor with a parameter");
Cube newCube=new Cube(1,2,3);;
newCube.displayCube();
System.out.println("Cube object constructor without parameter");
Scanner a=new Scanner(System.in);
System.out.println("Enter value of length:");
l=a.nextDouble();
Scanner b=new Scanner(System.in);
System.out.println("Enter value of height:");
w=b.nextDouble();Scanner
c=new Scanner(System.in);
System.out.println("Enter value of width:");
h=c.nextDouble();
System.out.println("The Cube object without a parameter");
Cube secondCube=new Cube();
secondCube.setDimension(l,w,h);
secondCube.displayCube();
}
}
No comments:
Post a Comment