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

I\'m having a bit of trouble with this one. Here\'s what I\'m supposed to do: Wr

ID: 3552122 • Letter: I

Question

I'm having a bit of trouble with this one. Here's what I'm supposed to do:


Write a Cube class that has the following fields:
   edgeLength: a double representing the edge length of the cube.
The class should have the following methods:
Constructor. Accepts the edge length of the cube as an argument.
Constructor. A no-arg constructor that sets the edgeLength field to 0.0.
setEdgeLength. A mutator method for the edgeLength field.
     getEdgeLength. A accessor method for the edgeLength field.
     getSurfaceArea. Returns the surface area of the cube, which is calculated as surface area = 6 * edgeLength * edgeLength
   getVolume. Returns the volume of the cube, which is calculated as volume = edgeLength * edgeLength * edgeLength
   getFaceDiagonal. Returns the face diagonal of the cube, which is calculated as face diagonal = ?2 * edgeLength

You also need to write a separate program called CubeDemo.java to test the class (all methods).


I can't seem to create the constructor without having errors slapped in my face. Can you help?

Explanation / Answer

import java.util.*;

//Write a Cube class that has the following fields:
class Cube
{
private double edgeLength; //: a double representing the edge length of the cube.
//The class should have the following methods:
//Constructor. Accepts the edge length of the cube as an argument.
public Cube(double edgeLength)
{
this.edgeLength = edgeLength;
}
//Constructor. A no-arg constructor that sets the edgeLength field to 0.0.
public Cube()
{
this.edgeLength = 0.0;
}
public void setEdgeLength(double edgeLength)// A mutator method for the edgeLength field.
{
this.edgeLength = edgeLength;
}
public double getEdgeLength() // A accessor method for the edgeLength field.
{
return edgeLength;
}
public double getSurfaceArea()// Returns the surface area of the cube, which is calculated as surface area = 6 * edgeLength * edgeLength
{
return 6 * edgeLength * edgeLength;
}
public double getVolume()// Returns the volume of the cube, which is calculated as volume = edgeLength * edgeLength * edgeLength
{
return edgeLength * edgeLength * edgeLength;
}
public double getFaceDiagonal()// Returns the face diagonal of the cube, which is calculated as face diagonal = ?2 * edgeLength
{
return Math.sqrt(2)*edgeLength;
}
}

//You also need to write a separate program called CubeDemo.java to test the class (all methods).
public class CubeDemo
{
public static void main(String[] args)
{
//testing one argument constructor
Cube cube_1 = new Cube(6);

System.out.println("Cube 1 edge given by "+cube_1.getEdgeLength());
//testing no argument constructor
Cube cube_2 = new Cube();

System.out.println("Cube 2 edge given by "+cube_2.getEdgeLength());
//testing setEdgeLength method
cube_2.setEdgeLength(5);
//testing getEdgeLength() method;
System.out.println("Cube 2 edge given by "+cube_2.getEdgeLength());
//testing getSurfaceArea() method;
System.out.println("Cube 2 Surface Area given by "+cube_2.getSurfaceArea());
//testing getVolume() method;
System.out.println("Cube 2 Volume given by "+cube_2.getVolume());
//testing getFaceDiagonal() method;
System.out.println("Cube 2 Face Diagonal given by "+cube_2.getFaceDiagonal());
}
}

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