C:UsersOwnerworkspaceIntegration ProjectsrcPS1 - Copy.java Sunday, May 01, 2016 3:31 PM
1 import java.util.Scanner;
2
3 import jdk.nashorn.internal.runtime.regexp.joni.exception.InternalException;
4
5 public class PS1
6 {
7 public static void main(String[] args)
8 {
9 Scanner input = new Scanner(System.in);
10 String name = null;
11 int num1 = 0;
12 int num2 = 0;
13 int sum;
14
15 // A variable is simply a place in memory. In this case, I have declared
16 // three variables with the data type int.
17
18 System.out.println("Hello and welcome to Joe's Integration Project! "
19 + "Please input a name that will be used as personal "
20 + "reference throughout the course of this project.");
21
22 try
23 {
24 name = input.nextLine();
25 }
26
27 catch (ClassCastException e)
28 {
29 System.out.println("Please enter a name.");
30 }
31
32 System.out.println("Okay! The name you'd like to use is " + name
33 + ". Got " + "it.");
34 System.out.println("Let's get started by doing some basic math with "
35 + "two integers, beginning with addition. Please enter "
36 + "the first integer.");
37
38 //Here, I am implementing exception handling for num1. For example,
39 //explicitly inputting a double such as 5.6 would produce an operation
40 //error.
41 try
42 {
43 num1 = input.nextInt();
44
45 System.out.print("Thank you, " + name + ". ");
46 System.out.println("Please enter the second integer.");
47 num2 = input.nextInt();
48 }
49
50 catch (Exception e)
51 {
52 System.out.println("Sorry, please enter an integer.");
53 }
54
55 sum = num1 + num2;
56 System.out.println("I see...The answer is " + sum + ".");
57
-1-
C:UsersOwnerworkspaceIntegration ProjectsrcPS1 - Copy.java Sunday, May 01, 2016 3:31 PM
58
59 double num3 = 0;
60 double num4 = 0;
61 double sum2;
62
63 // Unlike earlier in the program, I am using the data type double rather
64 // than int. These are merely two of the eight primitive Java data types.
65 // The following is a list and description of all eight data types:
66
67 // 1. byte: The byte data type is an 8-bit signed two's complement
68 // integer
69 // It has a minimum value of -128 and a maximum value of 127
70 // (inclusive).
71 // The byte data type can be useful for saving memory in large arrays,
72 // where
73 // the memory savings actually matters. They can also be used in place
74 // of
75 // int where their limits help to clarify your code; the fact that a
76 // variable's range is limited can serve as a form of documentation.
77
78 // 2. short: The short data type is a 16-bit signed two's complement
79 // integer.
80 // It has a minimum value of -32,768 and a maximum value of 32,767
81 // (inclusive). As with byte, the same guidelines apply: you can use a
82 // short
83 // to save memory in large arrays, in situations where the memory
84 // savings
85 // actually matters.
86
87 // 3. int: By default, the int data type is a 32-bit signed two's
88 // complement integer, which has a minimum value of -231 and a maximum
89 // value
90 // of 231-1. In Java SE 8 and later, you can use the int data type to
91 // represent an unsigned 32-bit integer, which has a minimum value of 0
92 // and
93 // a maximum value of 232-1. Use the Integer class to use int data type
94 // as
95 // an unsigned integer.
96
97 // 4. long: The long data type is a 64-bit two's complement integer.
98 // The signed long has a minimum value of -263 and a maximum value of
99 // 263-1.
100 // In Java SE 8 and later, you can use the long data type to represent
101 // an
102 // unsigned 64-bit long, which has a minimum value of 0 and a maximum
103 // value
104 // of 264-1. Use this data type when you need a range of values wider
105 // than
106 // those provided by int. The Long class also contains methods like
107 // compareUnsigned, divideUnsigned etc to support arithmetic operations
108 // for
109 // unsigned long.
110
111 // 5. float: The float data type is a single-precision 32-bit IEEE 754
112 // floating point. Its range of values is beyond the scope of this
113 // discussion, but is specified in the Floating-Point Types, Formats,
114 // and
-2-
C:UsersOwnerworkspaceIntegration ProjectsrcPS1 - Copy.java Sunday, May 01, 2016 3:31 PM
115 // Values section of the Java Language Specification. As with the
116 // recommendations for byte and short, use a float (instead of double)
117 // if
118 // you need to save memory in large arrays of floating point numbers.
119
120 // 6. double: The double data type is a double-precision 64-bit IEEE
121 // 754 floating point. For decimal values, this data type is generally
122 // the
123 // default choice. As mentioned above, this data type should never be
124 // used
125 // for precise values, such as currency.
126
127 // 7. boolean: The boolean data type has only two possible values:
128 // true and false. Use this data type for simple flags that track
129 // true/false
130 // conditions. This data type represents one bit of information, but its
131 // "size" isn't something that's precisely defined.
132
133 // 8.char: The char data type is a single 16-bit Unicode character. It
134 // has
135 // a minimum value of 'u0000' (or 0) and a maximum value of 'uffff'
136 // (or 65,535 inclusive).
137
138 System.out.println("I'm a pretty good program, right? I can also add "
139 + "numbers that have fractional values, such as 5.5 "
140 + "and -15. Let's give it a try.");
141
142 System.out.println("Please enter the first number you'd like to add.");
143 try
144 {
145 num3 = input.nextDouble();
146
147 System.out.println("Thank you " + name
148 + ". Please enter the second number" + ".");
149 num4 = input.nextDouble();
150 }
151
152 catch (Exception e)
153 {
154 System.out.print("Sorry, please enter an integer.");
155 }
156
157
158 int num5;
159 int num6;
160 int result1;
161 int result2;
162 float result3;
163
164 // Result 3 represents integer division. This variable is declared as a
165 // float in order to properly approximate the value of the quotient as
166 //having a decimal value or remainder, such as with the number 2.25.
167
168 sum2 = num3 + num4;
169 System.out.println("Excellent! The sum of those two numbers is " + sum2
170 + ".");
171 System.out.println("Alright, let's turn things up a bit. Now we'll use "
-3-
C:UsersOwnerworkspaceIntegration ProjectsrcPS1 - Copy.java Sunday, May 01, 2016 3:31 PM
172 + "two more integer values and output the results "
173 + "of subtracting, multiplying and dividing them.");
174
175 System.out.println("Please enter the first integer you'd like to use "
176 + "to demonstrate these mathematical operations.");
177 try
178 {
179 num5 = input.nextInt();
180
181 System.out.println("Okay, now for the second number that you'd like "
182 + "to use:");
183 num6 = input.nextInt();
184
185 result1 = num5 - num6;
186 result2 = num5 * num6;
187 result3 = (float) num5 / num6;
188 System.out.println("Thank you! ");
189 System.out.println("The difference is " + result1 + ".");
190 System.out.println("The product is " + result2 + ".");
191 System.out.println("The quotient is " + result3 + ".");
192
193 int num7;
194
195 System.out.println("Pretty cool, right? I know it is. For my next "
196 + "trick, we'll be determining if a chosen integer "
197 + "is even or odd. Go ahead, enter an integer "
198 + "and see what happens.");
199 num7 = input.nextInt();
200
201
202 // Here, I am going to establish conditions within an if else
203 //statement in order to determine if the user's integer is even
204 //or odd.
205
206 if (num7 % 2 == 0)
207 System.out.println("That number is..wait don't tell me, I got "
208 + "this..even, right? ");
209 else
210 System.out.println("Okay, let's see, that number is..carry the "
211 + "two..odd! ");
212 }
213
214 catch (Exception e)
215 {
216 System.out.println("Please make sure you entered an integer");
217 }
218
219
220 double Celsius = 0;
221 double Fahrenheit;
222
223 System.out.println("But that's not all. Ever wanted to know what a "
224 + "temperature in Celsius is in Fahrenheit? Just type in a "
225 + "temperature in Celsius and prepare to be blown away!" );
226 try
227 {
228 Celsius = input.nextDouble();
-4-
C:UsersOwnerworkspaceIntegration ProjectsrcPS1 - Copy.java Sunday, May 01, 2016 3:31 PM
229 }
230
231 catch (Exception e)
232 {
233 System.out.println("Please enter a double value");
234 }
235
236 Fahrenheit = ((Celsius * 9) / 5) + 32;
237 System.out.println("That temperature in Fahrenheit is " + Fahrenheit
238 + ". Boom, I just blew you away, didn't I? Don't deny it." );
239
240 System.out.println("Finally, let's have a quick lesson on driving a car "
241 + "with a manual transmission, in the case of how to "
242 + "drive off from a dead stop, when to shift during "
243 + "daily driving and how to come to a complete stop "
244 + "without stalling the car, such as in the event of "
245 + "a red traffic light.");
246
247 int choice = 0;
248
249 System.out.println("Please choose which event you would like to learn "
250 + "about by typing in a number, with 1 being the first "
251 + "event, 2 being the second event and 3 being the third "
252 + "event, respectively.");
253 try
254 {
255 choice = input.nextInt();
256 }
257
258 catch (Exception e)
259 {
260 System.out.println("Please enter either 1, 2, or 3.");
261 }
262
263 String eventString = null;
264 switch (choice) {
265 case 1:
266 eventString = "Fully depress the clutch and put the car in "
267 + "first gear. Then slowly give it gas and slowly let off "
268 + "of the clutch.";
269 break;
270 case 2:
271 eventString = "When to the point of moving after driving off in, "
272 + "first gear, once you reach about 2500 to 3000 rpm, "
273 + "shift to the next gear and continue to do so for "
274 + "all gears.";
275 break;
276 case 3:
277 eventString = "To come to a complete stop, simply depress the "
278 + "clutch fully and shift the car into neutral, or no "
279 + "gear. Then apply the brake pedal as necessary.";
280 break;
281 }
282
283 System.out.println(eventString);
284
285 System.out.println("Excellent! Thank you for your time thusfar. This "
-5-
C:UsersOwnerworkspaceIntegration ProjectsrcPS1 - Copy.java Sunday, May 01, 2016 3:31 PM
286 + "next section will begin by demonstrating relational "
287 + "and conditional operators.");
288
289 // Relational operators are tools in Java that are used to determine if
290 // an operand is less than, greater than and equal or not equal to
291 // another
292 // operand. There are six relational operators:
293 // 1. Equal to: ==
294 // 2. Not equal to: !=
295 // 3. Greater than: >
296 // 4. Greater than or equal to: >=
297 // 5. Less than: <
298 // 6. Less than or equal to: <=
299 // Conditional operators perform Conditional-AND and Conditional-OR
300 // operations on two boolean expressions. Therefore, there are two
301 // conditional operators:
302 // 1. && Conditional-AND
303 // 2. || Conditional-OR
304
305 int num8 = 0;
306 int num9 = 0;
307
308 System.out.println("Alright, I'm going to read two numbers from 1 to 10 "
309 + "and determine if they are either greater than or less than "
310 + "one another. Please enter the first number.");
311 try
312 {
313 num8 = input.nextInt();
314
315 System.out.println("Thank you! Now please enter the second number.");
316 num9 = input.nextInt();
317 }
318
319 catch (Exception e)
320 {
321 System.out.println("Invalid input. Please try again.");
322 }
323
324 //For this loop I am using a while loop with relational operators to
325 //compare two inputted numbers with a value from 1 to 10.
326 //The code in parentheses of the loop below would be considered
327 //arguments.
328 while (num8 < num9)
329 {
330 System.out.println(num9 + " is greater than " + num8 + ".");
331 break;
332 }
333 while (num8 > num9)
334 {
335 System.out.println(num9 + " is less than " + num8 + ".");
336 break;
337 }
338
339 //The following is a demonstration for a class that finds the radius and area
340 //of a circle:
341 //public class Testing
342 //{
-6-
C:UsersOwnerworkspaceIntegration ProjectsrcPS1 - Copy.java Sunday, May 01, 2016 3:31 PM
343 //private double radius;
344 //private double area;
345
346 //public Testing()
347 //{
348 //radius = 0;
349 //area = 0;
350 //}
351
352 //public Testing(double radius) <--this method contains a double parameter.
353 //{
354 //this.radius = radius;
355 //}
356
357 //public double getRadius()
358 //{
359 //return radius;
360 //}
361
362 //public void setRadius(double radius)
363 //{
364 //this.radius = radius;
365 //}
366
367 //public double getArea()
368 //{
369 //return area;
370 //}
371
372 //public void setArea(double area)
373 //{
374 //area = radius * radius * Math.PI;
375 //}
376
377 //public String toString()
378 //{
379 //return "The radius of the circle is: " + radius + ", and the "
380 //+ "area is: " + area;
381 //}
382
383 System.out.println("Awesome! For this next component, I will read 5 "
384 + "numbers from the user and determine the largest and "
385 + "smallest values. Please enter five integers.");
386
387 int[] anArray = new int[5];
388 int total = 0;
389
390 try
391 {
392 System.out.println("Integer 1:");
393 anArray[0] = input.nextInt();
394 total += anArray[0];
395
396 System.out.println("Integer 2:");
397 anArray[1] = input.nextInt();
398 total += anArray[1];
399
-7-
C:UsersOwnerworkspaceIntegration ProjectsrcPS1 - Copy.java Sunday, May 01, 2016 3:31 PM
400 System.out.println("Integer 3:");
401 anArray[2] = input.nextInt();
402 total += anArray[2];
403
404 System.out.println("Integer 4:");
405 anArray[3] = input.nextInt();
406 total+= anArray[3];
407
408 System.out.println("Integer 5:");
409 anArray[4] = input.nextInt();
410 total += anArray[4];
411
412 System.out.println("The value " + anArray[0] + " is found at "
413 + "index [0].");
414
415 } catch (ArrayStoreException e)
416 {
417 System.out.println("Please enter an integer.");
418 }
419
420 System.out.println("Thank you! The sum of these values is " + total
421 + ".");
422
423 int smallest = anArray[0];
424 int largest = anArray[0];
425
426 //Here, I am using a nested "if" loop within a for loop in order to
427 //determine the largest and smallest values within the array.
428
429 for (int index = 1; index < anArray.length; index++)
430 {
431 if (anArray[index] > largest)
432 largest = anArray[index];
433 else if (anArray[index] < smallest)
434 smallest = anArray[index];
435 }
436
437 System.out.println("The smallest value is " + smallest + ".");
438 System.out.println("The largest value is " + largest + ".");
439
440 //Two dimensional arrays are like standard arrays except that, as expected,
441 //they have two dimensions instead of one.
442
443 int[][] demo = new int [2][2];
444 demo [0][0] = 1;
445 demo [0][1] = 2;
446 demo [1][0] = 3;
447 demo [1][1] = 4;
448
449 System.out.println("For my 2d array demo, I will print the values from "
450 + "the first and second index places: ");
451
452 System.out.println("From first index: " + demo [0][0]);
453 System.out.println("From second index: " + demo [0][1]);
454
455
456 System.out.println("Now, to demonstrate string processing, I will "
-8-
C:UsersOwnerworkspaceIntegration ProjectsrcPS1 - Copy.java Sunday, May 01, 2016 3:31 PM
457 + "concatenate two strings and print the values of each one." );
458
459 String demo2 = "Happy";
460 String demo3 = "coding";
461
462 System.out.println("When concatenated: " + demo2 + demo3);
463
464 System.out.println("The length of the first string is " + demo2.length()
465 + ".");
466
467 System.out.println("The length of the second string is " + demo3.length()
468 + ".");
469
470 //One can think of the fact that a variety of objects, such as in real life,
471 //have certain characteristics in common with one another. In essence,
472 //classes are able to obtain commonly used state and behavior from other
473 //classes. For example:
474
475 //public class App {
476
477 //public static void main(String[] args) {
478
479 //Machine mach1 = new Machine();
480 //mach1.start();
481
482 //Person person1 = new Person("Bob");
483 //person1.greet();
484
485 //Info info1 = new Machine();
486 //info1.showInfo();
487
488 //Info info2 = person1;
489 //info2.showInfo();
490
491 //System.out.println();
492
493 //outputInfo(mach1);
494 //outputInfo(person1);
495 //}
496
497 //private static void outputInfo(Info info) {
498 //info.showInfo();
499 //}
500
501 //}
502
503 //As with polymorphism, the term is derived from its biology definition in
504 //which an organism or species can have various forms or stages.
505 //Ultimately, subclasses can define their own behaviors in addition to
506 //sharing similar functionality of the parent class. For example:
507
508 //public class App {
509
510 //public static void main(String[] args) {
511
512
513 //Plant plant1 = new Plant();
-9-
C:UsersOwnerworkspaceIntegration ProjectsrcPS1 - Copy.java Sunday, May 01, 2016 3:31 PM
514
515 //Tree tree = new Tree();
516
517 //Plant plant2 = tree;
518
519 //plant2.grow();
520
521 //tree.shedLeaves();
522
523
524 //doGrow(tree);
525 //}
526
527 //public static void doGrow(Plant plant) {
528 //plant.grow();
529 //}
530
531 //}
532
533 System.out.println("This is the conclusion of my program, thank you for "
534 + "your cooperation.");
535
536
537 }
538 }
539
540
541
542
543
544
-10-

PSI 3 Integration

  • 1.
    C:UsersOwnerworkspaceIntegration ProjectsrcPS1 -Copy.java Sunday, May 01, 2016 3:31 PM 1 import java.util.Scanner; 2 3 import jdk.nashorn.internal.runtime.regexp.joni.exception.InternalException; 4 5 public class PS1 6 { 7 public static void main(String[] args) 8 { 9 Scanner input = new Scanner(System.in); 10 String name = null; 11 int num1 = 0; 12 int num2 = 0; 13 int sum; 14 15 // A variable is simply a place in memory. In this case, I have declared 16 // three variables with the data type int. 17 18 System.out.println("Hello and welcome to Joe's Integration Project! " 19 + "Please input a name that will be used as personal " 20 + "reference throughout the course of this project."); 21 22 try 23 { 24 name = input.nextLine(); 25 } 26 27 catch (ClassCastException e) 28 { 29 System.out.println("Please enter a name."); 30 } 31 32 System.out.println("Okay! The name you'd like to use is " + name 33 + ". Got " + "it."); 34 System.out.println("Let's get started by doing some basic math with " 35 + "two integers, beginning with addition. Please enter " 36 + "the first integer."); 37 38 //Here, I am implementing exception handling for num1. For example, 39 //explicitly inputting a double such as 5.6 would produce an operation 40 //error. 41 try 42 { 43 num1 = input.nextInt(); 44 45 System.out.print("Thank you, " + name + ". "); 46 System.out.println("Please enter the second integer."); 47 num2 = input.nextInt(); 48 } 49 50 catch (Exception e) 51 { 52 System.out.println("Sorry, please enter an integer."); 53 } 54 55 sum = num1 + num2; 56 System.out.println("I see...The answer is " + sum + "."); 57 -1-
  • 2.
    C:UsersOwnerworkspaceIntegration ProjectsrcPS1 -Copy.java Sunday, May 01, 2016 3:31 PM 58 59 double num3 = 0; 60 double num4 = 0; 61 double sum2; 62 63 // Unlike earlier in the program, I am using the data type double rather 64 // than int. These are merely two of the eight primitive Java data types. 65 // The following is a list and description of all eight data types: 66 67 // 1. byte: The byte data type is an 8-bit signed two's complement 68 // integer 69 // It has a minimum value of -128 and a maximum value of 127 70 // (inclusive). 71 // The byte data type can be useful for saving memory in large arrays, 72 // where 73 // the memory savings actually matters. They can also be used in place 74 // of 75 // int where their limits help to clarify your code; the fact that a 76 // variable's range is limited can serve as a form of documentation. 77 78 // 2. short: The short data type is a 16-bit signed two's complement 79 // integer. 80 // It has a minimum value of -32,768 and a maximum value of 32,767 81 // (inclusive). As with byte, the same guidelines apply: you can use a 82 // short 83 // to save memory in large arrays, in situations where the memory 84 // savings 85 // actually matters. 86 87 // 3. int: By default, the int data type is a 32-bit signed two's 88 // complement integer, which has a minimum value of -231 and a maximum 89 // value 90 // of 231-1. In Java SE 8 and later, you can use the int data type to 91 // represent an unsigned 32-bit integer, which has a minimum value of 0 92 // and 93 // a maximum value of 232-1. Use the Integer class to use int data type 94 // as 95 // an unsigned integer. 96 97 // 4. long: The long data type is a 64-bit two's complement integer. 98 // The signed long has a minimum value of -263 and a maximum value of 99 // 263-1. 100 // In Java SE 8 and later, you can use the long data type to represent 101 // an 102 // unsigned 64-bit long, which has a minimum value of 0 and a maximum 103 // value 104 // of 264-1. Use this data type when you need a range of values wider 105 // than 106 // those provided by int. The Long class also contains methods like 107 // compareUnsigned, divideUnsigned etc to support arithmetic operations 108 // for 109 // unsigned long. 110 111 // 5. float: The float data type is a single-precision 32-bit IEEE 754 112 // floating point. Its range of values is beyond the scope of this 113 // discussion, but is specified in the Floating-Point Types, Formats, 114 // and -2-
  • 3.
    C:UsersOwnerworkspaceIntegration ProjectsrcPS1 -Copy.java Sunday, May 01, 2016 3:31 PM 115 // Values section of the Java Language Specification. As with the 116 // recommendations for byte and short, use a float (instead of double) 117 // if 118 // you need to save memory in large arrays of floating point numbers. 119 120 // 6. double: The double data type is a double-precision 64-bit IEEE 121 // 754 floating point. For decimal values, this data type is generally 122 // the 123 // default choice. As mentioned above, this data type should never be 124 // used 125 // for precise values, such as currency. 126 127 // 7. boolean: The boolean data type has only two possible values: 128 // true and false. Use this data type for simple flags that track 129 // true/false 130 // conditions. This data type represents one bit of information, but its 131 // "size" isn't something that's precisely defined. 132 133 // 8.char: The char data type is a single 16-bit Unicode character. It 134 // has 135 // a minimum value of 'u0000' (or 0) and a maximum value of 'uffff' 136 // (or 65,535 inclusive). 137 138 System.out.println("I'm a pretty good program, right? I can also add " 139 + "numbers that have fractional values, such as 5.5 " 140 + "and -15. Let's give it a try."); 141 142 System.out.println("Please enter the first number you'd like to add."); 143 try 144 { 145 num3 = input.nextDouble(); 146 147 System.out.println("Thank you " + name 148 + ". Please enter the second number" + "."); 149 num4 = input.nextDouble(); 150 } 151 152 catch (Exception e) 153 { 154 System.out.print("Sorry, please enter an integer."); 155 } 156 157 158 int num5; 159 int num6; 160 int result1; 161 int result2; 162 float result3; 163 164 // Result 3 represents integer division. This variable is declared as a 165 // float in order to properly approximate the value of the quotient as 166 //having a decimal value or remainder, such as with the number 2.25. 167 168 sum2 = num3 + num4; 169 System.out.println("Excellent! The sum of those two numbers is " + sum2 170 + "."); 171 System.out.println("Alright, let's turn things up a bit. Now we'll use " -3-
  • 4.
    C:UsersOwnerworkspaceIntegration ProjectsrcPS1 -Copy.java Sunday, May 01, 2016 3:31 PM 172 + "two more integer values and output the results " 173 + "of subtracting, multiplying and dividing them."); 174 175 System.out.println("Please enter the first integer you'd like to use " 176 + "to demonstrate these mathematical operations."); 177 try 178 { 179 num5 = input.nextInt(); 180 181 System.out.println("Okay, now for the second number that you'd like " 182 + "to use:"); 183 num6 = input.nextInt(); 184 185 result1 = num5 - num6; 186 result2 = num5 * num6; 187 result3 = (float) num5 / num6; 188 System.out.println("Thank you! "); 189 System.out.println("The difference is " + result1 + "."); 190 System.out.println("The product is " + result2 + "."); 191 System.out.println("The quotient is " + result3 + "."); 192 193 int num7; 194 195 System.out.println("Pretty cool, right? I know it is. For my next " 196 + "trick, we'll be determining if a chosen integer " 197 + "is even or odd. Go ahead, enter an integer " 198 + "and see what happens."); 199 num7 = input.nextInt(); 200 201 202 // Here, I am going to establish conditions within an if else 203 //statement in order to determine if the user's integer is even 204 //or odd. 205 206 if (num7 % 2 == 0) 207 System.out.println("That number is..wait don't tell me, I got " 208 + "this..even, right? "); 209 else 210 System.out.println("Okay, let's see, that number is..carry the " 211 + "two..odd! "); 212 } 213 214 catch (Exception e) 215 { 216 System.out.println("Please make sure you entered an integer"); 217 } 218 219 220 double Celsius = 0; 221 double Fahrenheit; 222 223 System.out.println("But that's not all. Ever wanted to know what a " 224 + "temperature in Celsius is in Fahrenheit? Just type in a " 225 + "temperature in Celsius and prepare to be blown away!" ); 226 try 227 { 228 Celsius = input.nextDouble(); -4-
  • 5.
    C:UsersOwnerworkspaceIntegration ProjectsrcPS1 -Copy.java Sunday, May 01, 2016 3:31 PM 229 } 230 231 catch (Exception e) 232 { 233 System.out.println("Please enter a double value"); 234 } 235 236 Fahrenheit = ((Celsius * 9) / 5) + 32; 237 System.out.println("That temperature in Fahrenheit is " + Fahrenheit 238 + ". Boom, I just blew you away, didn't I? Don't deny it." ); 239 240 System.out.println("Finally, let's have a quick lesson on driving a car " 241 + "with a manual transmission, in the case of how to " 242 + "drive off from a dead stop, when to shift during " 243 + "daily driving and how to come to a complete stop " 244 + "without stalling the car, such as in the event of " 245 + "a red traffic light."); 246 247 int choice = 0; 248 249 System.out.println("Please choose which event you would like to learn " 250 + "about by typing in a number, with 1 being the first " 251 + "event, 2 being the second event and 3 being the third " 252 + "event, respectively."); 253 try 254 { 255 choice = input.nextInt(); 256 } 257 258 catch (Exception e) 259 { 260 System.out.println("Please enter either 1, 2, or 3."); 261 } 262 263 String eventString = null; 264 switch (choice) { 265 case 1: 266 eventString = "Fully depress the clutch and put the car in " 267 + "first gear. Then slowly give it gas and slowly let off " 268 + "of the clutch."; 269 break; 270 case 2: 271 eventString = "When to the point of moving after driving off in, " 272 + "first gear, once you reach about 2500 to 3000 rpm, " 273 + "shift to the next gear and continue to do so for " 274 + "all gears."; 275 break; 276 case 3: 277 eventString = "To come to a complete stop, simply depress the " 278 + "clutch fully and shift the car into neutral, or no " 279 + "gear. Then apply the brake pedal as necessary."; 280 break; 281 } 282 283 System.out.println(eventString); 284 285 System.out.println("Excellent! Thank you for your time thusfar. This " -5-
  • 6.
    C:UsersOwnerworkspaceIntegration ProjectsrcPS1 -Copy.java Sunday, May 01, 2016 3:31 PM 286 + "next section will begin by demonstrating relational " 287 + "and conditional operators."); 288 289 // Relational operators are tools in Java that are used to determine if 290 // an operand is less than, greater than and equal or not equal to 291 // another 292 // operand. There are six relational operators: 293 // 1. Equal to: == 294 // 2. Not equal to: != 295 // 3. Greater than: > 296 // 4. Greater than or equal to: >= 297 // 5. Less than: < 298 // 6. Less than or equal to: <= 299 // Conditional operators perform Conditional-AND and Conditional-OR 300 // operations on two boolean expressions. Therefore, there are two 301 // conditional operators: 302 // 1. && Conditional-AND 303 // 2. || Conditional-OR 304 305 int num8 = 0; 306 int num9 = 0; 307 308 System.out.println("Alright, I'm going to read two numbers from 1 to 10 " 309 + "and determine if they are either greater than or less than " 310 + "one another. Please enter the first number."); 311 try 312 { 313 num8 = input.nextInt(); 314 315 System.out.println("Thank you! Now please enter the second number."); 316 num9 = input.nextInt(); 317 } 318 319 catch (Exception e) 320 { 321 System.out.println("Invalid input. Please try again."); 322 } 323 324 //For this loop I am using a while loop with relational operators to 325 //compare two inputted numbers with a value from 1 to 10. 326 //The code in parentheses of the loop below would be considered 327 //arguments. 328 while (num8 < num9) 329 { 330 System.out.println(num9 + " is greater than " + num8 + "."); 331 break; 332 } 333 while (num8 > num9) 334 { 335 System.out.println(num9 + " is less than " + num8 + "."); 336 break; 337 } 338 339 //The following is a demonstration for a class that finds the radius and area 340 //of a circle: 341 //public class Testing 342 //{ -6-
  • 7.
    C:UsersOwnerworkspaceIntegration ProjectsrcPS1 -Copy.java Sunday, May 01, 2016 3:31 PM 343 //private double radius; 344 //private double area; 345 346 //public Testing() 347 //{ 348 //radius = 0; 349 //area = 0; 350 //} 351 352 //public Testing(double radius) <--this method contains a double parameter. 353 //{ 354 //this.radius = radius; 355 //} 356 357 //public double getRadius() 358 //{ 359 //return radius; 360 //} 361 362 //public void setRadius(double radius) 363 //{ 364 //this.radius = radius; 365 //} 366 367 //public double getArea() 368 //{ 369 //return area; 370 //} 371 372 //public void setArea(double area) 373 //{ 374 //area = radius * radius * Math.PI; 375 //} 376 377 //public String toString() 378 //{ 379 //return "The radius of the circle is: " + radius + ", and the " 380 //+ "area is: " + area; 381 //} 382 383 System.out.println("Awesome! For this next component, I will read 5 " 384 + "numbers from the user and determine the largest and " 385 + "smallest values. Please enter five integers."); 386 387 int[] anArray = new int[5]; 388 int total = 0; 389 390 try 391 { 392 System.out.println("Integer 1:"); 393 anArray[0] = input.nextInt(); 394 total += anArray[0]; 395 396 System.out.println("Integer 2:"); 397 anArray[1] = input.nextInt(); 398 total += anArray[1]; 399 -7-
  • 8.
    C:UsersOwnerworkspaceIntegration ProjectsrcPS1 -Copy.java Sunday, May 01, 2016 3:31 PM 400 System.out.println("Integer 3:"); 401 anArray[2] = input.nextInt(); 402 total += anArray[2]; 403 404 System.out.println("Integer 4:"); 405 anArray[3] = input.nextInt(); 406 total+= anArray[3]; 407 408 System.out.println("Integer 5:"); 409 anArray[4] = input.nextInt(); 410 total += anArray[4]; 411 412 System.out.println("The value " + anArray[0] + " is found at " 413 + "index [0]."); 414 415 } catch (ArrayStoreException e) 416 { 417 System.out.println("Please enter an integer."); 418 } 419 420 System.out.println("Thank you! The sum of these values is " + total 421 + "."); 422 423 int smallest = anArray[0]; 424 int largest = anArray[0]; 425 426 //Here, I am using a nested "if" loop within a for loop in order to 427 //determine the largest and smallest values within the array. 428 429 for (int index = 1; index < anArray.length; index++) 430 { 431 if (anArray[index] > largest) 432 largest = anArray[index]; 433 else if (anArray[index] < smallest) 434 smallest = anArray[index]; 435 } 436 437 System.out.println("The smallest value is " + smallest + "."); 438 System.out.println("The largest value is " + largest + "."); 439 440 //Two dimensional arrays are like standard arrays except that, as expected, 441 //they have two dimensions instead of one. 442 443 int[][] demo = new int [2][2]; 444 demo [0][0] = 1; 445 demo [0][1] = 2; 446 demo [1][0] = 3; 447 demo [1][1] = 4; 448 449 System.out.println("For my 2d array demo, I will print the values from " 450 + "the first and second index places: "); 451 452 System.out.println("From first index: " + demo [0][0]); 453 System.out.println("From second index: " + demo [0][1]); 454 455 456 System.out.println("Now, to demonstrate string processing, I will " -8-
  • 9.
    C:UsersOwnerworkspaceIntegration ProjectsrcPS1 -Copy.java Sunday, May 01, 2016 3:31 PM 457 + "concatenate two strings and print the values of each one." ); 458 459 String demo2 = "Happy"; 460 String demo3 = "coding"; 461 462 System.out.println("When concatenated: " + demo2 + demo3); 463 464 System.out.println("The length of the first string is " + demo2.length() 465 + "."); 466 467 System.out.println("The length of the second string is " + demo3.length() 468 + "."); 469 470 //One can think of the fact that a variety of objects, such as in real life, 471 //have certain characteristics in common with one another. In essence, 472 //classes are able to obtain commonly used state and behavior from other 473 //classes. For example: 474 475 //public class App { 476 477 //public static void main(String[] args) { 478 479 //Machine mach1 = new Machine(); 480 //mach1.start(); 481 482 //Person person1 = new Person("Bob"); 483 //person1.greet(); 484 485 //Info info1 = new Machine(); 486 //info1.showInfo(); 487 488 //Info info2 = person1; 489 //info2.showInfo(); 490 491 //System.out.println(); 492 493 //outputInfo(mach1); 494 //outputInfo(person1); 495 //} 496 497 //private static void outputInfo(Info info) { 498 //info.showInfo(); 499 //} 500 501 //} 502 503 //As with polymorphism, the term is derived from its biology definition in 504 //which an organism or species can have various forms or stages. 505 //Ultimately, subclasses can define their own behaviors in addition to 506 //sharing similar functionality of the parent class. For example: 507 508 //public class App { 509 510 //public static void main(String[] args) { 511 512 513 //Plant plant1 = new Plant(); -9-
  • 10.
    C:UsersOwnerworkspaceIntegration ProjectsrcPS1 -Copy.java Sunday, May 01, 2016 3:31 PM 514 515 //Tree tree = new Tree(); 516 517 //Plant plant2 = tree; 518 519 //plant2.grow(); 520 521 //tree.shedLeaves(); 522 523 524 //doGrow(tree); 525 //} 526 527 //public static void doGrow(Plant plant) { 528 //plant.grow(); 529 //} 530 531 //} 532 533 System.out.println("This is the conclusion of my program, thank you for " 534 + "your cooperation."); 535 536 537 } 538 } 539 540 541 542 543 544 -10-