1. C:UsersToriworkspaceIntegrationTorisrcINtegrationTori.java Thursday, April 14, 2016 5:11 PM
1 import java.util.ArrayList;
2 import java.util.Scanner;
3 import java.util.Stack;
4
5 import javax.swing.plaf.synth.SynthSeparatorUI;
6
7 //Tori Hoff Integration Project for COP 2006 this is
8 //a collaboration of all of my projects so far
9 public class INtegrationTori {
10
11 public static void main(String[] args) {
12 Scanner keyboard = new Scanner(System.in);
13 System.out.println("Hello this is my integration Project. Everything"
14 + " following is the project i have been working on" + " all year in COP 2006.");
15 String cont = "continue";
16 do {// while loop
17 System.out.println("Please choose the following program you would like to run");
18 System.out.println("1. For Echo on your input");
19 System.out.println("2. for Integer Division");
20 System.out.println("3. For birthday Switch Statement");
21 System.out.println("4. for more integer division");
22 System.out.println("5. for double IF statement");
23 System.out.println("6. for sum of entered digits loop");
24 System.out.println("7. for sum of integers within a number");
25 System.out.println("8. for a for Loop");
26 System.out.println("9. for your favorite number exercise");
27 System.out.println("10. to learn what my favorite song is ");
28 System.out.println("11. for a parallel array on the days in a month ");
29 System.out.println("12. for an array to find the largest integer ");
30 System.out.println("13. stack exercise ");
31 System.out.println("14. to work with strings ");
32 System.out.println("15. For a 2d array ");
33 System.out.println("16. To print the smallest value in an array ");
34 System.out.println("17. For an array list ");
35 System.out.println("18. for example of inheritance");
36
37 try {
38 int project;
39 project = keyboard.nextInt();
40 switch (project) {
41 case 1:
42 echo(keyboard);// keyboard is argument to my call of the echo method
43 break;
44 case 2:
45 integerDivision();
46 break;
47 case 3:
48 findingBirthday(keyboard);
49 break;
50 case 4:
51 divisionIf(keyboard);
52 break;
53 case 5:
54 returnValue();
55 break;
56 case 6:
57 runningTotal(keyboard);
58 break;
59 case 7:
60 getNumber(keyboard);
61 break;
62 case 8:
63 forLoop(keyboard);
64 break;
65 case 9:
66 relational(keyboard);
-1-
2. C:UsersToriworkspaceIntegrationTorisrcINtegrationTori.java Thursday, April 14, 2016 5:11 PM
67 break;
68 case 10:
69 Jukebox myjukebox = new Jukebox();// object myjukebox similar to a
70 // call to a special method
71 myjukebox.setSong("Beautiful Soul");// argument
72 myjukebox.play();
73 break;
74 case 11:
75 anArray();
76 break;
77 case 12:
78 largestArray();
79 break;
80 case 13:
81 stackDemo(keyboard);
82 break;
83 case 14:
84 stringPlay(keyboard);
85 break;
86 case 15:
87 arrayTwo();
88 break;
89 case 16:
90 smallestValue();
91 break;
92 case 17:
93 anotherArray(keyboard);
94 break;
95 case 18:
96 inheritance();
97 break;
98 }
99 System.out.println("Thank you for choosing number " + project + " :)");
100 } catch (Exception e) {
101 System.out.println("Error");
102 }
103 System.out.println("Please enter continue to continue the program");
104 cont = keyboard.next();
105 } while (cont.equals("continue"));
106 }
107
108 public static String findingBirthday(Scanner keyboard) {// parameter
109 // switch
110 try {
111 System.out.println("Enter the your birthday month number");
112 int month;
113 month = keyboard.nextInt();
114 String birthdayMonth = "Invalid Input";
115 switch (month) {
116 case 1:
117 birthdayMonth = "January";
118 break;
119 case 2:
120 birthdayMonth = "Febuary";
121 break;
122 case 3:
123 birthdayMonth = "March";
124 break;
125 case 4:
126 birthdayMonth = "April";
127 break;
128 case 5:
129 birthdayMonth = "May";
130 break;
131 case 6:
132 birthdayMonth = "June";
-2-
3. C:UsersToriworkspaceIntegrationTorisrcINtegrationTori.java Thursday, April 14, 2016 5:11 PM
133 break;
134 case 7:
135 birthdayMonth = "July";
136 break;
137 case 8:
138 birthdayMonth = "August";
139 break;
140 case 9:
141 birthdayMonth = "September";
142 break;
143 case 10:
144 birthdayMonth = "October";
145 break;
146 case 11:
147 birthdayMonth = "November:";
148 break;
149 case 12:
150 birthdayMonth = "December";
151 break;
152 default:
153 System.out.println("INVALID");
154 }
155 System.out.println("Your birthday is in " + birthdayMonth);
156 return birthdayMonth;
157 } catch (Exception e) {
158 System.out.println("Error");
159 }
160 return null;
161 }
162
163 public static void returnValue() {
164 try {
165 double aNumber = 3;// double if statement and declared variable of type
166 // double to equal 3
167 if (aNumber >= 0) {
168 if (aNumber == 0) {
169 System.out.println("3 is equal to 0");
170 } else {
171 System.out.println("3 is not equal to 0");
172 }
173 }
174 System.out.println("3 is greater than 0");
175 } catch (Exception e) {
176 System.out.println("Error");
177 }
178 }
179
180 public static void divisionIf(Scanner keyboard) {
181 try {
182 System.out.println("Enter any integer (divident): ");
183 double divident = keyboard.nextInt();
184 System.out.println("Enter any integer (divisor): ");
185 double divisor = keyboard.nextInt();
186 System.out.println("The (rounded) answer for " + divident + " divided by " +
divisor + " is: ");
187 if (divisor == 0) {
188 System.out.println("Cannot divide by 0 ");
189 } else {
190 System.out.println(divident / divisor);
191 }
192 } catch (Exception e) {
193 System.out.println("Error");
194 }
195 }
196
197 public static void integerDivision() {
-3-
4. C:UsersToriworkspaceIntegrationTorisrcINtegrationTori.java Thursday, April 14, 2016 5:11 PM
198 /*
199 * integer division, (make sure you either use double or a decimal because
200 * an integer divided by an integer does not work all of these 'variables'
201 * that i am assigning numbers to are holding the values that i give them...
202 * they hold value
203 */
204 try {
205 double num1 = 3;
206 double num2 = 15;
207 double num3 = 10;
208 num2 += num3;
209 System.out.println(num2);
210 System.out.println(num2 / num1);
211 System.out.println(++num1 + 1);
212 System.out.println(num1++ + 1);// pay attention to if you do the ++before
213 // or after the variable
214 System.out.println(num1);
215 System.out.println(num2 * num1);
216 } catch (Exception e) {
217 System.out.println("Error");
218 }
219 }
220
221 public static void echo(Scanner keyboard) {
222 System.out.println("Please enter what you want to be echoed: ");
223 String someText = keyboard.next();
224 String echo = "";
225 echo = someText;
226 System.out.println(echo);
227 }
228
229 public static void runningTotal(Scanner keyboard) {
230 try {
231 int numEntered;
232 int numSum = 0;
233 int count = 1;
234 System.out.println("This program will sum the five integers you input");
235 while (count < 6) {
236 System.out.println("Please enter your " + count + "number ");
237 numEntered = keyboard.nextInt();
238 numSum += numEntered;
239 count++;
240 }
241 System.out.println("The total sum is " + numSum);
242 } catch (Exception e) {
243 System.out.println("Error");
244 }
245 }
246
247 public static void getNumber(Scanner keyboard) {
248 System.out.println("This progrm will add up the integers in the" + " number you
enter, please enter a number ");
249 try {
250 int subscript = 0;
251 String numEntered = keyboard.nextLine();
252 int numSum = 0;
253 while (subscript < numEntered.length()) {
254 char aChar = numEntered.charAt(subscript);
255 int x = Character.getNumericValue(aChar);
256 numSum = numSum + x;
257 subscript++;
258 }
259 System.out.println(numSum);
260 } catch (Exception e) {
261 System.out.println("Error");
262 }
-4-
5. C:UsersToriworkspaceIntegrationTorisrcINtegrationTori.java Thursday, April 14, 2016 5:11 PM
263 }
264
265 public static void forLoop(Scanner keyboard) {
266 int startNum;
267 try {
268 System.out.println("Enter the number:");
269 startNum = keyboard.nextInt();
270 for (; startNum >= 1; startNum--) {
271 System.out.println(startNum);
272 }
273 } catch (Exception e) {
274 System.out.println("Error");
275 }
276 }
277
278 public static void relational(Scanner keyboard) {
279 int numChoice = 0;
280 try {
281
282 System.out.println("Enter your favorite number");
283 numChoice = keyboard.nextInt();
284 if (numChoice >= 10 || numChoice <= 50) {
285 System.out.println("Your favorite number is between 10 and 50");
286 } else {
287 throw new ArithmeticException();
288 }
289 } catch (ArithmeticException a) {
290 System.out.println("You entered the boring number " + numChoice);
291 } catch (Exception e) {
292 System.out.println("That cant be your favorite number its not a number");
293 }
294 }
295
296 public static void anArray() {
297 int[] numDays = { 31, 28, 31 };
298 String[] months = { "Jan", "Feb", "Mar" };
299 for (int i = 0; i < numDays.length; i++) {
300 System.out.println(months[i] + " has " + numDays[i] + " days");
301 }
302
303 }
304
305 public static void largestArray() {
306 int largest;
307 int sum = 0;
308 int indexOfLargest = 0;
309 int[] array1 = { 100, 200, 300 };
310 largest = array1[0];
311 for (int i = 0; i < array1.length; i++) {
312 if (largest < array1[i]) {
313 sum += array1[i];
314 largest = array1[i];
315 indexOfLargest = i;
316 }
317 }
318
319 System.out.println("Largest: " + largest + " Sum " + sum + "Found at index: " +
indexOfLargest);
320 }
321
322 public static void stackDemo(Scanner keyboard) {
323 try{
324 Stack s = new Stack();
325 int a = 0;
326 int b = 0;
327 System.out.println("Enter number ");
-5-
6. C:UsersToriworkspaceIntegrationTorisrcINtegrationTori.java Thursday, April 14, 2016 5:11 PM
328 a = keyboard.nextInt();
329 s.push(a);
330 System.out.println("Stack: " + s);
331 System.out.println("Enter another number ");
332 b = keyboard.nextInt();
333 s.push(b);
334 System.out.println("Stack is now : " + s);
335 }
336 catch(Exception e){
337 System.out.println("Error");
338 }
339
340 }
341
342 public static void stringPlay(Scanner keyboard) {
343 try{
344 String fullName = "";
345 String firstName = "";
346 String lastName = "";
347 System.out.println("Enter the full name:");
348 fullName = keyboard.nextLine();
349
350 System.out.println("The names after split is:");
351
352 String[] split = fullName.split(" ");
353 firstName = split[0];
354 lastName = split[1];
355
356 System.out.println(firstName + "' " + lastName);
357 }
358 catch (Exception e){
359 System.out.println("Error");
360 }
361 }
362
363 public static void arrayTwo() {
364 int search = 2;
365 int[][] math = new int[3][3];
366 for (int i = 0; i < math.length; i++) {
367 for (int j = 0; j < math[i].length; j++) {
368 math[i][j] = i * j;
369 }
370 }
371 for (int i = 0; i < math.length; i++) {
372 for (int j = 0; j < math[i].length; j++) {
373 System.out.print(math[i][j]);
374 }
375 System.out.println();
376 }
377 for (int i = 0; i < math.length; i++) {
378 for (int j = 0; j < math[i].length; j++) {
379 if (search == math[i][j]) {
380 System.out.println(search + " is found at row: " + i + " and column: " + j);
381 }
382
383 }
384 }
385 }
386
387 public static void smallestValue() {
388 int smallest;
389 int[] array1 = { 100, 200, 300 };
390 smallest = array1[0];
391 for (int i = 0; i < array1.length; i++) {
392 if (smallest > array1[i]) {
393 smallest = array1[i];
-6-
7. C:UsersToriworkspaceIntegrationTorisrcINtegrationTori.java Thursday, April 14, 2016 5:11 PM
394 }
395 System.out.println("smallest value is: " + smallest);
396 }
397 }
398
399 public static void anotherArray(Scanner keyboard) {
400 ArrayList<String> list = new ArrayList<String>();
401 list.add("Hello");
402 list.add(" my name is ");
403 String name = "";
404 System.out.println("Please enter your name ");
405 name = keyboard.nextLine();
406 list.add(name);
407 System.out.println(list);
408
409 }
410 public static void inheritance() {// this is inheritance because car IS A
411 // machine
412 Car mercedes = new Car();
413 Car bmw = new Car();
414 mercedes.start();
415 mercedes.stop();//object mercedes is using super class machine method stop in
subclass car which is polymorphic
416 mercedes.honk();
417 System.out.println("Your car speed is: ");
418 mercedes.setSpeed(70);
419 System.out.println(mercedes.getSpeed());
420 Machine[] garage = new Machine[2];
421 bmw.setMake("BMW");
422 garage[0] = mercedes;
423 garage[1] = bmw;
424 mercedes.setMake("Mercedes"); //this array shows polymorphism because i can use the
methods
425 //from two different classes together because one is
a super class and one is a subclass
426 System.out.println("Here are the cars in your garage: ");
427 for (int i = 0; i< garage.length; i++){
428 System.out.println(garage[i].getMake());
429 }
430 }
431
432 }// closing brackets leave these
433
434 class Jukebox {
435 String song;
436
437 public void setSong(String s) {// string s is a parameter
438 this.song = s;
439 }// this is a type of object for this song
440
441 public String getSong() {
442 return song;
443 }
444
445 public void play() {
446 System.out.println(getSong());
447 }
448 }
449
450 class Machine {
451 private String make;
452 public void start() {
453 System.out.println("Machine has started");
454 }
455
456 public void stop() {
-7-
8. C:UsersToriworkspaceIntegrationTorisrcINtegrationTori.java Thursday, April 14, 2016 5:11 PM
457 System.out.println("Machine has stopped");
458 }
459 public String getMake(){
460 return make;
461 }
462 public void setMake(String car){
463 make = car;
464 }
465 }
466
467 class Car extends Machine {//car IS A machine therefore all following methods car can use
468 //car is polymorphic because it is a type of machine and has multiple inheritance
469 private int speed;
470 public int getSpeed(){
471 return speed;
472 }
473 public void setSpeed(int s){
474 speed = s;
475 }
476
477 public void honk() {
478 System.out.println("Car is honking ");
479 }
480 }
481
-8-