// CSE 142, Winter 2008, Marty Stepp // This file can be used to test your solution to a midterm question. import java.util.*; public class MidtermTester { public static void main(String[] args) { question1(); question2(); question3(); question4(); question5(); question6(); } public static void question1() { System.out.println("Question 1 (expressions):"); System.out.println(4 + 3 * 2 - 1); System.out.println(1 + ( 2 + " ( 3 + " + 4 + " 5 ) " ) + 6); System.out.println(100 % 30 % 4 + 5 % 2); System.out.println(!(1 + 1 >= 2) || 10 / 3 == 3); System.out.println(2.0 * 2.2 + -23 / 2 / 2.0); System.out.println(10 > 5 && (2 > 3 || 5 <= 4 + 2)); System.out.println(); } public static void question2() { System.out.println("Question 2 (parameter mystery):"); String knight = "rook"; String king = "pawn"; String bishop = "knight"; String pawn = "queen"; String rook = bishop; chess(knight, king, bishop); chess(bishop, knight, king); chess(king, pawn, "queen"); bishop = "king"; chess("bishop", rook, knight); chess(pawn, bishop, bishop); } public static void chess(String b, String c, String a) { System.out.println("A " + a + " and a " + b + " beats a " + c); } public static void question3() { System.out.println(); System.out.println("Question 3 (while loop mystery):"); System.out.println(whileMystery(2)); System.out.println(whileMystery(84)); System.out.println(whileMystery(4097)); System.out.println(whileMystery(388)); System.out.println(whileMystery(23980)); System.out.println(); } public static int whileMystery(int a) { int b = 1; int c = 0; while (b <= a) { if (b % 2 == 0) { c++; } b = b * 10; c++; } return c + 1; } // Can't really test question 4 this way, but here's the code anyway. public static void question4() { System.out.println("Question 4 (assertions):"); Random rand = new Random(); System.out.println(dice(rand)); System.out.println(dice(rand)); System.out.println(dice(rand)); System.out.println(); } // Counts number of "coin tosses" until we get heads 3 times in a row. public static int dice(Random rand) { int count = 0; int even = 0; // Point A while (count < 10 && even < 3) { // Point B count++; int roll = rand.nextInt(6) + 1; if (roll % 2 == 0) { // Point C even++; // Point D } } // Point E return count; } public static void question5() { System.out.println("Question 5 (canMakeChange):"); System.out.println("Test cases on the exam sheet:"); testCanMakeChange( 3, 4, 12); // true testCanMakeChange( 1, 5, 26); testCanMakeChange(24, 2, 31); testCanMakeChange(87, 19, 134); testCanMakeChange( 0, 0, 0); testCanMakeChange( 1, 1, 9); // false testCanMakeChange( 2, 7, 8); testCanMakeChange( 4, 3, 39); testCanMakeChange( 3, 80, 14); System.out.println("Additional test cases:"); testCanMakeChange( 3, 1, 8); // true testCanMakeChange( 3, 1, 9); // false testCanMakeChange( 3, 2, 10); // true testCanMakeChange( 3, 1, 9); // false testCanMakeChange( 7, 1, 11); // true testCanMakeChange( 7, 1, 8); // true testCanMakeChange( 7, 1, 13); // false testCanMakeChange( 2, 4, 11); // true testCanMakeChange( 2, 4, 13); // false testCanMakeChange( 0, 2, 10); // true testCanMakeChange( 1, 2, 7); // false testCanMakeChange(20, 4, 31); // true testCanMakeChange( 3, 9, 24); // false System.out.println(); } public static void testCanMakeChange(int pennies, int nickels, int change) { // teacher's solution boolean expected = canMakeChangeWorking(pennies, nickels, change); // your solution boolean actual = canMakeChange(pennies, nickels, change); System.out.printf("canMakeChange(%2d, %2d, %3d) = %s", pennies, nickels, change, actual); if (expected == actual) { System.out.println(" (pass)"); } else { System.out.println(" (FAIL!!)"); } } public static boolean canMakeChange(int pennies, int nickels, int cents) { // you can replace this code with your solution return true; } public static boolean canMakeChangeWorking(int pennies, int nickels, int cents) { if (cents % 5 > pennies) { return false; } else if (pennies + 5 * nickels < cents) { return false; } else { return true; } } public static void question6() { System.out.println("Question 6 (meetings):"); meetings(2, 17); meetings(10, 14); meetings(7, 3); } public static void meetings(int hour, int minute) { // you can write your solution here } }