In: Computer Science
1) Write a functional program in Java that can calculate the volume and surface area of a sphere and a cube
2) Write a procedural program in Java that can calculate the volume and surface area of a sphere and a cube
3) Write an Object Oriented Program in Java that can find the volume and surface area of a sphere and cube
Procedural Program:
import java.util.*;
class Main {
public static void main(String[] args) {
double radius, side,SAsphere,VSphere,SAcube,Vcube;
Scanner sc=new Scanner(System.in);
System.out.println("Enter radius of sphere: ");
radius=sc.nextDouble();
System.out.println("Enter Side of cube: ");
side=sc.nextDouble();
SAsphere=3.14*4*radius*radius;
VSphere=(4/3)*3.14*radius*radius*radius;
SAcube=6*side*side;
Vcube=side*side*side;
System.out.println("Surface area of sphere is : "+SAsphere);
System.out.println("Volume of Sphere: "+VSphere);
System.out.println("Surface area of Cube is "+SAcube);
System.out.println("Volume of Cube: "+Vcube);
}
}
2)Functional Program:
import java.util.*;
class Main {
public static double volumeSphere(double radius){
return (4/3)*3.14*radius*radius*radius;
}
public static double SAsphere(double radius){
return 3.14*4*radius*radius;
}
public static double volumeCube(double side){
return side*side*side;
}
public static double SACube(double side){
return 6*side*side;
}
public static void main(String[] args) {
double radius, side,SAsphere,VSphere,SAcube,Vcube;
Scanner sc=new Scanner(System.in);
System.out.println("Enter radius of sphere: ");
radius=sc.nextDouble();
System.out.println("Enter Side of cube: ");
side=sc.nextDouble();
System.out.println("Surface area of sphere is : "+SAsphere(radius));
System.out.println("Volume of Sphere: "+volumeSphere(radius));
System.out.println("Surface area of Cube is "+SACube(side));
System.out.println("Volume of Cube: "+volumeCube(side));
}
}
3) Object Oriented:
import java.util.*;
class Calculation {
public static double volumeSphere(double radius){
return (4/3)*3.14*radius*radius*radius;
}
public static double SAsphere(double radius){
return 3.14*4*radius*radius;
}
public static double volumeCube(double side){
return side*side*side;
}
public static double SACube(double side){
return 6*side*side;
}
}
class Main{
public static void main(String[] args) {
Calculation cal=new Calculation();
double radius, side,SAsphere,VSphere,SAcube,Vcube;
Scanner sc=new Scanner(System.in);
System.out.println("Enter radius of sphere: ");
radius=sc.nextDouble();
System.out.println("Enter Side of cube: ");
side=sc.nextDouble();
System.out.println("Surface area of sphere is : "+cal.SAsphere(radius));
System.out.println("Volume of Sphere: "+cal.volumeSphere(radius));
System.out.println("Surface area of Cube is "+cal.SACube(side));
System.out.println("Volume of Cube: "+cal.volumeCube(side));
}
}
if you like the answer please provide a thumbs up.