CSC 212 -- Midterm Exam February 12, 2008 Part A: Short Answer Questions Do only 4 out of 5 questions. 1. (5 pts.) Convert the binary number 01101101 into hex and decimal. Ans: 0110 bin = 6 hex, 1101 bin = D hex, so the hex value is 6D. 01101101 = 64 + 32 + 8 + 4 + 1 = 109 decimal. 2. (5 pts.) Explain what short-circuit evaluation means. Ans: It means that when evaluating logical and ( && ) or logical or ( || ) expressions, if the answer is known after evaluating the first term, the second term is not evaluated. 3. (5 pts.) Explain how the compareTo method is used. Ans: compareTo takes an Object parameter representing the "other" object. If the current object (this) is greater than the other object, a positive value is returned. If the current object is less than the current object, a negative value is returned. If the objects are equal, 0 is returned. 4. (5 pts.) Explain what the keywords public, static and void mean in this method header: public static void main(String[ ] args) Ans: public means that the method can be executed by objects other then the current object. The main method must be public to be run at the command line. static means that the method is called by the class; no object is necessary to call it. The main method must always be static because it is the first method called; no object has yet been created. void means that the method does not return a value. 5. (5 pts.) What is the output? Justify your answer. Person p = new Person("George", 'M', 45); Person q = new Person("Chloe", 'F', 28); Person r = new Person("Sally", 'F', 19); q = r; r = p; System.out.println(p); System.out.println(q); System.out.println(r); Ans: After q = r, q refers to Sally. After r = p, r refers to George. The output is George M 45 Sally F 19 George M 45 Part B: Show Output or Variable Trace Do 2 out of 3 questions. 1. (8 pts.) Assume that the Stack class contains AR (AirlineReservation) objects, defined in Problem C1. What is the output of this main method? public static void main(String[ ] args) { Stack s = new Stack( ); s.push(new AR("Mouse, Mickey", "323/453-1111", "3C")); s.push(new AR("Mouse, Minnie", "323/453-2222", "5A")); s.push(new AR("Duck, Donald", "323/453-3333", "11B")); s.push(new AR("Bunny, Bugs", "323/453-4444", "23B")); s.pop( ); s.pop( ); System.out.println(s.getTop( ).getName( )); s.pop( ); s.push(new AR("Duck, Huey", "323/453-5555", "25A")); s.push(new AR("Duck, Louie", "323/453-6666", "19B")); s.push(new AR("Duck, Dewey", "323/453-7777", "18C")); System.out.println(s.getTop( ).getName( )); for(int i = 1; i <= 3; i++) s.pop( ); System.out.println(s.getTop( ).getName( )); System.out.println(s.isEmpty( )); } Ans: Only show the names in the output: Mouse, Minnie Duck, Dewey Mouse, Mickey false 2. (8 pts.) What is the output? int x = 0x7E, y = 0x5A, a, b, c, d; a = x | y; b = x & y; c = x ^ y; d = ~y; System.out.printf("%X; %X; %X; %X; ", a, b, c, d); Ans: 0x7E == 01111110 0x5A == 01011010 -------- x | y == 01111110 == 7E Output: 7E; 5A; 24; A5 x & y == 01011010 == 5A x ^ y == 00100100 == 24 ~y == 10100101 == A5 3. Given the array { 56, 33, 49, 75, 21 } a. (4 pts.) Show the variable trace for the InsertionSort algorithm. Variable Trace: a[0] a[1] a[2] a[3] a[4] temp ------+------+------+------+------+------ 56 33 49 75 21 33 33 56 49 75 21 49 33 49 56 75 21 75 33 49 56 75 21 21 21 33 49 56 75 b. (4 pts.) Show the variable trace for the SelectionSort algorithm. a[0] a[1] a[2] a[3] a[4] ------+------+------+------+------- 56 33 49 75 21 21 33 49 75 56 21 33 49 75 56 21 33 49 75 56 21 33 49 56 75 Part C: Class Design 1. Design, code and test an AR (AirlineReservation) class with instance variables name, phone and seat. Include a parameterized constructor, a getter for name and a setter for seatNumber. Also include a toString method. a. (8 pts.) Construct the UML diagram. The UML Diagram: +-----------------------------+ | AR | +-----------------------------+ | -name : String | | -phone : String | | -seat : String | +-----------------------------+ | +AR(String, String, String) | | +getName( ) : String | | +setSeat(String) | | +toString( ) : String | +-----------------------------+ b. (15 pts.) Write the code for the AR class. The code: public class AR { public String name; public String phone; public String seat; public AR(String n, String p, String s) { name = n; phone = p; seat = s; } public String getName( ) { return name; } public void setSeat(String s) { seat = s; } public String toString( ) { return String.format("%s %s %s", name, phone, seat); } } c. (12 pts.) Write the code for the TestAR class. This class should test all of the methods in the AR class. The code: public class TestAR { public static void main(String[ ] args) { AR x = new AR("Smith, Sherry", "888/888-8888", "23A"); System.out.println(x); System.out.println(x.getName( )); x.setSeat("23C"); System.out.println(x); } } d. (8 pts.) Predict the output of the TestAR class. The output: Smith, Sherry 888/888-8888 23A Smith, Sherry Smith, Sherry 888/888-8888 23C Part D: Write Code Work only 1 out of 3 problems: 1. (12 pts.) Given an array of AR objects named reservations, write a for loop that prints the name of the person in seat 23C. If no one is in that seat, print "Seat Available". If more than one person is assigned to seat 23C by mistake, print all of these names. See Problem C1 for the definition of the AR class. The code: boolean seatTaken = false; for(int i = 0; i < reservations.length - 1; i++) { if (reservations[i] != null && reservations[i].getSeat( ).equals("23C")) { seatTaken = true; System.out.println(reservations[i].getName( )); } } if (!seatTaken) System.out.println("Seat available."); 2. (12 pts.) Write a main method that accepts an arbitrary number of command line arguments that represent floating point numbers. Your main method should print out the sum of these numbers. The code: public static void main(String[ ] args) { double sum = 0.0; for(int i = 0; i < args.length; i++) sum += Double.parseDouble(args[i]); System.out.printf("The sum is %f.", sum); } 3. (12 pts.) Write a main method that accepts a command line argument that represents an input file. Your main method should print out the number of lines in this input file. public static void main(String[ ] args) throws FileNotFoundException { Scanner s = new Scanner(new FileReader(args[0])); while (s.getNextLine( )) { s.getLine( ); lineCount++; } System.out.printf("The number of lines is %d.", lineCount); }