| CCS 2100 | | Comp 310 | | CCS 1300 | | CCS 1200 | | CCS 1100 | | CCS 1000 | | CS 212 | | Email: cpuccs@yahoo.com

Tuesday, July 10, 2012

Inheritance Box

The link for Inheritance Box:

http://www.2shared.com/file/dd7MqR6Z/Inheritance_Box.html

This program would ask the user for the length, width and depth of a box, and calculate its volume. Updated with try/catch block and do while loop

Below is the encoded version of the program.

**************** Rectangle.java *************
public class Rectangle{
protected double length;
protected double width;
protected double area;

public Rectangle(){
length = 0;
width = 0;
}

public Rectangle(double l, double w){
length = l;
width = w;
}

public Rectangle(Rectangle other){
length = other.length;
width = other.width;
}

public String toString(){
String str = " ";
area = length * width;
str = "Area: " + area;
return (str);
}

public void setDimensionRectangle(double l, double w){
if (l < 0) length = 0;
else length = l;
if (w < 0) width = 0;
else width = w;
}
}
**************** end of Rectangle.java ************

***************** Box.java *************
public class Box extends Rectangle{
private double depth;
private double volume;

public Box(){
super(0,0);
depth = 0;
}
public Box(double l, double w, double d){
super(l,w);
depth = d;
}

public Box(Box other){
super(other.length, other.width);
depth = other.depth;
}

public Box(Rectangle r, double d){
super(r.length, r.width);
depth = d;
}

public String toString(){
String str = " ";
volume = length * width * depth;
str = "Volume: " + volume;
return (str);
}

public void setDimensionBox(double l, double w, double d){
setDimensionRectangle(l, w);
if (d < 0) depth = 0; else depth = d; } } ***************** end of Box.java **************

***************** TestProgBox.java **************
import java.io.*;

public class TestProgBox{
public static void main(String[] args) throws IOException {
BufferedReader keyboard = new BufferedReader(new InputStreamReader(System.in));

double length = 0;
double width = 0;
double depth = 0;

Box b1 = new Box();
Box b2 = new Box(3,4,5);
Box b3 = new Box(b2);

Rectangle r4 = new Rectangle(5,7);
Box b4 = new Box(r4,8);

System.out.println("b1: " + b1);
System.out.println("b2: " + b2);
System.out.println("b3: " + b3);

System.out.println("b4: " + b4);


System.out.println("Enter length: ");
length = Double.parseDouble(keyboard.readLine());

System.out.println("Enter width: ");
width = Double.parseDouble(keyboard.readLine());

System.out.println("Enter depth: ");
depth = Double.parseDouble(keyboard.readLine());

b1.setDimensionBox(length, width, depth);
System.out.println("b1: " + b1);

}
}
****************** end of TestProgBox.java *********

************** TestProgBox2.java ***********
import java.io.*;

public class TestProgBox2{
public static void main(String[] args) throws IOException {
BufferedReader keyboard = new BufferedReader(new InputStreamReader(System.in));

double length = 0;
double width = 0;
double depth = 0;
boolean done = false;

Box b1 = new Box();
do{
try{
System.out.println("Enter length: ");
length = Double.parseDouble(keyboard.readLine());

System.out.println("Enter width: ");
width = Double.parseDouble(keyboard.readLine());

System.out.println("Enter depth: ");
depth = Double.parseDouble(keyboard.readLine());

b1.setDimensionBox(length, width, depth);
System.out.println("b1: " + b1);
done = true;
} catch (NumberFormatException x1){
System.out.println("Input is number, not letters. Try again.");
}
}while(!done);

}
}
************* end of TestProgBox2.java **********

No comments:

Post a Comment