COP 2800 Java Programming I Homework #2 Model Solution 1) Can the following three conversions involving casting be allowed? If so, find the converted result. boolean b = true; int i = (int) b; int i = 1; boolean b = (boolean) i; int i = 1; String s = (String) i; None of those conversions are allowed. A short and simple way to convert these would be: int i = b ? 1 : 0; boolean b = (i != 0 ); String s = Integer.toString(i); 2) What is the printout of the code in (a) and (b): if number is 30? If number is 35? a) if (number % 2 == 0) System.out.println(number + " is even."); System.out.println(number + " is odd."); OUTPUT for number == 30: 30 is even. 30 is odd. OUTPUT for number == 35: 35 is odd. b) if (number % 2 == 0) System.out.println(number + " is even."); else System.out.println(number + " is odd."); OUTPUT for number == 30: 30 is even. OUTPUT for number == 35: 35 is odd. 3) How do you generate a random integer i such that 0 <= i < 20? int i = (int)( Math.random() * 20 ); OR: Random rnd = new Random(); int i = rnd.nextInt(20); How do you generate a random integer i such that 10 <= i < 20? int i = 10 + (int)( Math.random() * ( 20 - 10) ); OR: Random rnd = new Random(); int i = 10 + rnd.nextInt(10); How do you generate a random integer i such that 10 <= i <= 50? int i = 10 + (int)( Math.random() * ( (50 + 1) - 10) ); OR: Random rnd = new Random(); int i = 10 + rnd.nextInt(41); Notice the pattern: Math.random() * range + lowestValue. And add one to the range if using "<=" instead of "<"; that is, an inclusive range. If using class Random, then the pattern is rand.nextInt( range ) + lowestValue. As before, add one to range if an inclusive range is desired. 4) What is the value of the expression (assume ch is a char): ch >= 'A' && ch <= 'Z' a. if ch is 'A'? true b. if ch is 'p'? false c. if ch is 'E'? true d. if ch is '5'? false The expression is only true for uppercase letters. (So only a. and c. are true.) 5) Write a switch statement that assigns a String variable dayName with Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, if day is 0, 1, 2, 3, 4, 5, 6, accordingly. switch ( day ) { case 0: dayName = "Sunday"; break; case 1: dayName = "Monday"; break; case 2: dayName = "Tuesday"; break; case 3: dayName = "Wednesday"; break; case 4: dayName = "Thursday"; break; case 5: dayName = "Friday"; break; case 6: dayName = "Saturday"; break; } 6) If a variable is declared in the for loop control, can it be used after the loop exits? No 7) The for loop on the left is converted into the while loop on the right. What is wrong? Correct it. int sum = 0; int i = 1, sum = 0; for (int i = 1; i <= 4; i++) { while (i <= 4) { if (i % 3 == 0) continue; if (i % 3 == 0) continue; sum += i; sum += i; } i++; } The continue statement causes the increment of i to be skipped. Here's one way to do this correctly (sum should be 7 at the end): int i = 1, sum = 0; while ( i <= 4 ) { if( i % 3 != 0 ) sum += i; i++; } 8) Do the following two loops result in the same value in sum? a) int sum = 0; for ( int i = 0; i < 10; ++i ) { sum += i; } b) int sum = 0; for ( int i = 0; i < 10; i++ ) { sum += i; } Yes, the loops result in the same value in sum, 45. (Since the increment statement is run without using the value of the result, the only effect is the increment of "i". Both pre and post ++ in this case do the same thing.) 9a) After the break statement is executed in the following loop, which statement is executed? Show the output. 1. for (int i = 1; i < 4; i++) { 2. for (int j = 1; j < 4; j++) { 3. if (i * j > 2) 4. break; 5. System.out.println(i * j); 6. } 7. System.out.println(i); 8. } Line 7 ("System.out.println(i);") is executed after the break statement. Here's the output: 1 2 1 2 2 3 9b) After the continue statement is executed in the following loop, which statement is executed? Show the output. 1. for (int i = 1; i < 4; i++) { 2. for (int j = 1; j < 4; j++) { 3. if (i * j > 2) 4. continue; 5. System.out.println(i * j); 6. } 7. System.out.println(i); 8. } Line 2 ("for (int j = 1; j < 4; j++) {") is executed after the continue statement. (Note, the if-statement on line 3 means the remaining loop cycles do nothing; eventually the loop exits and line 7 executes.) Here's the output: 1 2 1 2 2 3 10) What is wrong in the following statements? (a) System.out.printf("%5d %d", 1, 2, 3); Although this compiles and runs without error, and shows the output " 1 2", there is a logic error evident. This is wrong because there are only two of the % symbols (conversion specifiers) and three numbers. (b) System.out.printf("%5d %f" , 1); In this case, there are two % symbols and only one number. There would need to be 2 numbers for this to work. (c) System.out.printf("%5d %f" , 1, 2); This one will not work because the second number is corresponding to the %f, which specifies a floating point conversion. In this case the 2 would need to be a 2.0 for this to work. 11) Show the output of the following statements: (a) System.out.printf("amount is %f %e\n" , 32.32, 32.32); amount is 32.320000 3.232000e+01 (b) System.out.printf("amount is %5.4f %5.4e\n", 32.32, 32.32); amount is 32.3200 3.2320e+01 (c) System.out.printf("%6b\n", (1>2)); false (one leading space) (d) System.out.printf("%6s\n", "Java"); Java (two leading spaces) (e) System.out.printf("%-6b%s\n", (1>2), "Java"); false Java (f) System.out.printf("%6b%-s\n", (1>2), "Java"); Exception in thread "main" java.util.MissingFormatWidthException: -s (g) System.out.printf("%6b%-8s\n", (1>2), "Java"); falseJava (one leading and 4 trailing spaces) (a) amount is 32.320000 3.232000e+01 (b) amount is 32.3200 3.2320e+01 (c) false (one leading space) (d) Java (two leading spaces) (e) false Java (f) Exception in thread "main" java.util.MissingFormatWidthException: -s (g) falseJava (one leading and four trailing spaces) 12) Write a Java regular expression that matches en_US formatted whole numbers. Such numbers are composed of only digits and commas. For example: Input Result ,0 False , False 1,20 False 1,234, False 0 True 12 True 123 True 1,234 True 0,000 True Such numbers have 1-3 leading digits, followed by zero or more groups of comma-digit-digit-digit. The regular expression should use anchors (or at least word boundaries "\b") to make sure there is no leading or trailing extra input: ^\d{1,3}(,\d{3})*$ Explanation: ^ = anchor to beginning of the string \d{1,3} = 1, 2, or 3 digits (,\d{3})* = zero or more groups of "comma digit digit digit" $ = anchor to the end of the string (Remember to double-up the backslashes when using this in a Java program.) end