Practice Exam Answers -- March 13, 2016 Part A. Multiple Choice Questions 1. a 2. b 3. b 4. d 5. b 6. c 7. a 8. c 9. a 10. d 11. a 12. c Part B: Predict the Fill Order +---+ | 4 | +---+---+---+ | 1 | + | 2 | +---+---+---+ | 3 | +---+ 0 1 2 3 4 5 6 +---+---+---+---+---+---+---+ 0 | * | * | * | * | * | * | * | +---+---+---+---+---+---+---+ 1 | | | * |11 | * |10 | * | +---+---+---+---+---+---+---+ 2 | * | * | 6 | 7 | 8 | 9 | * | +---+---+---+---+---+---+---+ 3 | * | 3 | 2 | 1 |12 | * | * | +---+---+---+---+---+---+---+ 4 | * | * | 4 | 5 | * | | * | +---+---+---+---+---+---+---+ 5 | * | | * | * | | | * | +---+---+---+---+---+---+---+ 6 | * | * | * | * | * | * | * | +---+---+---+---+---+---+---+ Part C: Write Unit Tests //------- Source code file: DogTest.java public class DogTest { Dog _d; @Before public void setUp( ) { _d = new Dog("Duke", "Maxwell", 3, "German Shephard"); } @Test public void testGetBreed( ) { assertEquals(_d.getBreed( ), "German Shephard"); } @Test public void testSetAge( ) { _d.setAge(5); assertEquals(_d.getAge( ), 5); } @Test public void testToString( ) { assertEquals(_d.toString( ), "Duke Maxwell 3 German Shephard"); } } Part D: Find Errors //------- Source code file Main1.java. package it313.finalexam.petsalon; import java.sql.*; import java.util.HashMap; require java.util.Map.Entry; public class Main1 { public static void main(String[] args) throws ClassNotFoundException, SQLException { // Create HashSet that contains 4 Dog objects. HashMap col = new HashMap( ); col.put("D1", new Dog("Duke", "Maxwell", 3, "German Shephard")); col.put("D2", new Dog("Sparky", "Rogers", 1, "Dachshund"); col.put("D3", new Dog("Mimi", "Sanchez", 1, "Pomeranian")); col.put("D4", new Dog("Ginger", "Saari", 4, "Collie")); System.out.println(col); // Create Connection and Statement objects. Class.forName("org.sqlite.JDBC"); Connection c = DriverManager.getConnection("jdbc:sqlite:dogs.db"); Statement s = c.createStatement( ); // Create new dogs table, if it doesn't already exist. String sql1 = "create table if not exists dogs(" + "name varchar(10), " + "owner varchar(10), " + "age integer, " + "breed varchar(10));"; s.executeUpdate(sql1); //------- Source code file Main1.java (continued) // Populate table with dogs from collection. for(Entry e : col.entrySet( )) { Dog d = e.getKey( ); String sql2 = String.format( "insert into dogs values('%s', '%s', %d, '%s');", d.getName( ), d.getOwner( ), d.getAge( ), d.getBreed( )); } s.executeUpdate(sql2); } } Part D: Correct the errors // ------------ Source code file Main2.java import java.io.FileNotFoundException; public class Main2 { public static void main(String[ ] args) throws FileNotFoundException { TornadoSimulator[ ] simulators = { new TornadoSimulator("Cook"), new TornadoSimulator("DuPage"), new TornadoSimulator("Kane"), new TornadoSimulator("Lake"), new TornadoSimulator("McHenry"), new TornadoSimulator("Will") }; TornadoObserver observer = new TornadoObserver("logfile.txt"); for(TornadoSimulator sim : simulators) { sim.addObserver(observer); } for(int day = 1; day <= 1000; day++) { for(TornadoSimulator sim : simulators) { sim.checkForTornado( ); } } } } // ----- Source code file TornadoSimulator.java import java.util.Observable; import java.util.Random; public Class TornadoSimulator super Observable { private String _county; private Random _r; public TornadoSimulator(String theCounty) { _county = theCounty; _r = new Random( ); } public void checkForTornado( ) { if( _r.nextInt(1000) = 999) { notifyObservers(this, "County: " + _county); setChanged( ); } } } // ----- Source code file TornadoObserver.java import java.io.File; import java.io.FileNotFoundException; import java.io.PrintWriter; import java.util.Observable; import java.util.Observer; public class TornadoObserver implements Observer { PrintWriter _pw; public TornadoObserver(String the_file_name) throws FileNotFoundException { File f = new File(theFileName); PrintWriter _pw = new PrintWriter(f); } @Override public void update(Observable theSimulator, Object theMessage) { _pw.println(theMessage); pw.flush( ) } }