Question: Identify the output of following Java code. Paste the screenshot of output. Justify the syntax. File Name:CubeDemo.java import java.util.Scanner; /** This program demonstrates passing arguments
- Identify the output of following Java code. Paste the screenshot of output. Justify the syntax.
File Name:CubeDemo.java
import java.util.Scanner;
/**
This program demonstrates passing arguments to a
superclass constructor.
*/
public class CubeDemo
{
public static void main(String[] args)
{
double length; // The cube's length
double width; // The cube's width
double height; // The cube's height
// Create a Scanner object for keyboard input.
Scanner keyboard = new Scanner(System.in);
// Get cube's length.
System.out.println("Enter the following " +
"dimensions of a cube:");
System.out.print("Length: ");
length = keyboard.nextDouble();
// Get the cube's width.
System.out.print("Width: ");
width = keyboard.nextDouble();
// Get the cube's height.
System.out.print("Height: ");
height = keyboard.nextDouble();
// Create a cube object and pass the
// dimensions to the constructor.
Cube myCube =
new Cube(length, width, height);
// Display the cube's properties.
System.out.println("Here are the cube's " +
"properties.");
System.out.println("Length: " +
myCube.getLength());
System.out.println("Width: " +
myCube.getWidth());
System.out.println("Height: " +
myCube.getHeight());
System.out.println("Base Area: " +
myCube.getArea());
System.out.println("Surface Area: " +
myCube.getSurfaceArea());
System.out.println("Volume: " +
myCube.getVolume());
}
}
File Name:Cube.java
/**
This class holds data about a cube.
*/
public class Cube extends Rectangle
{
private double height; // The cube's height
/**
The constructor sets the cube's length,
width, and height.
@param len The cube's length.
@param w The cube's width.
@param h The cube's height.
*/
public Cube(double len, double w, double h)
{
// Call the superclass constructor.
super(len, w);
// Set the height.
height = h;
}
/**
The getHeight method returns the cube's height.
@return The value in the height field.
*/
public double getHeight()
{
return height;
}
/**
The getSurfaceArea method calculates and
returns the cube's surface area.
@return The surface area of the cube.
*/
public double getSurfaceArea()
{
return getArea() * 6;
}
/**
The getVolume method calculates and
returns the cube's volume.
@return The volume of the cube.
*/
public double getVolume()
{
return getArea() * height;
}
}
File Name: Rectangle.java
public class Rectangle
{
private double length;
private double width;
/**
Constructor
@param len The length of the rectangle.
@param w The width of the rectangle.
*/
public Rectangle(double len, double w)
{
length = len;
width = w;
}
/**
The setLength method stores a value in the
length field.
@param len The value to store in length.
*/
public void setLength(double len)
{
length = len;
}
/**
The setWidth method stores a value in the
width field.
@param w The value to store in width.
*/
public void setWidth(double w)
{
width = w;
}
/**
The getLength method returns a Rectangle
object's length.
@return The value in the length field.
*/
public double getLength()
{
return length;
}
/**
The getWidth method returns a Rectangle
object's width.
@return The value in the width field.
*/
public double getWidth()
{
return width;
}
/**
The getArea method returns a Rectangle
object's area.
@return The product of length times width.
*/
public double getArea()
{
return length * width;
}
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
