Java Program to find Volume and Surface Area of Sphere

Write Java Program to find Volume and Surface area of Sphere with example. Before we step into the Java program, let see the definitions and formulas behind the Surface area of Sphere and Volume of Sphere

Java Surface Area of Sphere

A Sphere looks like a basketball, or we can say it as a three-dimensional view of a circle. If we know the radius of a Sphere then we can calculate the Surface Area of a Sphere using the formula:

Surface Area of a Sphere = 4πr² (Where r is the radius of the sphere).

From the above formula, If we know the Surface Area of a sphere, then we can calculate the radius of a Sphere using the formula:

The radius of a Sphere = √sa / 4π (Where sa is the Surface Area of a sphere).

The Java Volume of a Sphere

The amount of space inside the sphere is called Volume. If we know the radius of the Sphere, then we can calculate the Volume of Sphere using the formula:

The volume of a Sphere = 4πr³

Java Program to find Volume and Surface Area of Sphere

This Java program allows user to enter the value of a radius. Then the Java program will find the Surface Area and Volume of a Sphere as per the formula.

// Java Program to find Volume and Surface Area of Sphere 

package SurfaceAreaPrograms;

import java.util.Scanner;

public class VolumeOfSphere {
	private static Scanner sc;

	public static void main(String[] args) {
		double radius, sa, Volume;
		sc = new Scanner(System.in);
		
		System.out.println("\n Please Enter the radius of a Sphere : ");
		radius = sc.nextDouble();
		
		sa =  4 * Math.PI * radius * radius;
		Volume = (4.0 / 3) * Math.PI * radius * radius * radius;

		System.out.format("\n The Surface area of a Sphere = %.2f", sa);
		System.out.format("\n The Volume of a Sphere = %.2f", Volume);
	}
}
Java Program to find Volume and Surface Area of Sphere 1

The following statements will allow the user to enter the radius of the sphere. Then we are assigning the user entered value to an already declared variable called radius.

System.out.println("\n Please Enter the radius of a Sphere : ");
radius = sc.nextDouble();

Next, we are using the Mathematical Formula to calculate the surface area of the sphere

sa =  4 * Math.PI * radius * radius;

In the next Java line, We are calculating the volume of the sphere

Volume = (4.0 / 3) * Math.PI * radius * radius * radius;

The following System.out.format statements will help us to print the volume and surface area of the sphere

System.out.format("\n The Surface area of a Sphere = %.2f", sa);
System.out.format("\n The Volume of a Sphere = %.2f", Volume);

From the above Java Program to find Volume and Surface Area of Sphere screenshot, you can observe that we have entered the Radius of a Sphere = 6

The Surface Area of a Sphere is

Surface Area = 4πr²
Surface Area = 4 * Math.PI * radius * radius;
Surface Area = 4 * 3.14 * 6 * 6
Surface Area = 314

The Volume of a Sphere is

Volume = 4πr³
Volume = (4.0 / 3) * PI * radius * radius * radius
Volume = (4.0 / 3) * 3.14 * 5 * 5 * 5
Volume = 452.39

Let us calculate the Radius of a Sphere using the Surface Area:

In the above example, we got the Surface area of a Sphere = 452 when the radius = 6. Let us make the reverse approach (Calculating the radius from the Surface area).

radius of a Sphere = √sa / 4π
radius of a Sphere = √452 / 4 * 3.14
radius of a Sphere = √452 / 12.56
radius of a Sphere = √35.98
radius of a Sphere = 5.99

Java Program to find Volume and Surface Area of Sphere using Functions

This Java program allows user to enter the value of a radius. Next, the Java program will calculate the Surface Area and Volume of a Sphere as per the formula. In this example, we are going to use the logic that we specified in the first example. However, we will separate the logic and place it in a method.

package SurfaceAreaPrograms;

import java.util.Scanner;

public class VolumeOfSphereUsingMethod {
	private static Scanner sc;

	public static void main(String[] args) {
		double radius;
		sc = new Scanner(System.in);
		
		System.out.println("\n Please Enter the radius of a Sphere : ");
		radius = sc.nextDouble();
		VolumeOfASphere (radius);

	}
	public static void VolumeOfASphere (double radius) {
		double sa, Volume;
		
		sa =  4 * Math.PI * radius * radius;
		Volume = (4.0 / 3) * Math.PI * radius * radius * radius;

		System.out.format("\n The Surface area of a Sphere = %.2f", sa);
		System.out.format("\n The Volume of a Sphere = %.2f", Volume);
	}
}
 Please Enter the radius of a Sphere : 
5

 The Surface area of a Sphere = 314.16
 The Volume of a Sphere = 523.60

Java Program to find Volume and Surface Area of Sphere using Oops

In this Java program, we are dividing the Volume and Surface Area of Sphere code using the Object-Oriented Programming. To do this, we will create a class that holds methods.

package SurfaceAreaPrograms;

public class VolumeOfASphere {
	double sa, Volume;
	
	public double VolumeOfSphere (double radius) {
		Volume = (4.0 / 3) * Math.PI * radius * radius * radius;
		return Volume;
	}
	
	public double SurfaceAreaOfSphere (double radius) {
		sa =  4 * Math.PI * radius * radius;
		return sa;
	}
}

Within the Main Java Program to find the Volume and Surface Area of Sphere program, we will create an instance of the above-specified class and call the methods.

package SurfaceAreaPrograms;

import java.util.Scanner;

public class VolumeOfSphereUsingClass {
	private static Scanner sc;

	public static void main(String[] args) {
		double radius, volume,surfaceArea;
		sc = new Scanner(System.in);
		
		System.out.println("\n Please Enter the radius of a Sphere : ");
		radius = sc.nextDouble();
		
		VolumeOfASphere vs = new VolumeOfASphere();
		volume = vs.VolumeOfSphere(radius);
		surfaceArea = vs.SurfaceAreaOfSphere(radius);
		
		System.out.format("\n The Surface area of a Sphere = %.2f", surfaceArea);
		System.out.format("\n The Volume of a Sphere = %.2f", volume);
	}
}
 Please Enter the radius of a Sphere : 
4

 The Surface area of a Sphere = 201.06
 The Volume of a Sphere = 268.08

AreaOfASphere Class Analysis:

  • First, we declared a function VolumeofSphere with one argument. Within the function, we are calculating the Volume of a Sphere and returning the value
  • Next, we declared a function SurfaceAreaofSphere with one argument. Within the function, we are calculating the Surface Area of a Sphere and returning the value.

Main Class Analysis:

In this Java Program to find Volume and Surface Area of Sphere, we created an instance / created an Object of the AreaOfASphere Class

VolumeOfASphere vs = new VolumeOfASphere();

Next, we are calling the VolumeofSphere method. It is the first method that we created with a double data type, and this method will calculate the Volume of a Sphere and return a value. So, we are assigning the return value to the volume variable.

volume = vs.VolumeOfSphere(radius);

Next, we are calling the SurfaceAreaofSphere method. It is the second method that we created with a double data type, and this method will calculate the Surface Area of a Sphere and return a value. So, we are assigning the return value to the surfaceArea variable.

surfaceArea = vs.SurfaceAreaOfSphere(radius);

Lastly, we used the following Java System.out.format statement to print the Volume and Surface Area of a Sphere.

System.out.format("\n The Surface area of a Sphere = %.2f", surfaceArea);
System.out.format("\n The Volume of a Sphere = %.2f", volume);