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

Write a piece of code that asks the user to enter the length, width, and height

ID: 3726858 • Letter: W

Question

Write a piece of code that asks the user to enter the length, width, and height of a cube. The code should check to see if the dimensions are equal. If they are, let the user know that they have entered the dimensions of a cube and use the formula: 3 volume length to ca ume: volume - length width height. ragral public class Cubes public static void main (String[1 args)

Explanation / Answer

/* In Java , you can use Scanner to read input from user , first instantiate it and after that you can use its methods to read input like Scanner scan=new Scanner(System.in); 1 to rand integer=> scan.nextInt(); 2.to read double => scan.nextDouble(); And to check sides are equal you can use && operator in if condition. Sample Input: Enter Length: 5 Enter Width: 5 Enter Height: 5 Sample Output: You have entered all the side equal The volume of cube =125.0 Sample Input 2: Enter Length: 4 Enter Width: 5 Enter Height: 9 Sample Output 2: The volume of cube =180.0 */ //Please remove all the System.out.println() if not required. //Your code start from below line import java.util.Scanner; public class Cubes { public static void main(String[] args) { //Instantiating Scanner to read input from user Scanner scan=new Scanner(System.in); //Asking user to enter length , width and height of the cube System.out.println("Enter Length: "); double length=scan.nextDouble(); System.out.println("Enter Width: "); double width=scan.nextDouble(); System.out.println("Enter Height: "); double height=scan.nextDouble(); //Comparing all the side , if all are equal if(length==width && length==height) { System.out.println("You have entered all the side equal"); //Calculating volume and displaying //Math.pow(x,y) function of java calculates the x raise to the power y double volume=Math.pow(length,3); //Displaying System.out.println("The volume of cube ="+volume); } else { //Calculating volume of cube double volume=length*width*height; //Displaying System.out.println("The volume of cube ="+volume); } } }

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