Midterm Exam Answers -- Winter 15 Part A. 1. a. Use a "parse" method to convert a string to a numeric value. 2. d. String.valueOf converts a numeric value to a string. You can also use n + "" because whenever concatenation involves a string, the result is a string. 3. a. 4. c. This is the definition of an instance variable. 5. a. r.nextDouble( ) is always a number between 0.0 and 1.0. Math.min(m, r.nextDouble( )) is always 0.0 because m starts out as 0.0 and is always smaller than the random number. 6. d. The indices for the standard IO streams are 0 for stdin, 1 for stdout, and 2 for stderr. 7. c or d. the statement col.add = 5; should have been col.add(5); 8. b. 9. d. 10. d. Part B. 1. Since a.substring(i, i+1) returns the ith letter of a as a string, int h = Integer.parseInt(items.substring(i, i+1)); and int k = Integer.parseInt(letters.substring(i, i+1)); pick out the ith digit of items and letters, respectively, converted to a digit. h k cities[h].charAt(k) output =====+=====+=====================+================ 1 | 3 | 'v' | "v" 0 | 4 | 'a' | "va" 2 | 5 | 'l' | "val" 1 | 1 | 'e' | "vale" 1 | 2 | 'n' | "valen" 2 | 3 | 't' | "valent" 0 | 2 | 'i' | "valenti" 1 | 2 | 'n' | "valentin" 2 | 6 | 'e' | "valentine" Part C: 1. public static int findMaxIndex(int[ ] arr) { int maxVal = 0; maxIndex = 0; for(int i = 0; i < arr.length; i++) { if (arr[i] > maxVal) { maxVal = arr[i]; maxIndex = i; } } } public static void main(String[ ] args) { int[ ] a = {5, 3, 1, 5, 9, 4, 2, 1} System.out.println(findMaxIndex(a); } // Output: 3 2. public static String getMonthAbbrev(int month) { String[ ] abbrevs = {"JAN", "FEB", "MAR", "APR", "MAY", "JUN", "JUL", "AUG", "SEP", "OCT", "NOV", "DEC"}; if (0 <= month && month <= 11) { return abbrevs[month]; } else { return "Illegal month code."; } } public static void main(String[ ] args) { System.out.println(getMonthAbbrev(8)); System.out.println(getMonthAbbrev(13)); } // Output: SEP Illegal month code. Part D: See class notes. Part E: Find the errors. Corrections are marked with <-- ==== Source code file Vehicle.java ========================== // There are about 5 errors in this Vehicle class. Correct them. private class Vehicle { <-- Change private to public. private String vin, color, driver; <-- Delete , driver and add following line private Person driver; <-- Add line public Vehicle(String theVin, String theColor, Person theDriver) { vin = theVin; color = theColor; driver = theDriver; } Override <-- Add @ before Override public String toString( ) { return String.format("VIN: %s Color: %s Driver: %d", <-- change %s to %d. vin, color, driver.getName); <-- Add ( ) after getName. } <-- Insert { ==== Source code file Taxi.java ============================= // There are about 5 errors in this Vehicle class. Correct them. public class Taxi < Vehicle { <-- Change < to extends. private String pickupLocation; private String dropoffLocation; private Person passenger; public void Taxi(String theVin, String theColor, <-- delete void Person theDriver) { super(theVin, theColor, theDriver); pickupLocation = ""; dropoffLocation = ""; passenger = null; } public void pickup(Person thePassenger, String theDropoffLocation) Person passenger = the_Passenger; <-- delete Person dropoffLocation = theDropoffLocation; } @Override public String toString( ) { String passengerName = ""; Person p = getPassenger( ); if p != null { <-- Add ( ) around p != null. passengerName = p.getName( ); } else { passengerName = "No passenger"; } return super.toString( ) + "\n" + String.format("Pickup Location: %s\n" + "Passenger Name: %s\n", "Dropoff Location: %s", pickupLocation, passengerName, dropoffLocation); } <-- Delete } } ==== Source code file Main.java ============================= // There are about 5 errors in this main class. Correct them. public class Main { public static String main(String args) <-- Change 1st String to void, insert [ ] after 2nd string Taxi t1 = new Taxi("abc34567", "yellow", Person.new("Bruno", 'M', 45)); <-- Change Person.new to new Person. t1.setPickupLocation("Art Institute"); t1.pickup(new Person("Julie", 'F', 29), "Union Station"); System.println(t1); <-- Insert .out after System. } } 2. Write the source code for the getter getPickupLocation and the setter setPickupLocation for the pickupLocation instance variable in the Taxi class. 10 points. public String getPickupLocation( ) { return pickupLocation; } public void setPickupLocation(String thePickupLocation) { pickUpLocation = thePickupLocation; } 3. Write a Junit test class that tests the getter getPickupLocation and the setter setPickupLocation that you wrote in the preceding problem. 10 points. import static org.junit.Assert.*; import org.junit.Before; import org.junit.Test; public class JUnitTest { Taxi t; @Before public void setUp() { t = new Taxi("WEUR384293", "yellow"); } @Test public void test() { assertEquals("", t.getPickupLocation( )); t.setPickupLocation("Art Institute"); assertEquals("Art Institute", t.getPickupLocation( )); } } Part F: // main method: import java.io.File; import java.io.IOException; public class Main { public static void main(String[] args) throws IOException { File f = new File("test.txt"); // Print false because file // does not yet exist. System.out.println("can read?: " + f.canRead( )); // Print false because file // does not yet exist. System.out.println("can write?: " + f.canWrite( )); // Print false because file // does not yet exist. System.out.println("exists?: " + f.exists( )); // Create new file, then retest above methods. System.out.println(f.createNewFile( )); // Print true because now file exists. System.out.println("can read?: " + f.canRead( )); // Print true because now file exists. System.out.println("can write?: " + f.canWrite( )); // Print because now file exists. System.out.println("exists?: " + f.exists( )); // Print name of file. System.out.println("File name: " + f.getName( )); // Print name of file. System.out.println("Absolute path: " + f.getAbsolutePath( )); // Print false because filename text.txt is not absolute. System.out.println("Is absolute?: " + f.isAbsolute( )); // Print false because file is not a directory. System.out.println("Is directory?: " + f.isDirectory( )); // Print 0 because file is empty. System.out.println("File length: " + f.length( )); // Create new filename for directory File g = new File("testdir"); // Print true because directory is created. System.out.println("File name: " + g.mkdir( )); // Print false because file is a directory. System.out.println("Is directory?: " + g.isDirectory( )); } } // Output can read?: true can write?: true exists?: true true can read?: true can write?: true exists?: true File name: test.txt Absolute path: C:\sjost\it313\java-workspace\TestFileMethods\test.txt Is absolute?: false Is directory?: false File length: 0 File name: true Is directory?: true