Hello fellow students. I\'m looking for some help on this one. JAVA Code if you
ID: 3575471 • Letter: H
Question
Hello fellow students. I'm looking for some help on this one. JAVA Code if you can.
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.
HERE'S MY CODE
public class Cube {
import java.util.Scanner;
public static void main(String[ ] args)
{
double side01 = 0.0;
double side02 = 0.0;
Scanner kBd = new Scanner(System.in);
Squar square1 = new Squar( ); // create an instance of Squar
System.out.print("Enter side of the Square...");
side01 = kBd.nextDouble();
Squar square2 = new Squar(side01); // create another instance
System.out.print("Enter side of the Square...");
side02 = kBd.nextDouble();
System.out.println("The side of square1 is..." + square1.getSide( ));
System.out.println("The area of square1 is..." + square1.getArea( ));
System.out.println("The perimeter of square1 is ... " + square1.getPerimeter( ));
System.out.println("The side of square2 is..." + square2.getSide( ));
System.out.println("The area of square2 is..." + square2.getArea( ));
System.out.println("The perimeter of square2 is ... " + square2.getPerimeter( ));
System.out.print("Enter side of the Square...");
side02 = kBd.nextDouble();
square1.setSide(side02);
System.out.println("The side of square1 is..." + square1.getSide( ));
System.out.println("The area of square1 is..." + square1.getArea( ));
System.out.println("The perimeter of square1 is ... " + square1.getPerimeter( ));
}
}
===================================================================
ALSO MY CODE
public class Squar
{
private double side1 = 0.0; // contents 0 - 99.0
public Squar()
{
side1 = 2.0;
}
public Squar(double s)
{
side1 = s;
}
public double getArea()
{
return side1 * side1;
}
public double getSide()
{
return side1;
}
public double getPerimeter(){
return side1 * 4;
}
public void setSide(double side02) {
side1 = side02;
}
}
THANKS
Explanation / Answer
public static void main(String[ ] args)
{
double side01 = 0.0;
Scanner kBd = new Scanner(System.in);
int choice;
Squar square1=null;
do{
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.println("Enter choice:");
choice=kbd.nextInt();
switch(choice){
case 1: square1=new Squar();
System.out.println("Cube Created.");
break;
case 2: System.out.println("Enter side 1:");
side01=kbd.nextDouble();
square1=new Squar(side01);
System.out.println("Cube Created.");
break;
case 3: System.out.println("Volume is:"+(square1.getSide()*square1.getSide()*square1.getSide());
break;
case 4: System.out.println("Surface is:"+(6*square1.getArea());
break;
case 5: System.out.println("Side is:"+square1.getSide());
break;
case 6: System.out.print("Enter new side:");
int s=kbd.nextDouble();
square1.setSide(s);
System.out.println("Side changed"); break;
}
}while(choice!=7);
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.