Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Open a new project in Netbeans and call it Sphere. Write a complete Java program

ID: 3878019 • Letter: O

Question

Open a new project in Netbeans and call it Sphere. Write a complete Java program that takes the radius of a sphere as an input from the user and calculates its volume using the formula:

Volume = (4/3) x PI x (Radius)3

Your program should contain the following parts:

1. A class Sphere that contains a Constructor, Set, Get, and sphereVolume methods

2. Correct use of the static Math class and methods

3. A main function that prompts the user for a positive number and displays the sphere's volume with 2 digits precision. Perform validity checking for the input radius.

Explanation / Answer

Code :


import java.util.*;

class Sphere {
int radius;
Sphere(int r) {
radius = r;
}
  
public void set(int r) {
radius = r;
}
  
public int get() {
return radius;
}
  
public double sphereVolume() {
double r = radius;
double v = 4 * 3.14 * r * r * r;
v=v/3;
v = v*100;
v = Math.round(v);
v = v/100;
return v;
}
}

class Main {
   public static void main (String[] args) {
       Scanner sc = new Scanner(System.in);
       System.out.println("Enter radius of sphere :");
       int r = sc.nextInt();
       Sphere s = new Sphere(r);
       System.out.println("Volume is "+ s.sphereVolume());
   }
}

Working demo link : https://ide.geeksforgeeks.org/GZDUvvER4y