Question 7.
Design a class to overload a function volume() as follows :
(i) double volume (double R) — with radius (R) as an argument, returns the volume of sphere using the formula.
V = 4/3 × 22/7 × R3
(ii) double volume (double H, double R) – with height(H) and radius(R) as the arguments, returns the volume of a cylinder using the formula.
V = 22/7 × R2 × H
(iii) double volume (double L, double B, double H) – with length(L), breadth(B) and Height(H) as the arguments, returns the volume of a cuboid using the formula.
import java.util.Scanner;
public class ICSEJava2018 {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
VolumeOfObjects obj = new VolumeOfObjects();
int choice = 0;
double R = 0, H = 0, B = 0;
System.out.println("Press 1 to calculate the volume of a sphere:");
System.out.println("Press 2 to calculate the volume of a cylinder:");
System.out.println("Press 3 to calculate the volume of a cuboid:");
choice = in.nextInt();
switch (choice) {
case 1:
System.out.println("Enter the radius of the sphere:");
R = in.nextDouble();
System.out.println("Volume of the sphere:" + obj.volume(R)+" cubic units");
break;
case 2:
System.out.println("Enter the radius of the cylinder:");
R = in.nextDouble();
System.out.println("Enter the height of the cylinder:");
H = in.nextDouble();
System.out.println("Volume of the cylinder:" + obj.volume(H, R)+" cubic units");
break;
case 3:
System.out.println("Enter the length of the cuboid:");
R = in.nextDouble();
System.out.println("Enter the breadth of the cuboid:");
H = in.nextDouble();
System.out.println("Enter the height of the cuboid:");
B = in.nextDouble();
System.out.println("Volume of the cylinder:" + obj.volume(R, H,B)+" cubic units");
break;
default:
System.out.println("Wrong Choice!");
break;
}
in.close();
}
}
class VolumeOfObjects {
public double volume(double R) {
return (4 * 22 * R * R * R) / (21);
}
public double volume(double H, double R) {
return (22 * R * R * H) / 7;
}
public double volume(double L, double B, double H) {
return (L * B * H);
}
}
No comments:
Post a Comment