-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshape.java
More file actions
67 lines (65 loc) · 1.67 KB
/
shape.java
File metadata and controls
67 lines (65 loc) · 1.67 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
package assignment3;
import java.util.*;
interface Shape{
double calcArea();
double calcVolume();
}
class Sphere implements Shape{
double r;
Sphere(double r){
this.r=r;
}
public double calcArea(){
return 4*3.14*r*r;
}
public double calcVolume(){
return 4*3.14*Math.pow(r,3)/3;
}
}
class Cone implements Shape{
double r;
double h;
Cone(double r, double h){
this.r=r;
this.h=h;
}
public double calcArea(){
return 3.14*r*(r+Math.sqrt(r*r+h*h));
}
public double calcVolume(){
return 3.14*r*r*h/3;
}
}
class Cylinder implements Shape{
double r;
double h;
Cylinder(double r, double h){
this.r=r;
this.h=h;
}
public double calcArea(){
return 2*3.14*r*(h+r);
}
public double calcVolume(){
return 3.14*r*r*h;
}
}
public class ShapeDetails {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter radious of the Sphere ");
double r = sc.nextDouble();
System.out.println("Enter radious and height of the Cones ");
double r2=sc.nextDouble();
double h2=sc.nextDouble();
System.out.println("Enter radious and height of the Cyllender ");
double r3=sc.nextDouble();
double h3=sc.nextDouble();
Shape obj1 = new Sphere(r);
Shape obj2 = new Cone(r2,h2);
Shape obj3 = new Cylinder(r3,h3);
System.out.println("Area of Sphere : "+obj1.calcArea()+" Volume of Sphere : "+obj1.calcVolume());
System.out.println("Area of Cone : "+obj2.calcArea()+" Volume of Cone : "+obj2.calcVolume());
System.out.println("Area of Cylinder : "+obj3.calcArea()+" Volume of Cylinder : "+obj3.calcVolume());
}
}