-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathShapeTest.java
More file actions
189 lines (152 loc) · 4.16 KB
/
ShapeTest.java
File metadata and controls
189 lines (152 loc) · 4.16 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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
package com.bilgeadam.boost.Marathon01;
import java.util.Scanner;
public class ShapeTest {
static Scanner sc = new Scanner(System.in);
public static void main(String[] args) {
ShapeTest shapeTest = new ShapeTest();
shapeTest.readInput();
System.out.println("Bye....");
}
private void readInput() {
int shape = 1;
do {
int firstEdge = 0;
int secondEdge = 0;
int thirdEgde = 0;
int fourthEdge = 0;
int areaCalc = 0;
int perimeterSum = 0;
System.out.println("Lutfen hesaplamak istediginiz seklin (en fazla 4 kenar) "
+ "kenar uzunluklarini giriniz (-1 ile cikis): ");
while (true) {
int i = 1;
firstEdge = this.scanEdge(i);
if (this.isEnd(firstEdge)) {
return;
}
if (this.isExit(firstEdge)) {
break;
}
i++;
secondEdge = scanEdge(i);
if (this.isEnd(secondEdge)) {
return;
}
if (this.isExit(secondEdge)) {
break;
}
i++;
thirdEgde = scanEdge(i);
if (this.isEnd(thirdEgde)) {
return;
}
if (this.isExit(thirdEgde)) {
break;
}
i++;
fourthEdge = scanEdge(i);
if (this.isEnd(fourthEdge)) {
return;
}
if (this.isExit(fourthEdge)) {
break;
}
break;
}
if ((secondEdge != 0) && (thirdEgde != 0)) {
showMenu();
System.out.print("\nSecim: ");
String operation = scanString();
switch (operation) {
case "1":
perimeterSum = this.doPerimeterCalc(firstEdge, secondEdge, thirdEgde, fourthEdge);
System.out.println(shape + ". Sekilin cevresi:" + perimeterSum);
shape++;
break;
case "2":
areaCalc = this.doAreaCalc(firstEdge, secondEdge, thirdEgde, fourthEdge);
System.out.println(shape + ". Sekilin alani:" + areaCalc);
shape++;
break;
case "3":
perimeterSum = this.doPerimeterCalc(firstEdge, secondEdge, thirdEgde, fourthEdge);
areaCalc = this.doAreaCalc(firstEdge, secondEdge, thirdEgde, fourthEdge);
System.out.println(shape + ". Sekilin cevresi:" + perimeterSum);
System.out.println(shape + ". Sekilin alani:" + areaCalc);
shape++;
break;
default:
System.out.println("Wrong input!! Try Again.");
break;
}
} else if (secondEdge == 0) {
System.err.println("Tek kenarli sekil olamaz");
} else if (thirdEgde == 0) {
System.err.println("Iki kenarli sekil olamaz");
}
} while (true);
}
private int doAreaCalc(int firstEdge, int secondEdge, int thirdEgde, int fourthEdge) {
int areaCalc = 0;
if (this.isTriangle(fourthEdge)) {
areaCalc = this.calcTriangleArea(firstEdge, secondEdge, thirdEgde);
}
else {
areaCalc = this.calcTrapezoidalArea(firstEdge, secondEdge, thirdEgde, fourthEdge);
}
return areaCalc;
}
private int doPerimeterCalc(int firstEdge, int secondEdge, int thirdEgde, int fourthEdge) {
int perimeterSum = firstEdge + secondEdge + thirdEgde + fourthEdge;
return perimeterSum;
}
private static void showMenu() {
System.out.println("\n1- Cevre");
System.out.println("2- Alan");
System.out.println("3- Hem Cevre hem Alan");
}
private boolean isTriangle(int fourthEdge) {
if (fourthEdge == 0) {
return true;
}
return false;
}
private int calcTriangleArea(int firstEdge, int secondEdge, int thirdEgde) {
int area = 0;
int sum = firstEdge + secondEdge + thirdEgde;
area = (int) Math.sqrt(sum * (sum - firstEdge) * (sum - secondEdge) * (sum - thirdEgde));
return area;
}
private int calcTrapezoidalArea(int firstEdge, int secondEdge, int thirdEgde, int fourthEdge) {
int area = 0;
// trapezoidal area (a+b/2)*h
// But I made a wrong calculation here please ignore
area = (firstEdge + thirdEgde) * secondEdge / 2;
return area;
}
private boolean isExit(int edge) {
if (edge == 0) {
return true;
}
return false;
}
private boolean isEnd(int edge) {
if (edge == -1) {
return true;
}
return false;
}
private int scanEdge(int i) {
System.out.print("Lutfen " + i + ". kenar uzunlugunu giriniz (0 ile sonlandir): ");
int number = scanInt();
return number;
}
private static int scanInt() {
int number = sc.nextInt();
return number;
}
private static String scanString() {
String input = sc.next();
return input;
}
}