-
Notifications
You must be signed in to change notification settings - Fork 2
/
dsamidterm.java
318 lines (309 loc) · 17.6 KB
/
dsamidterm.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
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
import java.util.*;
public class dsamidterm {
static Scanner input = new Scanner(System.in);
public static void main(String[] args) {
boolean menu = false;
do {
System.out.println("Welcome to MIDTERM Practical Exam, please choose a lesson to start an exercise: ");
System.out.println(" A. Basic Display of Hello World");
System.out.println(" B. Using Arithmetic Operation");
System.out.println(" C. Using Decision Control Structures");
System.out.println(" D. Using Single Array");
System.out.println(" E. Using Stack and Queue");
System.out.println(" F. Exit the program");
System.out.print("Enter your choice here =>");
String choice = input.nextLine();
if (!(choice.equalsIgnoreCase("A") || choice.equalsIgnoreCase("B")
|| choice.equalsIgnoreCase("C") || choice.equalsIgnoreCase("D")
|| choice.equalsIgnoreCase("D") || choice.equalsIgnoreCase("E")
|| choice.equalsIgnoreCase("F"))) {
System.out.println("Wrong input, please try again");
System.out.println("Welcome to MIDTERM Practical Exam, please choose a lesson to start an exercise: ");
System.out.println(" A. Basic Display of Hello World");
System.out.println(" B. Using Arithmetic Operation");
System.out.println(" C. Using Decision Control Structures");
System.out.println(" D. Using Single Array");
System.out.println(" E. Using Stack and Queue");
System.out.println(" F. Exit the program");
System.out.print("Enter your choice here =>");
choice = input.nextLine();
}
if (choice.equalsIgnoreCase("A")) {
System.out.println("Basic Display of Hello World");
System.out.println("This is the basic structure of displaying a String:");
System.out.println("public class hello {");
System.out.println(" public static void main (String[] args) {");
System.out.println(" System.out.println(\"Hello World\");");
System.out.println("}}");
System.out.print("Do you want to try again[Y] to return to Basic Display " +
"of Hello World menu and [N] to return to the main Menu. =>");
String tryChoice = input.nextLine();
while (tryChoice.equalsIgnoreCase("Y")) {
System.out.println("Basic Display of Hello World");
System.out.println("This is the basic structure of displaying a String:");
System.out.println("public class hello {");
System.out.println(" public static void main (String[] args) {");
System.out.println(" System.out.println(\"Hello World\");");
System.out.println("}}");
System.out.print("Do you want to try again[Y] to return to Basic Display " +
"of Hello World menu and [N] to return to the main Menu. =>");
tryChoice = input.nextLine();
}
if (tryChoice.equalsIgnoreCase("N")) {
menu = true;
}
}
if (choice.equalsIgnoreCase("B")) {
System.out.println("Using Arithmetic Operation");
System.out.println("Enter two integers:");
System.out.print("Enter the first integer=>");
int firstInt = input.nextInt();
System.out.print("Enter the second integer=>");
int secondInt = input.nextInt();
input.nextLine();
System.out.println("a. Sum =>" + (firstInt + secondInt));
System.out.println("b. Difference=>" + (firstInt - secondInt));
System.out.println("c. Product=>" + (firstInt * secondInt));
double quotient = (double) firstInt / (double) secondInt;
System.out.println("d. Quotient=>" + quotient);
System.out.print("Do you want to try again[Y] to return to Using Arithmetic Operation" +
" menu and [N] to return to the main Menu. =>");
String tryChoice = input.nextLine();
while (tryChoice.equalsIgnoreCase("Y")) {
System.out.println("Using Arithmetic Operation");
System.out.println("Enter two integers:");
System.out.print("Enter the first integer=>");
firstInt = input.nextInt();
System.out.print("Enter the second integer=>");
secondInt = input.nextInt();
input.nextLine();
System.out.println("a. Sum =>" + (firstInt + secondInt));
System.out.println("b. Difference=>" + (firstInt - secondInt));
System.out.println("c. Product=>" + (firstInt * secondInt));
quotient = (double) firstInt / (double) secondInt;
System.out.println("d. Quotient=>" + quotient);
System.out.print("Do you want to try again[Y] to return to Using Arithmetic Operation " +
"menu and [N] to return to the main Menu. =>");
tryChoice = input.nextLine();
}
if (tryChoice.equalsIgnoreCase("N")) {
menu = true;
}
}
if (choice.equalsIgnoreCase("C")) {
System.out.println("Using Decision Control Structures");
System.out.println(" Determine an Integer it is ODD or EVEN");
System.out.println(" A. IF Statement");
System.out.println(" B. IF ELSE STATEMENT");
System.out.println(" C. IF ELSE IF STATEMENT");
System.out.print(" Enter your choice here =>");
String option = input.nextLine();
System.out.print(" Enter an Integer=>");
int number = input.nextInt();
input.nextLine();
if (option.equalsIgnoreCase("A")) {
if (number % 2 == 0)
System.out.println("The integer " + number + " is an " + "EVEN number.");
if (number % 2 != 0)
System.out.println("The integer " + number + " is an " + "ODD number.");
} else if (option.equalsIgnoreCase("B")) {
if (number % 2 == 0)
System.out.println("The integer " + number + " is an " + "EVEN number.");
else
System.out.println("The integer " + number + " is an " + "ODD number.");
} else if (option.equalsIgnoreCase("C")) {
if (number % 2 == 0)
System.out.println("The integer " + number + " is an " + "EVEN number.");
else if (number % 2 != 0)
System.out.println("The integer " + number + " is an " + "ODD number.");
}
System.out.print("Do you want to try again[Y] to return to Using Decision Control Structures menu " +
"and [N] to return to the main Menu. =>");
String tryChoice = input.nextLine();
while (tryChoice.equalsIgnoreCase("Y")) {
System.out.println("Using Decision Control Structures");
System.out.println(" Determine an Integer it is ODD or EVEN");
System.out.println(" A. IF Statement");
System.out.println(" B. IF ELSE STATEMENT");
System.out.println(" C. IF ELSE IF STATEMENT");
System.out.print(" Enter your choice here =>");
option = input.nextLine();
System.out.print(" Enter an Integer=>");
number = input.nextInt();
input.nextLine();
if (option.equalsIgnoreCase("A")) {
if (number % 2 == 0)
System.out.println("The integer " + number + " is an " + "EVEN number.");
if (number % 2 != 0)
System.out.println("The integer " + number + " is an " + "ODD number.");
} else if (option.equalsIgnoreCase("B")) {
if (number % 2 == 0)
System.out.println("The integer " + number + " is an " + "EVEN number.");
else
System.out.println("The integer " + number + " is an " + "ODD number.");
} else if (option.equalsIgnoreCase("C")) {
if (number % 2 == 0)
System.out.println("The integer " + number + " is an " + "EVEN number.");
else if (number % 2 != 0)
System.out.println("The integer " + number + " is an " + "ODD number.");
}
System.out.print("Do you want to try again[Y] to return to Using Decision Control Structures menu" +
" and [N] to return to the main Menu. =>");
tryChoice = input.nextLine();
}
if (tryChoice.equalsIgnoreCase("N")) {
menu = true;
}
}
if (choice.equalsIgnoreCase("D")) {
int[] numbers = new int[2];
String[] names = new String[2];
System.out.println("D. Using Single Array");
System.out.print("Enter the Start number: ");
numbers[0] = input.nextInt();
System.out.print("Enter the Last number: ");
numbers[1] = input.nextInt();
input.nextLine();
System.out.print("Enter First name: ");
names[0] = input.nextLine();
System.out.print("Enter Last name: ");
names[1] = input.nextLine();
System.out.println("Display Result: ");
if (numbers[0] < numbers[1]) {
for (int i = numbers[0]; i <= numbers[1]; i++) {
System.out.println(names[0] + "_" + i + "_" + names[1]);
}
} else {
for (int i = numbers[0]; i >= numbers[1]; i--) {
System.out.println(names[0] + "_" + i + "_" + names[1]);
}
}
System.out.print("Do you want to try again[Y] to return to Single Array menu " +
"and [N] to return to the main Menu. =>");
String tryChoice = input.nextLine();
while (tryChoice.equalsIgnoreCase("Y")) {
System.out.println("D. Using Single Array");
System.out.print("Enter the Start number: ");
numbers[0] = input.nextInt();
System.out.print("Enter the Last number: ");
numbers[1] = input.nextInt();
input.nextLine();
System.out.print("Enter First name: ");
names[0] = input.nextLine();
System.out.print("Enter Last name: ");
names[1] = input.nextLine();
System.out.println("Display Result: ");
if (numbers[0] < numbers[1]) {
for (int i = numbers[0]; i <= numbers[1]; i++) {
System.out.println(names[0] + "_" + i + "_" + names[1]);
}
} else {
for (int i = numbers[0]; i >= numbers[1]; i--) {
System.out.println(names[0] + "_" + i + "_" + names[1]);
}
}
System.out.print("Do you want to try again[Y] to return to Single Array menu " +
"and [N] to return to the main Menu. =>");
tryChoice = input.nextLine();
}
if (tryChoice.equalsIgnoreCase("N")) {
menu = true;
}
}
if (choice.equalsIgnoreCase("E")) {
System.out.println("Using Stack and Queue");
System.out.println("Stack");
System.out.println("Queue");
System.out.print("Enter your choice here=>");
String methodChoice = input.nextLine();
if (methodChoice.equalsIgnoreCase("Stack")) {
Stack<String> stack = new Stack<>();
System.out.println("You choose Stack Method!");
System.out.println("Enter Five Names: ");
for (int i = 0; i < 5; i++) {
System.out.print("Enter Name#"+(i+1)+":");
String name = input.nextLine();
stack.push(name);
}
System.out.println("Stack=>" + stack);
System.out.println("Stack.peek()=>" + stack.peek());
System.out.println("Stack.size()=>" + stack.size());
System.out.println("Stack.pop()=>" + stack.pop());
System.out.println();
System.out.println("Stack=>" + stack);
System.out.println("Is Stack Empty =>" + stack.isEmpty());
} else if (methodChoice.equalsIgnoreCase("Queue")) {
Queue<String> queue = new LinkedList<>();
System.out.println("You choose Queue Method!");
System.out.println("Enter Five Names: ");
for (int i = 0; i < 5; i++) {
System.out.print("Enter Name#"+(i+1)+":");
String name = input.nextLine();
queue.add(name);
}
System.out.println("Queue=>" + queue);
System.out.println("Queue.peek()=>" + queue.peek());
System.out.println("Queue.size()=>" + queue.size());
System.out.println("Queue.remove()=>" + queue.remove());
System.out.println();
System.out.println("Queue=>" + queue);
System.out.println("Is Queue Empty =>" + queue.isEmpty());
}
System.out.print("Do you want to try again[Y] to return to Using Stack and Queue Menu " +
"and [N] to return to the main Menu. =>");
String tryChoice = input.nextLine();
while (tryChoice.equalsIgnoreCase("Y")) {
System.out.println("Using Stack and Queue");
System.out.println("Stack");
System.out.println("Queue");
System.out.print("Enter your choice here=>");
methodChoice = input.nextLine();
if (methodChoice.equalsIgnoreCase("Stack")) {
Stack<String> stack = new Stack<>();
System.out.println("You choose Stack Method!");
System.out.println("Enter Five Names: ");
for (int i = 0; i < 5; i++) {
System.out.print("Enter Name#"+(i+1)+":");
String name = input.nextLine();
stack.push(name);
}
System.out.println("Stack=>" + stack);
System.out.println("Stack.peek()=>" + stack.peek());
System.out.println("Stack.size()=>" + stack.size());
System.out.println("Stack.pop()=>" + stack.pop());
System.out.println();
System.out.println("Stack=>" + stack);
System.out.println("Is Stack Empty =>" + stack.isEmpty());
} else if (methodChoice.equalsIgnoreCase("Queue")) {
Queue<String> queue = new LinkedList<>();
System.out.println("You choose Queue Method!");
System.out.println("Enter Five Names: ");
for (int i = 0; i < 5; i++) {
System.out.print("Enter Name#"+(i+1)+":");
String name = input.nextLine();
queue.add(name);
}
System.out.println("Queue=>" + queue);
System.out.println("Queue.peek()=>" + queue.peek());
System.out.println("Queue.size()=>" + queue.size());
System.out.println("Queue.remove()=>" + queue.remove());
System.out.println();
System.out.println("Queue=>" + queue);
System.out.println("Is Queue Empty =>" + queue.isEmpty());
}
System.out.print("Do you want to try again[Y] to return to Using Stack and Queue Menu " +
"and [N] to return to the main Menu. =>");
tryChoice = input.nextLine();
}
if (tryChoice.equalsIgnoreCase("N")) {
menu = true;
}
}
if (choice.equalsIgnoreCase("F")) {
System.out.println("Exiting Program...");
System.exit(0);
}
}
while (menu);
}
}