Java Program to Calculate Surface Area and Volume of Cube

Here is a java program to find the volume and surface area of a cube. In this Java program, we have to find the volume and total surface area of a cube, given the length of a side of cube.

A cube is a three dimensional object having equal width, height, and length measurements. A cube has twelve edges, six square faces and eight vertices. Each face of a cube is a square of same side length and all three adjacent faces are perpendicular to each other.

Total Surface Area of Cube
The total surface area of cube is the total area on the outer side of a cube. A cube has six square surfaces of equal area. Hence, to find the total surface area of a cube we should find the area of one square surface and then multiply it with 6 to get total surface area.
Area of a Face of Cube = Side X Side
Total Surface area of Cube = 6 X Side X Side
Where, Side is the length of any edge of a cube.
Volume of Cube
The volume of a cube is the amount of three dimensional space occupied by the cube. Volume of a cube is the number of cubic units that will exactly fill a cube. To calculate the volume of a cube we need the length of any side of cube. You can multiply the length of any side three times to get the volume.
Volume of Cube = Side X Side X Side = Side3
Where, Side is the length of any edge of a cube.

Java program to find the volume and surface area of cube

In this java program, we first take length of an edge of cube as input from user and then calculate volume and surface area using above mentioned mathematical formulae.

package com.tcc.java.programs;

import java.util.Scanner;

public class VolumeOfCube {

    public static void main(String[] args) {
        double side, volume, surfaceArea;
        Scanner scanner;
        scanner = new Scanner(System.in);

        System.out.println("Enter Length of Side of Cube");
        side = scanner.nextDouble();

        /* Total surface area of Cube = 6 X side X side */
        surfaceArea = 6 * side * side;
        /* Volume of Cube = side X side X side */
        volume = side * side * side;

        System.out.format("Surface Area of Cube 
            = %.3f\n", surfaceArea);
        System.out.format("Volume of Cube 
            = %.3f\n", volume);
    }
}
Output
Enter Length of Side of Cube
8
Surface Area of Cube = 384.000
Volume of Cube = 512.000
Enter Length of Side of Cube
1
Surface Area of Cube = 6.000
Volume of Cube = 1.000

Java program to find the volume and total surface area of cube using function

This java program is similar to above program except here we are using two user defined functions "getSurfaceArea" and "getVolume" functions to calculate total surface area and volume of cube respectively. Both functions takes length of side as input parameter.

package com.tcc.java.programs;

import java.util.Scanner;

public class VolumeOfCubeFunction {

    public static void main(String[] args) {
        double side;
        Scanner scanner;
        scanner = new Scanner(System.in);
        // Take input from user
        System.out.println("Enter Length of Side of Cube");
        side = scanner.nextDouble();

        System.out.format("Surface Area of Cube = %.3f\n", 
            getSurfaceArea(side));
        System.out.format("Volume of Cube = %.3f\n", 
            getVolume(side));
    }

    public static double getSurfaceArea(double side) {
        /* Total surface area of Cube = 6 X side X side */
        return 6 * side * side;
    }

    public static double getVolume(double side) {
        /* Volume of Cube = side X side X side */
        return side * side * side;
    }
}
Output
Enter Length of Side of Cube
4.5
Surface Area of Cube = 121.500
Volume of Cube = 91.125
Enter Length of Side of Cube
2
Surface Area of Cube = 24.000
Volume of Cube = 8.000

Recommended Posts
Java Program to Find Volume and Surface Area of Cuboid
Java Program to Calculate Volume and Surface Area of Cone
Java Program to Calculate Volume of Sphere
Java Program to Find Surface Area and Volume of Cylinder
Java Program to Find Area of Right Angled Triangle
Java Program to Find Area Of Trapezoid
Java Program to Find Area Of Triangle
Java Program to find Area Of Equilateral Triangle
Java Program to calculate area and circumference of circle
Java Program to Find Area and Perimeter of Rectangle
Java Program to Convert Decimal to Binary Numbers
All Java Programs