Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVolumeCalculator.java
More file actions
Latest commit
136 lines (107 loc) · 4.22 KB
/
Copy pathVolumeCalculator.java
File metadata and controls
136 lines (107 loc) · 4.22 KB
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
importjava.util.Scanner;
// Interface for Volume calculation
interfaceVolume {
// Abstract method to calculate volume
doublevolume();
// Method to display volume
defaultvoiddisplayVolume() {
System.out.printf("Volume: %.2f cubic units\n", volume());
}
}
// Cone class implementing Volume
classConeimplementsVolume {
privatedoubleradius;
privatedoubleheight;
// Constructor
publicCone(doubleradius, doubleheight) {
this.radius = radius;
this.height = height;
}
@Override
publicdoublevolume() {
// Volume of cone = (1/3) * π * r² * h
return (1.0/3.0) * Math.PI * radius * radius * height;
}
}
// Hemisphere class implementing Volume
classHemisphereimplementsVolume {
privatedoubleradius;
// Constructor
publicHemisphere(doubleradius) {
this.radius = radius;
}
@Override
publicdoublevolume() {
// Volume of hemisphere = (2/3) * π * r³
return (2.0/3.0) * Math.PI * Math.pow(radius, 3);
}
}
// Cylinder class implementing Volume
classCylinderimplementsVolume {
privatedoubleradius;
privatedoubleheight;
// Constructor
publicCylinder(doubleradius, doubleheight) {
this.radius = radius;
this.height = height;
}
@Override
publicdoublevolume() {
// Volume of cylinder = π * r² * h
returnMath.PI * radius * radius * height;
}
}
publicclassVolumeCalculator {
publicstaticvoidmain(String[] args) {
Scannerscanner = newScanner(System.in);
System.out.println("Volume Calculator");
System.out.println("----------------");
// Menu for selecting shape
intchoice;
do {
System.out.println("\nSelect a shape to calculate volume:");
System.out.println("1. Cone");
System.out.println("2. Hemisphere");
System.out.println("3. Cylinder");
System.out.println("4. Exit");
System.out.print("Enter your choice (1-4): ");
choice = scanner.nextInt();
switch (choice) {
case1:
System.out.println("\nCone Volume Calculation");
System.out.println("----------------------");
System.out.print("Enter radius: ");
doubleconeRadius = scanner.nextDouble();
System.out.print("Enter height: ");
doubleconeHeight = scanner.nextDouble();
Conecone = newCone(coneRadius, coneHeight);
cone.displayVolume();
break;
case2:
System.out.println("\nHemisphere Volume Calculation");
System.out.println("---------------------------");
System.out.print("Enter radius: ");
doublehemisphereRadius = scanner.nextDouble();
Hemispherehemisphere = newHemisphere(hemisphereRadius);
hemisphere.displayVolume();
break;
case3:
System.out.println("\nCylinder Volume Calculation");
System.out.println("-------------------------");
System.out.print("Enter radius: ");
doublecylinderRadius = scanner.nextDouble();
System.out.print("Enter height: ");
doublecylinderHeight = scanner.nextDouble();
Cylindercylinder = newCylinder(cylinderRadius, cylinderHeight);
cylinder.displayVolume();
break;
case4:
System.out.println("Exiting...");
break;
default:
System.out.println("Invalid choice. Please try again.");
}
} while (choice != 4);
scanner.close();
}
}