-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2D&3DShapes.java
More file actions
165 lines (141 loc) · 3.78 KB
/
Copy path2D&3DShapes.java
File metadata and controls
165 lines (141 loc) · 3.78 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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
import java.util.Scanner;
//3 Abstract Classes
abstract class Shape{
public abstract String toString();
}
abstract class TwoDimensionalShape extends Shape{
public double perimeter(){return 0.0;}
public double area(){return 0.0;}
public String toString(){
return String.format("Area: %.2f %nPerimeter: %.2f%n", area(), perimeter());
}
}
abstract class ThreeDimensionalShape extends Shape{
public double getVolume(){ return 0.0;}
public double getSurfaceArea(){ return 0.0;}
public String toString(){
return String.format("Surface area: %.2f %nVolume: %.2f", getSurfaceArea(), getVolume());
}
}
//two dimensional shapes.
class Square extends TwoDimensionalShape{
private double side;
public Square(double s){
side = s;
}
public double area(){
return Math.pow(side, 2);
}
public double perimeter(){
return 4*side;
}
}
class Triangle extends TwoDimensionalShape{
private double side1;
private double side2;
private double side3;
private double heron;
public Triangle(double s1, double s2, double s3){
side1 = s1; side2 = s2; side3 = s3;
}
public double area(){
heron = (side1 + side2 + side3)/2;
return Math.sqrt(heron*(heron-side1)*(heron-side2)*(heron-side3));
}
public double perimeter(){
return side1+side2+side3;
}
}
//three dimensional shapes.
class Sphere extends ThreeDimensionalShape{
private double radius;
public Sphere(double r){
radius = r;
}
public double getVolume(){
return (4.0/3)*Math.PI * Math.pow(radius, 3);
}
public double getSurfaceArea(){
return 4* Math.pow(radius, 2)* Math.PI;
}
}
class Cube extends ThreeDimensionalShape{
private double side;
public Cube(double s){
side = s;
}
public double getVolume(){
return Math.pow(side, 3);
}
public double getSurfaceArea(){
return Math.pow(side, 2) * 6;
}
}
class Tetrahedron extends ThreeDimensionalShape{
private double side;
public Tetrahedron(double s){
side = s;
}
public double getSurfaceArea(){
return Math.pow(side, 2) * Math.sqrt(3);
}
public double getVolume(){
return Math.pow(side, 3) * (1.0/(6 *Math.sqrt(2)));
}
}
//Driver Class
public class Driver{
public static void main(String []args){
Scanner input = new Scanner(System.in);
System.out.printf("Enter\n1)Two dimensional shape\n2)Three dimensional shape:");
int number1 = input.nextInt();
Shape obj;
if( number1 == 1){
System.out.printf("Enter\n1)Circle\n2)Square\n3)Triangle:");
int number2 = input.nextInt();
if(number2 == 1){
System.out.printf("Enter radius of circle:");
double radius = input.nextDouble();
obj = new Circle(radius);
System.out.print(obj.toString());
}
if(number2 == 2){
System.out.printf("Enter side of square:");
double side = input.nextDouble();
obj = new Square(side);
System.out.print(obj.toString());
}
if(number2 == 3){
System.out.printf("Enter side of triangle:");
double side1 = input.nextDouble();
System.out.printf("Enter side of triangle:");
double side2 = input.nextDouble();
System.out.printf("Enter side of triangle:");
double side3 = input.nextDouble();
obj = new Triangle(side1, side2, side3);
System.out.print(obj.toString());
}
} else {
System.out.printf("Enter\n1)Sphere\n2)Cube\n3)Tetrahedron:");
int number3 = input.nextInt();
if(number3 == 1){
System.out.printf("Enter radius of sphere:");
double radius = input.nextDouble();
obj = new Sphere(radius);
System.out.println(obj.toString());
}
if(number3 == 2){
System.out.printf("Enter side of cube:");
double side = input.nextDouble();
obj = new Cube(side);
System.out.print(obj.toString());
}
if(number3 == 3){
System.out.printf("Enter side of tetrahedron:");
double side = input.nextDouble();
obj = new Tetrahedron(side);
System.out.print(obj.toString());
}
}
}
}