Java Code Example: volume and surface of a cube

This Java program calculates the volume and surface area of a cube given its edge length. The program defines two static methods, getVolume and getSurface, which compute the volume and surface area, respectively. The main method takes the edge length as input from the user and then calls these methods to display the results.

Code Example

import java.util.Scanner;

public class VolumeAndSurfaceExample {
    static double getVolume(double a) {
        double volume;
        volume = a * a * a;
        return volume;
    }

    static double getSurface(double a) {
        double surface;
        surface = 6 * a * a;
        return surface;
    }

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        double edgeLength;

        System.out.println("Please enter the edge length: ");
        edgeLength = scan.nextInt();

        scan.close();
    
        System.out.println("Volume: " + getVolume(edgeLength));
        System.out.println("Surface: " + getSurface(edgeLength));
    }
}
Output
Please enter the edge length: 
5
Volume: 125.0
Surface: 150.0

Code Explanation

Class Definition

import java.util.Scanner;

public class VolumeAndSurfaceExample {

This part of the code imports the Scanner class from the java.util package and defines the public class VolumeAndSurfaceExample.

Static Method: getVolume

static double getVolume(double a) {
    double volume;
    volume = a * a * a;
    return volume;
}
Description

The getVolume method calculates the volume of a cube.

Parameters
  • a (double): The edge length of the cube.
Implementation
  • Step 1: Declare a local variable volume.
  • Step 2: Calculate the volume using the formula a3a^3a3 (cube of the edge length).
  • Step 3: Return the calculated volume.

Static Method: getSurface

static double getSurface(double a) {
    double surface;
    surface = 6 * a * a;
    return surface;
}
Description

The getSurface method calculates the surface area of a cube.

Parameters
  • a (double): The edge length of the cube.
Implementation
  • Step 1: Declare a local variable surface.
  • Step 2: Calculate the surface area using the formula 6a26a^26a2 (six times the square of the edge length).
  • Step 3: Return the calculated surface area.

Main Method

public static void main(String[] args) {
    Scanner scan = new Scanner(System.in);
    double edgeLength;

    System.out.println("Please enter the edge length: ");
    edgeLength = scan.nextInt();

    scan.close();

    System.out.println("Volume: " + getVolume(edgeLength));
    System.out.println("Surface: " + getSurface(edgeLength));
}
Description

The main method is the entry point of the program. It reads user input for the edge length of the cube, calculates the volume and surface area, and prints the results.

Implementation
  • Step 1: Create a Scanner object to read input from the console.
  • Step 2: Declare a variable edgeLength to store the edge length of the cube.
  • Step 3: Prompt the user to enter the edge length and read the input using nextInt() method.
  • Step 4: Close the Scanner object to free resources.
  • Step 5: Call getVolume method with edgeLength as argument and print the result.
  • Step 6: Call getSurface method with edgeLength as argument and print the result.