The program should be able to create (instantiate) three types of boxes. The fir
ID: 3859666 • Letter: T
Question
The program should be able to create (instantiate) three types of boxes. The first box would use the default constructor. The second box would be a cube and only need one parameter. The third box would be a rectangular solid and you will send in three parameters to the constructor You should be able to perform the following calculations on your box: surface area of the entire box and volume. Furthermore, you should be able to make your box larger and smaller in the following two different ways: • Specify by how much larger (multiplier) you would like to enlarge each dimension of the object. For example, makeLarger(2) would multiply the length, width, and height of the box by 2. • Specify the specific amount you would like to add to each dimension of the box. For example, makeLarger(1, 3, 5) would add 1 to the length, 3 to the width, and 5 to the height of the box. Be sure to include methods for the following tasks: 1. Constructors (three) 2. Calculate and return the volume 3. Calculate and return the surface area 4. Enlarge the box by multiplying each side by a number 5. Enlarge the box by adding a number to each side 6. Output the dimensions Demonstrate that your program works properly. You will need to test all of the constructors and all of the methods in your class. Display all results to two decimal places. Input and output should be done with Dialog and Message boxes. Your program should be well documented internally and externally.
Explanation / Answer
Box.java
/** Box Assignment */
class Box{
// declaring data members
private float length,breadth,height;
// Accessor for length
public float getLength() {
return length;
}
// Accessor for breadth
public float getBreadth(){
return breadth;
}
//Accessor for height
public float getHeight(){
return height;
}
//declaring a default constructor for a Cuboid Box
public Box() {
length=0f;
breadth=0f;
height=0f;
}
//constructor for a Cube Box
public Box(float side){
length=side;
breadth=side;
height=side;
}
//parameterised constructor for a Cuboid Box
public Box(float l,float b,float h){
length=l;
breadth=b;
height=h;
}
//method computes volume of a Box
public float volume(){
return length*breadth*height;
}
//method computes surface area of a Box
public float surfaceArea(){
return 2*(length*breadth + breadth*height + height*length);
}
//method for Box enlarger with a single parameter
public void makeLarger(int x){
length *=x;
breadth *=x;
height *=x;
}
//method for Box enlarger with three parameters
public void makeLarger(int l,int b,int h){
length +=l;
breadth +=b;
height +=h;
}
}
---------------------------------
TestBox.java
import javax.swing.JOptionPane; //import the class JOptionPane
public class TestBox
{
public static void main(String args[])
{
String lstr,bstr,hstr; // length, breadth, height strings entered by user
float l,b,h; //length, breadth, height of the Box
double SA,V; // surface area and volume of the Box
System.out.println(" CUBOID BOX DIMENSIONS ");
//accept length, breadth and height of cuboid box as Strings
lstr=JOptionPane.showInputDialog(" Enter Box length :");
bstr=JOptionPane.showInputDialog(" Enter Box breadth :");
hstr=JOptionPane.showInputDialog(" Enter Box height :");
//convert length, breadth and height of Box from String to float datatype
l=Float.parseFloat(lstr);
b=Float.parseFloat(bstr);
h=Float.parseFloat(hstr);
//Create a box object b1 using Box class
Box b1=new Box(l,b,h);
//compute box volume
V=b1.volume();
//compute box surface area
SA=b1.surfaceArea();
// Output Box dimensions, volume and surface Area using Message dialog box
String msg = " Length : "+b1.getLength() +" Breadth :" +b1.getBreadth()+" Height :"+b1.getHeight() +" Volume :"+ V +" Surface Area "+SA;
JOptionPane.showMessageDialog(null,msg,"CUBOID BOX",JOptionPane.PLAIN_MESSAGE);
System.out.println("CUBE BOX DIMENSIONS");
//accept side of CUBE as string
lstr =JOptionPane.showInputDialog("Enter Box side :");
//convert String to float data type
l=Float.parseFloat(lstr);
//Create a cube box object b2 using Box class
Box b2=new Box(l);
//compute Box volume
V=b2.volume();
//compute box surface area
SA=b2.surfaceArea();
// Output Box dimensions, volume and surface Area using Message dialog box
String msg1 = " Length : "+b2.getLength() +" Breadth :" +b2.getBreadth()+" Height :"+b2.getHeight() +" Volume :"+ V +" Surface Area "+SA;
JOptionPane.showMessageDialog(null,msg1,"CUBE BOX",JOptionPane.PLAIN_MESSAGE);
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.