Homework 4 ========== Problem 1: String s1 = "CSCI 161: Introduction to Programming 1"; s1 ==> "CSCI 161: Introduction to Programming 1" String s2 = s1.substring (10, 20); s2 ==> "Introducti" String s3 = s2.toUpperCase(); s3 ==> "INTRODUCTI" int indexOfO1 = s2.indexOf("o"); indexOfO1 ==> 4 int indexOfO2 = s3.indexOf("o"); indexOfO2 ==> -1 int indexOfT = s2.indexOf("t"); indexOfT ==> 2 String small = s3.substring(indexOfO1, indexOfT); | java.lang.StringIndexOutOfBoundsException thrown: begin 4, end 2, length 10 | at String.checkBoundsBeginEnd (String.java:3116) | at String.substring (String.java:1885) | at (#7:1) String large = s1.toUpperCase().substring(20); large ==> "ON TO PROGRAMMING 1" Problem 2: if (grade >= 60) { System.out.println("Pass"); } else { System.out.println("Fail"); } Problem 3: Scanner input = new Scanner(System.in); double number = input.nextDouble(); double answer = -1; if (number > 0) { answer = Math.sqrt(number); } Problem 4: if (gpa >= 3.95) { System.out.println ("Magna Cum Laude"); } else if (gpa >= 3.7) { System.out.println ("Summa Cum Laude"); } else if (gpa >= 3.5) { System.out.println ("Cum Laude"); } Problem 5: Scanner input = new Scanner(System.in); int a = input.nextInt(); int b = input.nextInt(); double average = ((double)a + (double)b) / 2.0; System.out.println(average); Homework 5 ========== Problem 1: z is odd: z % 2 == 1 OR z % 2 != 0 y is positive: y > 0 Either x or y is even: x % 2 != y % 2 y is a multiple of z: y % z == 0 x and z are of opposite signs: x * z < 0 OR x > 0 && z < 0 || x < 0 && z > 0 Problem 2: ifElseMystery1(3, 20); z = z + 9; // because z (4) <= x (3) y++; // because z (13) <= y (20) Output: "13 21" ifElseMystery1(4, 5); z = z + 1; // because z (4) <= x (4) y++; // because z (5) <= y (5) Output: "5 6" ifElseMystery1(5, 5); z = z + 1; // because z (4) <= x (5) Output: "6 5" ifElseMystery1(6, 10); z = z + 1; // because z (4) <= x (5) y++; // because z (5) <= y (10) Output: "7 11" Problem 3: public static int daysInMonth (int month) { if (month == 2) { return 28; } else if (month == 4 || month == 6 || month == 9 || month == 11) { return 30; } else { return 31; } } Homework 7 ========== Problem 1: A). x starts off at one; loop increments x by 10; loop condition is x < 100 1, 11, 21, 31, 41, 51, 61, 71, 81, 91, [101] 10 times B). max starts off at 10; loop condition is max < 10; loop never executes 0 times C). x starts off at 250; loop condition is x % 3 != 0; loop doesn't change x Since x never changes, and 250 % 3 is always != 0, the loop runs forever D). x starts off at 2; loop condition is x < 200; we multiply x by itself each iteration 2, 4, 16, [256] 3 times E). words is initially "a"; loop condition is length of word < 10; surround word by "b"s "a", "bab", "bbabb", "bbbabbb", "bbbbabbbb", ["bbbbbabbbbb"] 5 times F). x starts off at 100; loop condition is x > 0; we divide x by 2 each iteration 100, 50, 25, 12, 6, 3, 1, [0] 7 times Problem 2: x is our parameter; x = 1, z = 0; For each iteration we increment z by one and multiply y by 2 we will evolve each iteration until 2 * y <= x Finally, we print "y z" mystery(1) -> 2 * y <= x | x = 1, y = 1, z = 0 Output: "1 0" mystery(6) -> 2 * y <= x | x = 6, y = 1, z = 0 -> 2 * y <= x | x = 6, y = 2, z = 1 -> 2 * y <= x | x = 6, y = 4, z = 2 Output: "4 2" mystery(19) -> 2 * y <= x | x = 6, y = 1, z = 0 -> 2 * y <= x | x = 6, y = 2, z = 1 -> 2 * y <= x | x = 6, y = 4, z = 2 -> 2 * y <= x | x = 6, y = 8, z = 3 -> 2 * y <= x | x = 6, y = 16, z = 4 Output: "16 4" mystery(19) -> 2 * y <= x | x = 6, y = 1, z = 0 -> 2 * y <= x | x = 6, y = 2, z = 1 -> 2 * y <= x | x = 6, y = 4, z = 2 -> 2 * y <= x | x = 6, y = 8, z = 3 -> 2 * y <= x | x = 6, y = 16, z = 4 -> 2 * y <= x | x = 6, y = 32, z = 5 Output: "32 5" mystery(19) -> 2 * y <= x | x = 6, y = 1, z = 0 -> 2 * y <= x | x = 6, y = 2, z = 1 -> 2 * y <= x | x = 6, y = 4, z = 2 -> 2 * y <= x | x = 6, y = 8, z = 3 -> 2 * y <= x | x = 6, y = 16, z = 4 -> 2 * y <= x | x = 6, y = 32, z = 5 -> 2 * y <= x | x = 6, y = 64, z = 6 Output: "64 6" Problem 2: int x = 27; int y = -1; int z = 32; boolean b = false; a). !b true b). b || true true c). (x > y) && (y > z) (27 > -1) is false; therefore false d). (x == y) || (x <= z) (27 == -1) || (27 <= 32); therefore true e). !(x % 2 == 0) !(27 % 2 == 0); therefore true f). (x % 2 != 0) && b False because b must be true but is false g). b && !b False (tautology) h). b || !b True (tautology) i). (x < y) == b (27 < -1) is false; therefore, true j). !(x / 2 == 13) || b || (z * 3 == 96) z * 3 is 96; therefore true k). (z < x) == false z is greater than x; therefore true l). !((x > 0) && (y < 0)) DeMorgan's: x < 0 || y > 0 x is greater than 0 and y is less than 0 False