Final Exam 2014 Answers Part A. Short Answers 1. The wrapper class for char is Character. A wrapper class is used to create an object that contains the value of a primative type so that it can be added to an array list or hash set. 2. public static void main(String[ ] args) { System.out.println(args[0].length); } 3. return String.valueOf(_x); or return _x_ + ""; 4. @Override means that the method is overriding a method in the base class. If the overriding method's name is misspelled, an error will be generated. 5. try { Scanner in = new Scanner(new File("numbers.txt")) double sum = 0.0; int count = 0; while(in.hasNextDouble( )) { double value = in.nextDouble( ); sum += value; count++; } System.out.println("ave = " + sum / count); } catch(FileNotFoundException e) { System.out.println("File not found."); } 6. Find the errors: Scanner fromFile = new Scanner(new File("motor-boats.csv")); fromFile.nextLine( ); while (fromFile.hasNextLine( )) { line = fromFile.nextLine( ); fields = line.split(","); String vin = fields[0].trim( ); String gender = fields[1].trim( ); int length = Integer.parseInt(fields[2].trim( )); int hp = Integer.parseInt(fields[3].trim( )); String manufacturer = fields[4].trim( ); String model = fields[4].trim( ); sql = String.format( "insert into (vin, gender, length, hp, manufacturer, model)"+ "values ('%s', '%s', %d, %d, '%s', '%s');", vin, gender, length, hp, manufacturer, model); System.out.println(sql); s.executeUpdate(sql2); } Here is the corrected version: Scanner fromFile = new Scanner(new File("motor-boats.csv")); fromFile.nextLine( ); while (fromFile.hasNextLine( )) { line = fromFile.nextLine( ); fields = line.split(","); String vin = fields[0].trim( ); int length = Integer.parseInt(fields[1].trim( )); int hp = Integer.parseInt(fields[2].trim( )); double price = Double.parseDouble(fields[3].trim( )); String manufacturer = fields[4].trim( ); String model = fields[5].trim( ); sql = String.format( "insert into (vin, length, hp, price, manufacturer, model)"+ "values ('%s', %d, %d, %f, '%s', '%s');", vin, gender, length, hp, manufacturer, model); System.out.println(sql); // Code is not given to create Statement object. s.executeUpdate(sql2); } 7. Draw the recursion tree and predict the output: f(4) 5 / 3 \ 4 / \ Output: 5 4 4 3 3 4 1 2 3 / \ f(1) f(3) 4 / 3 \ 2 / \ / \ f(0) f(2) 3 / 2 \ 1 / \ / \ f(-1) f(1) 8. Here is the Person class with changes indicated: // Person class for Problems A9 and A10. public class Person { #<-- add "implements Comparable" after Person. private String _name; private char _gender; private int _age; // <-- add compareTo method starting here public int compareTo(Person other) { if (_age > other._age) { return 1; } else if (_age < other._age) { return -1; } else { return 0; } } // <-- end compareTo method here public Person(String theName, char theGender, int theAge) { _name = theName; _gender = theGender; _age = theAge; } public String getName( ) { return _name; } public char getGender( ) { return _gender; } public int getAge( ) { return _age; } public void setAge(int theAge) { _age = theAge; } public void haveBirthday( ) { _age++; } @Override public String toString( ) { return _name + " " + _gender + " " + _age; } } 9. int id = 3425; map.put(id, new Person("Alice", 'F', 25)); 10. The fill order diagram is +---+ | 4 | +---+---+---+ | 1 | * | 3 | In the following diagram, +---+---+---+ * is he starting point | 2 | X marks a location that +---+ originally contained 1. 0 1 2 3 4 5 6 7 8 9 +---+---+---+---+---+---+---+---+---+---+ 0 | X | X | X | X | X | X | X | X | X | X | +---+---+---+---+---+---+---+---+---+---+ 1 | X | | X |10 | X |16 | X | X | X | X | +---+---+---+---+---+---+---+---+---+---+ 2 | X | | X | 9 | 8 | 7 |11 |15 | X | X | +---+---+---+---+---+---+---+---+---+---+ 3 | X | X | 2 | 1 | * | 6 |12 |14 | X | X | +---+---+---+---+---+---+---+---+---+---+ 4 | X | | X | 3 | 4 | 5 |13 | X | X | X | +---+---+---+---+---+---+---+---+---+---+ 5 | X | | | X |18 |17 | X | X | X | X | +---+---+---+---+---+---+---+---+---+---+ 6 | X | | | X |19 |20 |21 | X | | X | +---+---+---+---+---+---+---+---+---+---+ 7 | X | | | X | X | X | X | X | | X | +---+---+---+---+---+---+---+---+---+---+ 8 | X | | | | | | | | | X | +---+---+---+---+---+---+---+---+---+---+ 9 | X | X | X | X | X | X | X | X | X | X | +---+---+---+---+---+---+---+---+---+---+ 11. See the notes from 3/11/15. Part B: Test methods from the Java Currency class in the Java class library. import java.util.Currency; import java.util.Set; public class Main { public static void main(String[] args) { Currency c = Currency.getInstance("USD"); System.out.println(c.getDisplayName()); System.out.println(c.getNumericCode()); System.out.println(c.getSymbol()); System.out.println(c.getDefaultFractionDigits()); Set currencies = Currency.getAvailableCurrencies(); for(Currency curr: currencies) { System.out.println(curr.getDisplayName()); } } } Part C: Write methods: // Write method setPrice: public void setPrice(double thePrice) { price = thePrice; } // Write method toString: public String toString( ) { return String.format("%s %s %s", super.toString( ), _manufacturer, _model); } // Write remove method of BasicInventory public void remove(String theVin) { for(int i = 0; i < _col.size( ); i++) { if(_col.get(i).getVin.equals(theVin)) { _col.remove(i); } } } // Write getSize method of BasicInventory. public int getSize( ) { return _col.size( ); } // Write test methods for the di object of DerivedInventory. di.add(a); di.add(b); di.add(c); di.add(d); System.out.println(di.display("WQ3293")); System.out.println(di.getSize( )); System.out.println(di);