-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathShape.java
100 lines (87 loc) · 1.94 KB
/
Shape.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
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
package project_5;
import java.lang.Math;
/**A class for defining a shape (circle, rectangle, or triangle) being able to find that shape's area or a given side.
* @version 1.0
* @author logan
*
*/
public class Shape {
//data variables
private String name;
private int sideA;
private int sideB;
private int sideC;
//constructors
//constructor for circle
public Shape(int sideA) {
name = "circle";
this.sideA = sideA;
sideB = -1;
sideC = -1;
}
//constructor for rectangle
public Shape(int sideA, int sideB) {
name = "rectangle";
this.sideA = sideA;
this.sideB = sideB;
sideC = -1;
}
//constructor for triangle
public Shape(int sideA, int sideB, int sideC) {
name = "triangle";
this.sideA = sideA;
this.sideB = sideB;
this.sideC = sideC;
}
//methods
//method for returning the area of a given circle, rectangle, or triangle shape
public double getArea() {
if(name.equals("circle")) {
return Math.PI * (sideA * sideA);
}
else if(name.equals("rectangle")) {
return sideA * sideB;
}
else {
double s = (sideA + sideB + sideC)/2;
return Math.sqrt(s * (s - sideA) * (s - sideB) * (s - sideC));
}
}
//methods to update the parameters of an existing shape
//update one side of the shape
public boolean setSideA(int i) {
sideA = i;
return true;
}
//update second side (for rectanagle or triangle)
public boolean setSideB(int j) {
if(name.equals("circle")) {
return false;
} else {
sideB = j;
return true;
}
}
//update third side (for triangles only)
public boolean setSideC(int k) {
if(name.equals("triangle")) {
sideC = k;
return true;
} else {
return false;
}
}
//toString override for Shape
@Override
public String toString() {
if(name.equals("circle")) {
return "Area of Circle: " + getArea();
}
else if(name.equals("rectangle")) {
return "Area of Rectangle: " + getArea();
}
else {
return "Area of Triangle: " + getArea();
}
}
}