Tutorial Gateway

  • C Language
  • Java
  • R
  • SQL
  • MySQL
  • Python
  • BI Tools
    • Informatica
    • Tableau
    • Power BI
    • SSIS
    • SSRS
    • SSAS
    • MDX
    • QlikView
  • Js

Java Program to find Volume and Surface Area of Sphere

by suresh

In this article we will show you, How to write Java Program to find Volume and Surface area of Sphere with example. Before we step into the program, Let see the definitions and formulas behind Surface area of Sphere and Volume of Sphere

Surface Area of Sphere

A Sphere looks like a basketball or we can say it as 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 formula:

Surface Area of a Sphere = 4πr² (Where r is 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:

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

Volume of Sphere

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

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 and then it will calculate the Surface Area and Volume of a Sphere as per the formula.

JAVA CODE

// 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);
	}
}

OUTPUT

Java Program to find Volume and Surface Area of Sphere 1

ANALYSIS

Following statements will allow the User to enter the radius of sphere and then we are assigning the user entered value to 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 sphere

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

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

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

Following System.out.format statements will help us to print the volume and surface area of 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 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 Surface area of a Sphere = 452 when the radius = 6. Let us do the reverse approach (Calculating the radius from 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 and then it 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 first example but we will separated the logic and place it in a method.

JAVA CODE

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);
	}
}

OUTPUT

Java Program to find Volume and Surface Area of Sphere 2

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

In this Java program, we are dividing the code using the Object Oriented Programming. To do this, First we will create a class which holds a methods.

JAVA CODE

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 program, we will create an instance of the above specified class and call the methods.

JAVA CODE

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);
	}
}

OUTPUT

Java Program to find Volume and Surface Area of Sphere 3

ANALYSIS

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
  • First we declared a function SurfaceAreaofSphere with one argument. Within the function, we are calculating the Surface Area of a Sphere and returning the value

TIP: If you declare a method with void keyword then we can’t return any value. If you want to return any value then replace void with data type and add return keyword.

Main Class Analysis:

First we created an instance / created an Object of the AreaOfASphere Class

VolumeOfASphere vs = new VolumeOfASphere();

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

volume = vs.VolumeOfSphere(radius);

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

surfaceArea = vs.SurfaceAreaOfSphere(radius);

Lastly we used following 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);

Thank You for Visiting Our Blog

Placed Under: Java Programs

Stay in Touch!

Sign Up to receive Email updates on Latest Tutorials

  • C Programs
  • Java Programs
  • SQL FAQ’s
  • Python Programs
  • SSIS
  • Tableau
  • JavaScript

Copyright © 2019 | Tutorial Gateway· All Rights Reserved by Suresh

Home | About Us | Contact Us | Privacy Policy