-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCalcArea
125 lines (95 loc) · 3.59 KB
/
CalcArea
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
*****************************************************
import static com.google.common.math.IntMath.mod;
public class Main {
public static void main(String[] args) {
Calculator calculator=new Calculator();
int x = mod(7,3);
System.out.println(x);
}
}
*****************************************************
*****************************************************
import java.util.Scanner;
public class Calculator {
public Calculator() {
//calc();
}
public void calc() {
double area = 0;
boolean flag = true;
while (flag) {
System.out.println("Calc Area:" +
"\n 1 - Circle" +
"\n 2 - Triangle" +
"\n 3 - Rectangle" +
"\n 4 - Other Regular Polygon" +
"\n 5 - Quit calculating");
Scanner sc = new Scanner(System.in);
int choice = sc.nextInt();
if (choice == 1) {
System.out.println("Enter radius (no units):");
double radius = sc.nextDouble();
area = calcArea(radius);
} else if (choice == 2) {
System.out.println("Enter side length 1:");
double sideOne = sc.nextDouble();
System.out.println("Enter side length 2:");
double sideTwo = sc.nextDouble();
System.out.println("Enter side length 1:");
double sideThree = sc.nextDouble();
area = calcArea(sideOne, sideTwo, sideThree);
} else if (choice == 3) {
System.out.println("Enter side length 1:");
double sideOne = sc.nextDouble();
System.out.println("Enter side length 2:");
double sideTwo = sc.nextDouble();
area = calcArea(sideOne, sideTwo);
} else if (choice == 4) {
System.out.println("Enter side length:");
double sideLength = sc.nextDouble();
System.out.println("Enter number of sides:");
int sides = sc.nextInt();
area = calcArea(sideLength, sides);
} else {
flag = false;
break;
}
System.out.println("Area is: " + area);
}
}
public double calcArea(double r) {
double area = r * r * Math.PI;
return area;
}
public double calcArea(double x, double y, double z) {
double semiPerimeter = (x + y + z) / 2;
double area = Math.sqrt((semiPerimeter - x) * (semiPerimeter - y) * (semiPerimeter - z) * (semiPerimeter));
return area;
}
public double calcArea(double x, double y) {
double area = x * y;
return area;
}
public double calcArea(double x, int y) {
double halfCentAngle = (360 / (y * 2))*Math.PI/180;
double halfSide = x / 2;
double apothem = halfSide / Math.tan(halfCentAngle);
double area = x * y * apothem / 2;
return area;
}
}
*****************************************************
*****************************************************
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
public class CalculatorTests {
Calculator calculator = new Calculator();
@Test
void test(){
assertEquals(10,calculator.calcArea(5,2.0));
//assertEquals(3.14,calculator.calcArea(1));
//assertEquals(16,calculator.calcArea(4,4));
assertEquals(16,calculator.calcArea(4,4.0));
}
}
*****************************************************