This program calculates the volume and surface area of a cube. The user is prompted to input the edge length of the cube, which is then passed as an argument to the methods getVolume
and getSurface
. The methods getVolume
and getSurface
both take a single double argument a
and return the volume and surface area of a cube with edge length a
respectively. The returned values are then printed out to the console.
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));
}
}
Please enter the edge length:
5
Volume: 125.0
Surface: 150.0