/* CSE 142, Autumn 2007, 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(3 + 4 * 5 / 2); System.out.println(13 % 5 + 43 % (11 % 3)); System.out.println(1.5 * 3.0 + 25.0 / 10.0); System.out.println(5 / 2 + 123 / 10 / 10.0); System.out.println(5 + 2 + "(1 + 1)" + 4 + 2 * 3); System.out.println(); } public static void question2() { System.out.println("Question 2 (parameters):"); String p = "cause"; String q = "support"; String r = "troops"; String support = "hillary"; String cause = "rudy"; troops(p, q, r); troops(q, r, p); troops(support, p, cause); troops(r, "p", support); troops(q, "cause", q); } public static void troops(String r, String p, String q) { System.out.println(q + " gave " + r + " to the " + p); } public static void question3() { System.out.println(); System.out.println("Question 3 (while loop mystery):"); System.out.println(mystery(2)); System.out.println(mystery(-1)); System.out.println(mystery(7)); System.out.println(mystery(18)); System.out.println(mystery(43)); System.out.println(); } public static int mystery(int x) { int a = 1; int c = 0; while (x > 0) { a = x % 2; if (a == 1) { c++; } x = x / 2; } return c; } // Can't really test question 4 this way, but here's the code anyway. public static void question4() { System.out.println("Question 4 (assertions):"); threeHeads(); System.out.println(); } // Counts number of "coin tosses" until we get heads 3 times in a row. public static int threeHeads() { Random rand = new Random(); int flip = 1; int heads = 0; int count = 0; // Point A while (heads < 3) { // Point B flip = rand.nextInt(2); // flip coin if (flip == 0) { // heads heads++; // Point C } else { // tails // Point D heads = 0; } count++; } // Point E return count; } public static void question5() { System.out.println("Question 5 (monthApart):"); System.out.println("Test cases on the exam sheet:"); testMonthApart( 6, 4, 9, 21); testMonthApart( 4, 5, 5, 15); testMonthApart( 4, 15, 5, 15); testMonthApart( 4, 16, 5, 15); testMonthApart( 6, 14, 6, 8); testMonthApart( 7, 7, 6, 8); testMonthApart( 7, 8, 6, 8); testMonthApart(10, 14, 7, 15); System.out.println("Test cases we used in grading:"); System.out.println("Test A (dates are far apart):"); testMonthApart( 6, 21, 9, 14); testMonthApart( 8, 7, 6, 30); System.out.println("Tests B and C (1 month apart, days far enough apart):"); testMonthApart( 4, 15, 5, 16); testMonthApart( 5, 15, 4, 14); System.out.println("Tests D and E (1 month apart, days too close):"); testMonthApart( 5, 9, 6, 8); testMonthApart( 7, 7, 6, 8); System.out.println("Test F (same month):"); testMonthApart(10, 1, 10, 31); System.out.println("Boundary cases (<= vs. <):"); testMonthApart( 4, 15, 5, 15); testMonthApart( 5, 15, 4, 15); System.out.println("Other test cases:"); testMonthApart( 8, 19, 9, 21); testMonthApart( 8, 21, 9, 21); testMonthApart( 8, 22, 9, 21); testMonthApart( 9, 11, 9, 21); testMonthApart( 9, 29, 9, 21); testMonthApart(10, 11, 9, 21); testMonthApart(10, 20, 9, 21); testMonthApart(10, 21, 9, 21); testMonthApart(10, 25, 9, 21); testMonthApart(11, 1, 9, 21); System.out.println(); } public static void testMonthApart(int m1, int d1, int m2, int d2) { System.out.println("monthApart(" + m1 + ", " + d1 + ", " + m2 + ", " + d2 + ") = " + monthApart(m1, d1, m2, d2)); } public static boolean monthApart(int m1, int d1, int m2, int d2) { // you can replace this code with your solution return (m1 < m2 - 1) || (m1 > m2 + 1) || (m1 == m2 - 1 && d1 <= d2) || (m1 == m2 + 1 && d1 >= d2); } public static void question6() { System.out.println("Question 6 (sequenceSum):"); sequenceSum(0.0); sequenceSum(1.0); sequenceSum(1.5); sequenceSum(2.7); } public static void sequenceSum(double limit) { // you can replace this code with your solution if (limit >= 1) { System.out.print("1"); int denomenator = 1; double sum = 1.0; while (sum < limit) { denomenator++; sum += 1.0 / denomenator; System.out.print(" + 1/" + denomenator); } System.out.println(" = " + sum); } } }