// Miya Natsuhara // 07-24-2019 // CSE142 // TA: Grace Hopper // Contains some examples of some methods that you could be asked to write on the midterm. import java.util.*; public class MidtermPractice { public static void main(String[] args) { Random rand = new Random(); Scanner console = new Scanner(System.in); // seven System.out.println("SAMPLE CALLS TO seven()"); boolean sawSeven1 = seven(rand); System.out.println("first call to seven() returned " + sawSeven1); boolean sawSeven2 = seven(rand); System.out.println("second call to seven() returned " + sawSeven2); boolean sawSeven3 = seven(rand); System.out.println("third call to seven() returned " + sawSeven3); System.out.println(); // flip (named flip 001 in the old exam questions database) System.out.println("SAMPLE CALLS TO flip()"); int flips1 = flip(rand); System.out.println("first call to flip() returned " + flips1); int flips2 = flip(rand); System.out.println("second call to flip() returned " + flips2); int flips3 = flip(rand); System.out.println("third call to flip() returned " + flips3); System.out.println(); // digitRange System.out.println("SAMPLE CALLS TO digitRange()"); testMessage(0, digitRange(0)); testMessage(0, digitRange(5)); testMessage(4, digitRange(26)); testMessage(2, digitRange(42)); testMessage(5, digitRange(725)); testMessage(0, digitRange(888)); testMessage(3, digitRange(1234)); testMessage(8, digitRange(24680)); testMessage(7, digitRange(857492)); testMessage(6, digitRange(3876254)); System.out.println(); // longestName /* the following call should produce this output: name #1? roy name #2? DANE name #3? Erik name #4? sTeFaNiE name #5? LaurA Stefanie's name is longest */ longestName(console, 5); /* the following call should produce this output: name #1? TrEnt name #2? rita name #3? JORDAN name #4? craig name #5? leslie name #6? YUKI name #7? TaNnEr Jordan's name is longest (There was a tie!) */ longestName(console, 7); } // Prints a short message saying "Correct!" if the expected value matches the actual, or a // message saying the two values don't match if they don't. // int expected: expected value to compare against // int actual: actual, produced value to compare against public static void testMessage(int expected, int actual) { if (expected == actual) { System.out.println("Correct!"); } else { System.out.println("method call returned " + actual + ", but " + expected + " was expected"); } } /* THIS QUESTION IS NOT IN THE OLD EXAM QUESTIONS DATABASE So, the problem statement is repeated here for completeness: Write a static method called seven that takes a Random object as a parameter and that uses the random object to generate up to 10 numbers between 1 and 30 inclusive, printing them and stopping if the "lucky number" 7 comes up. It should return whether or not a 7 was generated. For example, if we construct a Random object and make the following calls: Random r = new Random(); boolean sawSeven1 = seven(r); System.out.println(); boolean sawSeven2 = seven(r); We would expect to get a log of execution like this: 10 3 14 1 8 7 30 11 6 27 2 18 25 22 9 10 and sawSeven1 would contain true after the first call, and sawSeven2 would contain false after the second call. You must exacty reproduce the format of the log above. */ public static boolean seven(Random r) { for (int i = 1; i <= 10; i++) { int next = r.nextInt(30) + 1; System.out.print(next + " "); if (next == 7) { // here we take advantage of the fact that hitting a return statement means that we // exit the method immediately return true; } } // if we've generated all 10 numbers and haven't returned from the method yet, it means we // did not see a seven, so we should return false. return false; } // See the problem statement by searching in the old exam questions database (there it is named // flip 001). public static int flip(Random r) { int headsInRow = 0; int flips = 0; while (headsInRow < 3) { int flip = r.nextInt(2); // 0 or 1 if (flip == 0) { // heads System.out.println("heads"); headsInRow++; } else { // tails // We must reset headsInRow to 0 if we see a tails, because then subsequent heads that // we see won't be "in a row" with previous heads...basically our "heads in a row" // count has to start over, once a flip of 'tails' ruins it. // Without this line, we'd just be counting up how many heads are flipped in total, // without accounting for the "in a row" part of the problem. System.out.println("tails"); headsInRow = 0; } flips++; } System.out.println("3 heads in a row after " + flips + " flips"); return flips; } // See the problem statement by searching in the old exam questions database (name digitRange) public static int digitRange(int n) { // initialize min/max variables to one of the digits of n, then chop off digit because we've // already processed it. int min = n % 10; int max = n % 10; n = n / 10; while (n > 0) { // runs while there are still digits left to process int digit = n % 10; // get rightmost digit of n n = n / 10; // chop off rightmost digit of n // update max if (digit > max) { max = digit; } // update min if (digit < min) { min = digit; } } return (max - min); } // See the problem statement by searching in the old exam questions database (name longestName) public static void longestName(Scanner console, int numNames) { String longest = ""; boolean tie = false; for (int i = 1; i <= numNames; i++) { System.out.print("name #" + i + "? "); String next = console.next(); // update longest name if (next.length() > longest.length()) { longest = next; // if we have a new longest name, any preexisting tie doesn't apply anymore because // those tied names are no longer longest! tie = false; } else if (next.length() == longest.length()) { // if we reach this branch and pass this test, it means we didn't find a new longest // name // if we have another name whose length is the same as our longest name so far, we have // a tie! tie = true; } } // fix casing for reporting the longest name to the console // NOTE: could also use substring(0, 1) to get the first letter of the name instead of // charAt(0) longest = longest.toUpperCase().charAt(0) + longest.toLowerCase().substring(1); System.out.println(longest + "'s name is longest"); if (tie) { System.out.println("(There was a tie!)"); } } }