/* programmer:Cuming Mary Grace
date started: February 4, 2009
date ended: February 4, 2009
Programme name: Solving a Cube
programme purpose: To create classes using Visibility access Modifiers
public class Cube{
private double width;
private double length;
private double height;
private double volume;
private double area;
// constructor
public Cube(double w, double h, double l)
{
width=w;
height=h;
length=l;
}
public Cube()
{
}
private double volume() //method for the volume
{
return (width*length*height);
}
private double area() //method for the area
{
return (width*length);
}
public void setDimension(double nlength, double nHeight, double nWidth)
{
length=nLength;
width=nWidth;
height=nHeight;
}
public void displayCube()
{
System.out.println("The volume of the Cube:" +volume());
System.out.println("The area of the Cube:" +area());
}
}
CubeTester
/* programmer:Cuming Mary Grace
date started: February 4, 2009
date ended: February 4, 2009
Programme name:Solving a Cube
programme purpose: To create classes using Visibility access Modifiers
import java.util.Scanner;
class CubeTester
{
public static void main(String args[])
{
double l;
double w;
double h;
System.out.println("The Cube with a parameter");
Cube newCube=new Cube(1,2,3);;
newCube.displayCube();
System.out.println("The Cube object without a parameter");
Scanner a=new Scanner(System.in);
System.out.println("enter the value of the length:");
l=a.nextDouble();
Scanner b=new Scanner(System.in);
System.out.println("enter the value of the height:");
w=b.nextDouble();
Scanner c=new Scanner(System.in);
System.out.println("enter the value of the width:");
h=c.nextDouble();
System.out.println("The Cube object without a parameter");
Cube secondCube=new Cube();
secondCube.setDimension(l,w,h);
secondCube.displayCube();
}
}
Wednesday, February 4, 2009
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment