In Java define and implement a class called Cube that contai
In Java, define and implement a class called Cube that contains instance data representing the length of one of its sides as well as its mass. The constructor should use parameters to initialize both these variables; also include mutator and accessor methods for the side length and for the mass. Write a method to calculate and return the density of the cube, and write a driver class to test it.
Solution
HI, Please find my implemetnation.
Please let me know in case of any issue.
######### Cube.java ########
public class Cube {
// instance variables
private double side;
private double mass;
public Cube(double side, double mass) {
this.side = side;
this.mass = mass;
}
public double getSide() {
return side;
}
public double getMass() {
return mass;
}
public void setSide(double side) {
this.side = side;
}
public void setMass(double mass) {
this.mass = mass;
}
public double getVolume(){
return side*side*side;
}
public double getArea(){
return 6*side*side;
}
public double getDensity(){
return mass/getVolume();
}
}
########### CubeTest.java ###########
public class CubeTest {
public static void main(String[] args) {
// creating object of Cube
Cube cube = new Cube(4.5, 12.54);
System.out.println(\"Area: \"+cube.getArea());
System.out.println(\"Volume: \"+cube.getVolume());
System.out.println(\"Density: \"+cube.getDensity());
}
}
/*
Sample run:
Area: 121.5
Volume: 91.125
Density: 0.13761316872427984
*/


