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

Hi in Java i need to create a Cube object and test its accessor and mutator meth

ID: 3576870 • Letter: H

Question

Hi in Java i need to

create a Cube object and test its accessor and mutator methods (described below) with a client program.

Notes about processing:

Input - Using a menu system, provide the user with the following choices:

Processing - provide the requested response depending on the menu choice selected. When necessary, prompt for additional input (see below).

Output - if requested, provide the output requested. Should the user request the program stop (see below) terminate the menu and program.

For any test run you should create only a default cube or a cube with the side provided by the user.

A sample session might look like this:

Cube Choices:
1. Create default cube
2. Create cube with side provided by user
3. Show volume of the cube
4. Show surface of cube
5. Show side
6. Change side
7. Quit
Enter choice:

1

Cube created!

Cube Choices:
1. Create default cube
2. Create cube with side provided by user
3. Show volume of the cube
4. Show surface of cube
5. Show side
6. Change side
7. Quit

Enter choice:

3

Volume is 8

Cube Choices:
1. Create default cube
2. Create cube with side provided by user
3. Show volume of the cube
4. Show surface of cube
5. Show side
6. Change side
7. Quit
Enter choice:

4

Surface is 12

Cube Choices:
1. Create default cube
2. Create cube with side provided by user
3. Show volume of the cube
4. Show surface of cube
5. Show side
6. Change side
7. Quit
Enter choice:

5

Side is 2

Cube Choices:
1. Create default cube
2. Create cube with side provided by user
3. Show volume of the cube
4. Show surface of cube
5. Show side
6. Change side
7. Quit
Enter choice:

4

Surface is 24

Cube Choices:
1. Create default cube
2. Create cube with side provided by user
3. Show volume of the cube
4. Show surface of cube
5. Show side
6. Change side
7. Quit
Enter choice:

7

End of program

Notes about the Cube class:

Constructors - Create two (2) constructors. The first should be a "no argument" constructor that initializes the side(s) to a value of 2.0 (declared as a double). The second constructor will initialize the side of the cube to a value entered by the user.

Accessor Methods -

getSide - returns the value of the side of the cube

getVolume - returns the value of the volume of the cube (the volume is the side3).

getSurface - returns the surface of the cube (a cube has 6 sides, the surface will be the 6 * area of a side, or 6 * side2)

Output -

Display the values requested by the user via the menu. Let the sample program above guide you.

Should include two files the client program  This program instantiates the Cube object and contains the main method.

and the file that defines the cube class

Explanation / Answer

Cube.java

public class Cube {
   //Declaring instance variable
   private double side;

   //Default constructor
   public Cube()
   {
   this.side=2.0;
   }

   //Parameterized constructor
   public Cube(double side) {
       this.side = side;
   }
  
   //Setter method and getter method
   public void setSide(double side) {
       this.side = side;
   }

   public double getSide()
   {
       return side;
   }
  
   //This method will calculate the volume of the cube
   public double getVolume()
   {
       return side*side*side;
   }
  
   //This method will calculate the surface area of the cube
   public double getSurface()
   {
       return 6*side*side;
   }

}

______________________

Test.java

import java.util.Scanner;

public class Test {

   public static void main(String[] args) {
       //Declaring variable
       int choice;
       double side;
      
       //Scanner class Object is used to read the inputs entered by the user
       Scanner sc = new Scanner(System.in);
       Cube c = null;
      
       //This while loop continue to execute until the user chooses the option 7
       while (true) {
           System.out.println(" Cube Choices:");
           System.out.println("1. Create default cube");
           System.out.println("2. Create cube with side provided by user");
           System.out.println("3. Show volume of the cube");
           System.out.println("4. Show surface of cube");
           System.out.println("5. Show side");
           System.out.println("6. Change side");
           System.out.println("7. Quit");

           System.out.print("Enter choice:");
           choice = sc.nextInt();
           switch (choice) {
           case 1: {
               //Creating the Cube class object
               c = new Cube();
               continue;
           }
           case 2: {
              
               //Getting the length of the side
               System.out.print("Enter the length of the side :");
               double length = sc.nextDouble();
              
               //Creating the cube class object by passing the side as argument
               c = new Cube(length);
               continue;
           }
           case 3: {
              
               //Displaying the volume of the cube
               System.out.println("Volume is :" + c.getVolume());
               continue;
           }
           case 4: {
              
               //Displaying the surface area of the cube
               System.out.println("Surface is :" + c.getSurface());
               continue;
           }
           case 5: {
               //Displaying the side of the cube
               System.out.println("Side is :" + c.getSide());
               continue;
           }
           case 6: {
              
               //Getting the side of the cube entered by the user
               System.out.print("Enter the side :");
               side = sc.nextDouble();
              
               //Setting the side of the cube
               c.setSide(side);
               continue;
           }
           case 7: {
               System.out.println(":: Program Exit ::");
               break;
           }

           }
           break;

       }
   }

}

_________________________

Output:


Cube Choices:
1. Create default cube
2. Create cube with side provided by user
3. Show volume of the cube
4. Show surface of cube
5. Show side
6. Change side
7. Quit
Enter choice:1

Cube Choices:
1. Create default cube
2. Create cube with side provided by user
3. Show volume of the cube
4. Show surface of cube
5. Show side
6. Change side
7. Quit
Enter choice:3
Volume is :8.0

Cube Choices:
1. Create default cube
2. Create cube with side provided by user
3. Show volume of the cube
4. Show surface of cube
5. Show side
6. Change side
7. Quit
Enter choice:4
Surface is :24.0

Cube Choices:
1. Create default cube
2. Create cube with side provided by user
3. Show volume of the cube
4. Show surface of cube
5. Show side
6. Change side
7. Quit
Enter choice:7
:: Program Exit ::

_______________Thank You

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