-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDriver.java
148 lines (118 loc) · 4.79 KB
/
Driver.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
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
package project_5;
import java.util.Scanner;
public class Driver {
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
//Priming read
System.out.println("Select a type from 1 for Circle, 2 for Rectangle, 3 for Triangle or 4 to Quit to exit ");
int shapeID = keyboard.nextInt();
//This loop will run until user enter quit
while (shapeID != 4) {
//User is trying to calculate the area of the circle
if (shapeID == 1) {
isCircle(keyboard);
} else if (shapeID == 2) {
isRectangle(keyboard);
} else if (shapeID == 3) {
isTriangle(keyboard);
}
//System.out.println("Enter the shape for which you want to calculate area:");
System.out.println("Select a type from 1 for Circle, 2 for Rectangle, 3 for Triangle or 4 to Quit to exit ");
shapeID = keyboard.nextInt();
}
System.out.println("Goodbye!");
}
//This method will compute the area of circle
public static void isCircle(Scanner keyboard) {
System.out.println("Please enter radius of the circle: ");
int radius = keyboard.nextInt();
keyboard.nextLine();
if (radius >= 0) {
//object of Shape class is created to define the dimensions for circle
Shape shapeOne = new Shape(radius);
// compute the area of the circle
// complete the code here
//ToString method in Shape class display the information is specific format
System.out.println(shapeOne.toString());
//Check if dimensions need to be updated
System.out.println("Do you want to compute the area with different dimensions: ");
String answer = keyboard.nextLine();
if (answer.equalsIgnoreCase("yes")) {
System.out.println("Please enter the updated parameter: ");
int param = keyboard.nextInt();
keyboard.nextLine();
//Calling update parameter method to set the new dimensions
updatedParameter(shapeOne, param);
//toString method in Shape class display the information is specific format
System.out.println(shapeOne.toString());
}
} else {
System.out.println("Value of radius is invalid, please try again ");
}
}
//This method will compute the area of Rectangle
public static void isRectangle(Scanner keyboard) {
System.out.println("Please enter sideA of a rectangle: ");
int sideA = keyboard.nextInt();
System.out.println("Please enter sideB of a rectangle: ");
int sideB = keyboard.nextInt();
keyboard.nextLine();
if(sideA >= 0 && sideB >= 0) {
//object of Shape class to define the dimensions of a rectangle
Shape shapeTwo = new Shape(sideA, sideB);
//compute and print the area of the rectangle
System.out.println(shapeTwo.toString());
//check if dimensions need to be updated
System.out.println("Do you want to compute the area with different dimensions: ");
String answer = keyboard.nextLine();
if (answer.equalsIgnoreCase("yes")) {
System.out.println("Please enter the updated parameter: ");
int param = keyboard.nextInt();
keyboard.nextLine();
//calling update parameter method to set the new dimensions
updatedParameter(shapeTwo, param);
//calculate and print new area
System.out.println(shapeTwo.toString());
}
} else {
System.out.println("Value of side is invalid, please try again ");
}
}
//This method will compute the area of triangle
public static void isTriangle(Scanner keyboard) {
System.out.println("Please enter sideA of a triangle: ");
int sideA = keyboard.nextInt();
System.out.println("Please enter sideB of a triangle: ");
int sideB = keyboard.nextInt();
System.out.println("Please enter sideC of a triangle: ");
int sideC = keyboard.nextInt();
keyboard.nextLine();
if(sideA >= 0 && sideB >= 0 && sideC >= 0) {
//object of Shape class to define the dimensions of a triangle
Shape shapeThree = new Shape(sideA, sideB);
//compute and print the area of the triangle
System.out.println(shapeThree.toString());
//check if dimensions need to be updated
System.out.println("Do you want to compute the area with different dimensions: ");
String answer = keyboard.nextLine();
if (answer.equalsIgnoreCase("yes")) {
System.out.println("Please enter the updated parameter: ");
int param = keyboard.nextInt();
keyboard.nextLine();
//calling update parameter method to set the new dimensions
updatedParameter(shapeThree, param);
//calculate and print new area
System.out.println(shapeThree.toString());
}
} else {
System.out.println("Value of side is invalid, please try again ");
}
}
// ToDo: Write this method to update first parameter of the triangle, circle or rectangle
public static void updatedParameter(Shape shapeToUpdate, int param) {
//updates the first side of the shape
shapeToUpdate.setSideA(param);
// computes the area
shapeToUpdate.getArea();
}
}