-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVolumeCalculator.java
More file actions
136 lines (107 loc) · 4.22 KB
/
VolumeCalculator.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
import java.util.Scanner;
// Interface for Volume calculation
interface Volume {
// Abstract method to calculate volume
double volume();
// Method to display volume
default void displayVolume() {
System.out.printf("Volume: %.2f cubic units\n", volume());
}
}
// Cone class implementing Volume
class Cone implements Volume {
private double radius;
private double height;
// Constructor
public Cone(double radius, double height) {
this.radius = radius;
this.height = height;
}
@Override
public double volume() {
// Volume of cone = (1/3) * π * r² * h
return (1.0/3.0) * Math.PI * radius * radius * height;
}
}
// Hemisphere class implementing Volume
class Hemisphere implements Volume {
private double radius;
// Constructor
public Hemisphere(double radius) {
this.radius = radius;
}
@Override
public double volume() {
// Volume of hemisphere = (2/3) * π * r³
return (2.0/3.0) * Math.PI * Math.pow(radius, 3);
}
}
// Cylinder class implementing Volume
class Cylinder implements Volume {
private double radius;
private double height;
// Constructor
public Cylinder(double radius, double height) {
this.radius = radius;
this.height = height;
}
@Override
public double volume() {
// Volume of cylinder = π * r² * h
return Math.PI * radius * radius * height;
}
}
public class VolumeCalculator {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Volume Calculator");
System.out.println("----------------");
// Menu for selecting shape
int choice;
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) {
case 1:
System.out.println("\nCone Volume Calculation");
System.out.println("----------------------");
System.out.print("Enter radius: ");
double coneRadius = scanner.nextDouble();
System.out.print("Enter height: ");
double coneHeight = scanner.nextDouble();
Cone cone = new Cone(coneRadius, coneHeight);
cone.displayVolume();
break;
case 2:
System.out.println("\nHemisphere Volume Calculation");
System.out.println("---------------------------");
System.out.print("Enter radius: ");
double hemisphereRadius = scanner.nextDouble();
Hemisphere hemisphere = new Hemisphere(hemisphereRadius);
hemisphere.displayVolume();
break;
case 3:
System.out.println("\nCylinder Volume Calculation");
System.out.println("-------------------------");
System.out.print("Enter radius: ");
double cylinderRadius = scanner.nextDouble();
System.out.print("Enter height: ");
double cylinderHeight = scanner.nextDouble();
Cylinder cylinder = new Cylinder(cylinderRadius, cylinderHeight);
cylinder.displayVolume();
break;
case 4:
System.out.println("Exiting...");
break;
default:
System.out.println("Invalid choice. Please try again.");
}
} while (choice != 4);
scanner.close();
}
}