-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path2030Writtentest1.txt
81 lines (67 loc) · 1.83 KB
/
2030Writtentest1.txt
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
public class Triangle {
double s1, s2, s3;
//remember the throws part
public Triangle(double s1, double s2, double s3) throws InvalidSidesException {
if(((s1 + s2) > s3) || ((s1 + s3) > s2) || ((s3 + s2) > s1)){
this.s1 = s1;
this.s2 = s2;
this.s3 = s3;
}
throw new InvalidSidesException("Invalid");
}
}
/**************************************************************/
CORRECT:
class TriangleCollector {
Triangle[] triangles;
int numberOfTriangles;
TriangleCollector() {
triangles = new Triangle[10];
}
void addTriangle(double side1, double side2, double side3)
throws InvalidSidesException {
Triangle t = new Triangle(side1, side2, side3);
triangles[numberOfTriangles] = t;
numberOfTriangles ++;
}
}
/********************************************************/
class TriangleCollectorTester {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
/* Your answer starts from here... */
TriangleCollector tc = new TriangleCollector();
boolean exit1 = false;
while( exit1 == false) {
sysout("Enter the first side: ");
double s1 = input.nextDouble;
sysout("Enter the 2nd side: ");
double s2 = input.nextDouble;
sysout("Enter the 3rd side: ");
double s3 = input.nextDouble;
try{
tc.addTriangle(s1,s2,s3);
sysout("A triangle with sides " + s1 + " " + s2 + " " + s3 + " is added.");
} catch (InvalidSidesException e) {
exit1 = true;
sysout("Invalid sides: " + s1 + " " + s2 + " " + s3);
sysout("Bye!");
}
}
}
}
/*******************************************************/
WRONG:
void addTriangle(double side1, double side2, double side3){
Triangle[] collection;
int num = 0;
try{
Triangle(side1, side2, side3);
if(collection.length < 11){
Triangle[num] = Triangle(side1, side2, side3);
num++;
}
}catch(InvalidSidesException e) {
System.out.println("Invalid");
}
}