-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProject10.java
More file actions
92 lines (77 loc) · 2.48 KB
/
Project10.java
File metadata and controls
92 lines (77 loc) · 2.48 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
// Abstract Shape class
class Shape {
public double getVolume() {
return 0;
}
}
// Sphere class that extends Shape
class Sphere extends Shape {
private double radius;
public Sphere(double radius) {
this.radius = radius;
}
@Override
public double getVolume() {
// Volume of sphere = (4/3) * π * r^3
return (4.0 / 3.0) * Math.PI * Math.pow(radius, 3);
}
}
// Cone class that extends Shape
class Cone extends Shape {
private double radius;
private double height;
public Cone(double radius, double height) {
this.radius = radius;
this.height = height;
}
@Override
public double getVolume() {
// Volume of cone = (1/3) * π * r^2 * h
return (1.0 / 3.0) * Math.PI * Math.pow(radius, 2) * height;
}
}
// Cube class that extends Shape
class Cube extends Shape {
private double side;
public Cube(double side) {
this.side = side;
}
@Override
public double getVolume() {
// Volume of cube = side^3
return Math.pow(side, 3);
}
}
// Cuboid class that extends Shape
class Cuboid extends Shape {
private double length;
private double breadth;
private double height;
public Cuboid(double length, double breadth, double height) {
this.length = length;
this.breadth = breadth;
this.height = height;
}
@Override
public double getVolume() {
// Volume of cuboid = length * breadth * height
return length * breadth * height;
}
}
// Main class to test the volume calculation
public class Project10{
public static void main(String[] args) {
// Creating an array of Shape objects to hold different shapes
Shape[] shapes = new Shape[4];
// Initializing the shapes with their dimensions
shapes[0] = new Sphere(3); // Sphere with radius 3
shapes[1] = new Cone(3, 5); // Cone with radius 3 and height 5
shapes[2] = new Cube(4); // Cube with side 4
shapes[3] = new Cuboid(2, 3, 4); // Cuboid with length 2, breadth 3, and height 4
// Printing the volumes of each shape
System.out.println("The Volume of your Sphere is: " + shapes[0].getVolume());
System.out.println("The Volume of your Cone is: " + shapes[1].getVolume());
System.out.println("The Volume of your Cube is: " + shapes[2].getVolume());
System.out.println("The Volume of your Cuboid is: " + shapes[3].getVolume());
}
}