-
Notifications
You must be signed in to change notification settings - Fork 0
/
slip3B.java
60 lines (49 loc) · 1.48 KB
/
slip3B.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
50
51
52
53
54
55
56
57
58
59
60
abstract class Shape {
abstract double area();
abstract double volume();
}
class Cone extends Shape {
private double radius;
private double height;
public Cone(double radius, double height) {
this.radius = radius;
this.height = height;
}
@Override
double area() {
double slantHeight = Math.sqrt(radius * radius + height * height);
return Math.PI * radius * (radius + slantHeight);
}
@Override
double volume() {
return (1.0 / 3.0) * Math.PI * radius * radius * height;
}
}
class Cylinder extends Shape {
private double radius;
private double height;
public Cylinder(double radius, double height) {
this.radius = radius;
this.height = height;
}
@Override
double area() {
return 2 * Math.PI * radius * (radius + height);
}
@Override
double volume() {
return Math.PI * radius * radius * height;
}
}
public class ShapeTest {
public static void main(String[] args) {
Cone cone = new Cone(5.0, 10.0);
Cylinder cylinder = new Cylinder(3.0, 7.0);
System.out.println("Cone:");
System.out.println("Surface Area: " + cone.area());
System.out.println("Volume: " + cone.volume());
System.out.println("\nCylinder:");
System.out.println("Surface Area: " + cylinder.area());
System.out.println("Volume: " + cylinder.volume());
}
}