Question

In: Computer Science

8. Complete the program that calculates the volume of a cube. If the side length entered...

8. Complete the program that calculates the volume of a cube. If the side length entered for the cube is negative, the program should display an error message saying the length should be positive. If the side length entered for the cube is greater than 100, the program should print a message saying the side is too big. Otherwise the program should calculate and print the volume of the cube of the given side length.

Sample program runs are shown below: import java.util.Scanner; public class CubeVolume { public static void main(){ Scanner reader = new Scanner(System.in); System.out.print("Enter the cube side length: "); double side = reader.nextDouble();

Solutions

Expert Solution

Java code(The given code is in java):

import java.util.Scanner;
public class CubeVolume{
    public static void main(String[] args){
        Scanner reader=new Scanner(System.in);
        //accepting cube side length
        System.out.print("Enter the cube side length: ");
        //reading it
        double side=reader.nextDouble();
        //checking if it is negative
        if(side<0)
            //printing it should be positive
            System.out.println("The length should be positive.");
        //checking if length of the side is 100
        else if(side>100)
            //printing side is too big
            System.out.println("The side is too big.");
        else
            //printing volume
            System.out.println("The volume of cube of length "+side+" is "+(side*side*side));
   }
}


Screenshot:


Input and Output:


Related Solutions

- The cube length is 3.23 cm. What is the volume of the cube? _ ml...
- The cube length is 3.23 cm. What is the volume of the cube? _ ml - How many significant figures should be in the calculated volume? - The measured mass of the cube is 43.808 g. Using that number combined with whatever the volume is, determine the cube's density (in g/mL) ? - Based on the density that was calculated, which type of plastic was used to make the cube? Low Density Polyethylene, High Density Polethylene, Nylon, Polystyrene, PVC,...
Write most basic C++ program possible that calculates volume and surface area of five shapes (Cube,...
Write most basic C++ program possible that calculates volume and surface area of five shapes (Cube, Sphere, Prism, Cylinder, Cone), where each shape has its own class (with a volume function, surface area function, constructor, as well as get and set functions), and the shape dimensions are private class members. Include input and display functions.
A cube with side length L, and density ρC sits at rest on a bathroom scale...
A cube with side length L, and density ρC sits at rest on a bathroom scale at the bottom of a large tank. The tank is partially filled with a fluid having density ρf, leaving the cube partially submerged. A side profile view of the cube shows the cube face a distance d outside of the fluid, and a distance L-d submerged under the fluid. Determine the distance dwhen ρf= 2000 kg/m3, ρC= 4000 kg/m3and L = .50 m. Write...
Write a program named MakeChange that calculates and displays the conversion of an entered number of...
Write a program named MakeChange that calculates and displays the conversion of an entered number of dollars into currency denominations—twenties, tens, fives, and ones. For example, if 113 dollars is entered, the output would be twenties: 5 tens: 1 fives: 0 ones: 3. Answer in C# (P.S i'm posting the incomplete code that i have so far below.) using System; using static System.Console; class MakeChange { static void Main() { int twenties, tens, fives, ones; WriteLine("Enter the number of dollars:");...
Calculate the necessary heat to take an ice cube (side length = 2.54cm) from -40.0degreesC to135.0...
Calculate the necessary heat to take an ice cube (side length = 2.54cm) from -40.0degreesC to135.0 degrees C. Calculate the heat necessary to take the ice to melting point.
Write a program that reads the radius of a sphere and calculates it's volume and surface...
Write a program that reads the radius of a sphere and calculates it's volume and surface area. Format results to 4 decimal places. The radius=3.2 Your program should output volume and surface area.  Repeat same steps for program2, but this time use the following input values: a= ꟷ2.4 , b = 4.5 . Your program should output sum, Fun1, and Fun2. It's for C++
Write a program that calculates mean and standard deviation for four user entered values. The most...
Write a program that calculates mean and standard deviation for four user entered values. The most common measures of a sample are the mean and the standard deviation. the mean is the sum of the values divided by the number of elements in the data set. The dispersion - or spread in the values - is measured by the standard deviation The equation for the mean is x¯ = x1 + x2 + · · · + xn The equation...
1. A cube (length of each side: 10 cm), made from some unknown material, floats on...
1. A cube (length of each side: 10 cm), made from some unknown material, floats on the surface of the ocean. If 72% of the total volume of this cube is underwater, determine: a) the total mass of water displaced by the cube b) the buoyant force acting on the cube c) the total mass of the object d) the density of the object
Write a program that reads in the radius and length of a cylinder and computes volume...
Write a program that reads in the radius and length of a cylinder and computes volume using the following formulas: area = radius * radius * PI volume = area * length
jgrasp environment, java write a complete program that calculates a restaurant bill. Prompt the user for...
jgrasp environment, java write a complete program that calculates a restaurant bill. Prompt the user for the check amount, then ask the user what type of tipp they would like to leave: the choices are 1 for good tip (20%), 2 for an average tip (15%), or 3 for poor tip (10%). Use their input and an if-else to calculate the tip amount. Then calculate and output the final bill: check+tip print out the bill, exactly as shown (print the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT