In: Computer Science
JAVA, trying to better learn and use JAVA
//Please note throughout if possible looking to better understand the process.
Write the following class: Box
Define Class Box
Write the class header
Class Variables
Class Box is to have the following private data members:
height of type double
width of type double
length of type double
Constructors
Class Box is to have two constructors with the following specifications:
a no-arg constructor that initializes each double data member to zero
a constructor that takes three parameters, each representing one of the class data members. the arguments are to be listed in the order of (height, width, length)
Get and Get Methods
Class Box is to have standard get/set methods for each data member
Auxiliary Methods
an auxiliary method getVolume( ). The method is to have an access of public and return a value of type double. The method is to do the following:
calculate and return the volume of the box
volume is calculated by multiplying the length of the box by the width of the box by the height of the box
Method Overrides
Class Box is to have an overridden equals() method that does the following:
tests to see if the parameter represents an object (null test)
tests to see if the parameter object is of the same class type as the calling object (class test)
determines if the calling object and the parameter object store identical data values for the corresponding data members (variable to variable test)
Class Box is to have an overridden toString() method that does the following:
displays the following information in the format presented:
Height: display height of the object
Width: display the width of the object
Length: display the length of the object
//-----CODE-----
// Main class. Box class is tested in this class
class Main {
// main function
public static void main(String[] args) {
// creating a Box Object using parameterized constructor
Box b = new Box(10,15,20);
System.out.println("Box b created");
// function call: toString
System.out.println(b.toString());
// function call: getVolume
System.out.println(b.getVolume());
// creating another Box Object using parameterized constructor
Box b1 = new Box(10,10,20);
System.out.println("Box b1 created");
// function call: toString
System.out.println(b1.toString());
// function call: getVolume
System.out.println(b1.getVolume());
System.out.println("Checking if b and b1 are equal");
// function call: equals
System.out.println(b.equals(b1));
}
}
// class Box
class Box{
// private members
private double height;
private double width;
private double length;
// constructor without parameter
public Box(){
this.height=0;
this.width=0;
this.length=0;
}
// constructor with parameter
public Box(double h,double w,double l){
this.height=h;
this.width=w;
this.length=l;
}
// getters and setters
public double getHeight(){
return this.height;
}
public void setHeight(double H){
this.height=H;
}
public double getWidth(){
return this.width;
}
public void setWidth(double W){
this.width=W;
}
public double getLength(){
return this.length;
}
public void setLength(double L){
this.length=L;
}
public double getVolume(){
return this.height*this.width*this.length;
}
// method equals
public Boolean equals(Box b){
Boolean ans= false;
if(this.height==b.getHeight()&&this.width==b.getWidth()&&this.length==b.getLength()){
ans=true;
}
return ans;
}
// method toString
public String toString(){
return "Height: "+this.height+"\nWidth: "+this.width+"\nLength:"+this.length;
}
}
Output
Summary
The program is implemented in Java.
Box class is created with private members, default constructor, parametrized constructor, getters and setters.
Method getVolume and overriding methods equals and toString are also implemented.
Comments and included in code for better understanding.
Code and output are provided.