-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram42.java
49 lines (43 loc) · 1.4 KB
/
Program42.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
/* Program 42
Write a class with the name Volume using function overloading that computes the volume of a cube, a sphere and a cuboid.
04/06/24 */
import java.util.Scanner;
public class Volume {
static int volume(int s) { // cube
return s * s * s;
}
static double volume(double r) { // sphere
return (double)4/3 * 3.14 * r * r * r;
}
static int volume(int l, int b, int h) { // cuboid
return l * b * h;
}
static double input(String p) {
Scanner sc = new Scanner(System.in);
System.out.print(p);
return sc.nextDouble();
}
public static void main(String args[]) {
int choice = (int)input("Enter choice\n1. Cube\n2. Sphere\n3. Cuboid\n");
double res = -1;
switch (choice) {
case 1:
int s = (int)input("Enter side length: ");
res = volume(s);
break;
case 2:
double r = input("Enter radius: ");
res = volume(r);
break;
case 3:
int l = (int)input("Enter length: ");
int b = (int)input("Enter breadth: ");
int h = (int)input("Enter height: ");
res = volume(l, b, h);
break;
default:
System.out.println("Invalid input");
}
System.out.println("The volume is " + res);
}
}