CS 12300 Exam 2 Review The exam is on Thursday, April 1. The exam is over Chapters 4, 5 and 6 from Think Java, https://books.trinket.io/thinkjava2/ and the following sections from https://runestone.academy/runestone/books/published/csjava/index.html Sections 3.1, 3.2, 3.3, 3.4, 3.5, 3.6, 3.7. Sections 4.1, 4.2, 4.3, 4.4, 4.5. Sections 5.1, 5.2, 5.3. The topics on the exam are: 1) method definition a) method declaration i) method name ii) method parameters (each has a type and a name) iii) method return type b) method body i) local variables ii) scope of a variable iii) return statements 2) method call a) flow of control b) argument values (in method call) c) parameter variables (in method definition) d) pass argument values to parameter variables e) stack diagram f) return value 3) boolean expressions a) relational operators b) logic operators (&&, ||, !) c) operator precedence 4) if-else-statement 5) nested if-else-statements a) mutually exclusive cases 6) while-loop 7) for-loop 8) nested for-loops Here are a few review problems. Problem 1) What does the following code fragment print out? Briefly explain why. String s = "************"; int i = 0; while ( i < 11 ) { System.out.println( s.substring( i++ ) ); } while ( i > 0 ) { System.out.println( s.substring( i-- ) ); } Problem 2) Write a single (not nested) for-loop that uses substrings of these two strings, String s1 = "************"; String s2 = " "; to create the following 12 lines of output. ************ *********** ********** ********* ******** ******* ****** ***** **** *** ** * Problem 3) Let s be the following string of nine spaces. String s = " "; For each part below, write a single (not nested) while-loop that uses substrings of s to create the given picture. a) Create the following ten lines of output. * * * * * * * * * * b) Create the following ten lines of output. * * * * * * * * * * c) Create the following six lines of output. * * * * * * * * * * * Problem 4) What is printed out by the following code fragment? int n = 15; if ( 10 <= n && 20 <= n ) { System.out.println( "A" ); } else { System.out.println( "B" ); } if ( 10 < n || 20 < n ) { System.out.println( "C" ); } else { System.out.println( "D" ); } if ( n != 15 ) { System.out.println( "E" ); } else { System.out.println( "F" ); } if ( n == 15 ) { System.out.println( "G" ); } else { if ( n > 0 ) { System.out.println( "H" ); } } Problem 5) Write a code fragment, using nested for-loops, whose output in the console will look like the following. (Hint: Don't try to compute the output numbers from the loop indices. To get the output numbers, just have a counter variable that counts up from 10, and print out the counter.) 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 Problem 6) In the following program, complete the definition of the main method so that the program, when it is run, would produce the following output. * * * * * 12345-12345-12345-12345-12345-12345-12345-12345-12345-12345-12345-12345-12345-12345- * * * * * public class Problem15 { public static void methodA() { System.out.print("12345"); } public static void methodB() { System.out.println(" *"); System.out.println(" *"); System.out.println(" *"); System.out.println(" *"); System.out.println("*"); } public static void methodC() { System.out.println("*"); System.out.println(" *"); System.out.println(" *"); System.out.println(" *"); System.out.println(" *"); } public static void main(String[] args) { } } Problem 7) Write nested for-loops to produce the following output. Your for-loops only need to produce exactly these seven lines (your for-loops can use "magic numbers", there is no size parameter). 6 55 444 3333 22222 111111 0000000 Problem 8) Consider the following code fragment. if ( p < 0 ) System.out.println( "p is negative" ); if ( p == 0 ) System.out.println( "p is zero" ); if ( p > 0 ) System.out.println( "p is positive" ); Rewrite this code fragment using nested if-else-statements. Problem 9) Explain what the following program segment does. Then add a pair of braces so that what the resulting code prints out is always correct. if ( x == y ) if ( x == 0 ) System.out.println("x and y are both zero"); else System.out.println("x and y are not equal"); Problem 10) Fill in the blanks so that the following method definition is correct. public static ________ max2( _______ a, int ______ ) { if ( ______ >= ______ ) { return _______ ; } else { _______ b; } } Problem 11) Fill in the blanks so that the following method definition is correct. public static ________ min2( _______ a, double ______ ) { _______ result; if ( a - b < ______ ) { result = _______ ; } else { result = b; } return result; } Problem 12) Write a complete definition for a public static method called closeEnough that takes a double and an integer and returns true if the double is within one unit of the integer. For example closeEnough(3.9, 5) -> false closeEnough(4.1, 5) -> true closeEnough(6.0, 5) -> true closeEnough(6.1, 5) -> false Problem 13) Write a definition for a public static void method that has a parameter called size and prints a line with 1+size number of characters on the line and with the first character being an asterisk, the last character being an equal sign, and the rest of the characters being ampersands. Problem 14) The following method contains a single error. Describe the error and show how to fix it. private static boolean isMillionaire(int netWorth) { if (netWorth >= 1000000) return true; } Problem 15) Which of the following methods returns the minimum of its four inputs? Which is easiest to understand? public static int min(int a, int b, int c, int d) { if (a <= b && a <= c && a <= d) return a; if (b <= c && b <= d) return b; if (c <= d) return c; return d; } public static int min(int a, int b, int c, int d) { int min = a; if (b < min) min = b; if (c < min) min = c; if (d < min) min = d; return min; } public static int min(int a, int b, int c, int d) { if (a <= b) { if (a <= c) { if (a <= d) return a; else return d; } if (c <= d) return c; else return d; } if (b <= c) { if (b <= d) return b; else return d; } else if (c <= d) return c; return d; } public static int min(int a, int b, int c, int d) { return min2(min2(a, b), min2(c, d)); } public static int min2(int a, int b) { if (a <= b) return a; else return b; } Problem 16) What is printed out by the following program segment? for (int i = 0; i < 4; i++) { for (int j = 0; j < 5; j++) if ( (i+j) % 2 == 0 ) System.out.print("0 "); else System.out.print("1 "); System.out.print("\n"); } Problem 17) The body of the following method has three statements. Rewrite the body so that it has just a single statement which is the simplest statement that will work. (Hint: When does the method return true and when does the method return false?) private static boolean isInside(int n) { boolean inside = false; if (0 < n && n < 100) inside = true; return inside; } Problem 18) Simplify the code for the following method as much as you can. (Hint: What are the mutually exclusive cases?) public static int inTheInterval(int n) { if (n < 0) return -1; else if (0 <= n && n <= 1) return 0; else if (n > 1) return 1; return 42; // we never get here } Problem 19) Here are two code fragments. Below them are three pictures. These fragments draw two of the three pictures. Determine which fragment draws which picture. Explain your answer by detailing which part of the if-statements draw which part of the pictures. Be very specific about how you can tell which fragment draws which picture. (In each code fragment, assume that n is an odd integer.) // assume that n is odd // assume that n is odd for (int i = 1; i <= n; i++) for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) { for (int j = 1; j <= n; j++) { { if (i==1 || i==n || if (i==1 || i==n || j==1 || j==n || j==1 || j==n || (i==(n/2)+1 && j>=(n/2)+1) || (i==(n/2)+1 && j<=(n/2)+1) || (i==j && i<=(n/2)+1) ) (i==j && i>=(n/2)+1) ) System.out.print("*"); System.out.print("*"); else else System.out.print(" "); System.out.print(" "); } } System.out.println(); System.out.println(); } } *************** *************** *************** * * ** * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * ******** * * ******** * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * ** * * * ** *************** *************** ***************