Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a Java class definition for a Cube object, that has an integer attribute f

ID: 3871047 • Letter: W

Question

Write a Java class definition for a Cube object, that has an integer attribute for the length of its side. The object should be capable of reporting its surface area and volume. The surface area of a cube is six times the area of any side. The volume is calculated by cubing the side.

Write a Java class definition for a CubeUser object that will use the Cube object. This class should create three Cube instances, each with a different side, and then report their respective surface areas and volumes.

Write an override for the toString() method.

Explanation / Answer

Write a Java class definition for a Cube object, that has an integer attribute for the length of its side. The object should be capable of reporting its surface area and volume. The surface area of a cube is six times the area of any side. The volume is calculated by cubing the side.

public class Cube

{

int side;

Cube(int side)

{

this.side=side;

int vol=volume(side);

System.out.println("volume of cube is"+ vol);

int sa=surfaceArea(side);

System.out.println(" Surface area of cube is "+sa);

}

public int volume(int side)

{

int vol=side*side*side;

return vol;

}

public int surfaceArea(int side)

{

int sa=6*side*side;

return sa;

}

public static void main(String args[])

{

Cube c= new Cube(3);

}

}

output:

****************************************************************************************************

Write a Java class definition for a CubeUser object that will use the Cube object. This class should create three Cube instances, each with a different side, and then report their respective surface areas and volumes.

class Cube

{

int side;

Cube(int side)

{

this.side=side;

System.out.println(" when side is " +side);

int vol=volume(side);

System.out.println("volume of cube is"+ vol);

int sa=surfaceArea(side);

System.out.println(" Surface area of cube is "+sa);

}

public int volume(int side) //finding volume

{

int vol=side*side*side;

return vol;

}

public int surfaceArea(int side) // finding surface area

{

int sa=6*side*side;

return sa;

}

}

public class CubeUser extends Cube //using cube class
{
public static void main(String args[])
{
CubeUser c= new CubeUser(4); //creating object one
CubeUser c1= new CubeUser(5); //creating object one
CubeUser c2= new CubeUser(6); //creating object one
}
  
CubeUser(int side)
{
super(side);
}
  
}

output:

****************************************************************************************************

Write an override for the toString() method.

public class Stringoverride

{
int roll_number;
String name;

Stringoverride(int roll_number, String name){
this.roll_number=roll_number;
this.name=name;
  
}

public String toString()

{ //overriding the toString() method
return roll_number+" "+name;
}
public static void main(String args[]){
Stringoverride s1=new Stringoverride(51,"Pavan");
Stringoverride s2=new Stringoverride(52,"Sai");

System.out.println(s1); //compiler writes here s1.toString()
System.out.println(s2); //compiler writes here s2.toString()
}
}

output:

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote