ANSWERS FOR MIDTERM REVIEW QUESTIONS -- WINTER 08 1. 73 decimal = 64 + 8 + 1 = 01001001 binary = 49 hex. -28 decimal = -x. Consider x = 28 decimal = 16 + 8 + 4 = 00011100 binary. Apply the 2s complement algorithm: Complement to get 11100011 --> 11100100 binary = E4 hex. 2. 00110110111100010110011110110110 = 0011 0110 1111 0001 0110 0111 1011 0110 = 3 6 F 1 6 7 B 6. It is positive because it begins with 0. It is even because it ends with 0. 3. EF 72 4. public class Primes { // Answer to Questions 4b. public static boolean isFactor(int m, int n) { return n % m == 0; } // Answer to Question 4b public static boolean isPrime(int n) { for(int i = 2; i <= n - 1; i++) if (isFactor(i, n)) return false; return true; } public static void main(String[ ] args) { // Test isFactor System.out.println(isFactor(3, 12)); System.out.println(isFactor(3, 17)); System.out.println( ); // Test isPrime on primes. System.out.println(isPrime(2)); System.out.println(isPrime(97)); System.out.println( ); // Test isPrime on non-primes. System.out.println(isPrime(12)); System.out.println(isPrime(39)); System.out.println( ); } } Output: true false true true false false 5. An argument is a value that is passed in to a method when the method is called. A parameter is specified in the method definition to tell the method what to do with the parameters. In the following, x is the argument and n is the parameter: public static int sqr(int n) { return n * n; } public static void main(String[ ] args) { int a = 5; System.out.println(a); } 6. Two methods are overloaded if they have the same name but different signatures. 7. A method should be declared public if it is intended to be used outside of the class definition. Most methods are public. 8. A method is only declared private if it is a helper method intended only to be used within the class where it is defined. 9. The instance variable age is defined to be write-once: public class Person { private int age; private boolean ageFlag = false; public void setAge(int newAge) { if (!ageFlag) { age = newAge; ageFlag = false; } } } 10. import java.util.StringTokenizer; public class ExtractTokens { public static void main(String[ ] args) { String str = "abc;145;true;M;99.95;end"; StringTokenizer st = new StringTokenizer(str, ";"); String[ ] array = new String[10]; int index = 0; while(st.hasMoreTokens( )) array[index++] = st.nextToken( ); for(int i = 0; i <= array.length; i++) System.out.println(array[i]); } } 11. These are the legal method calls: f('a') Main.f('a') x = g( ) x = this.g( ) 12. Here are the corrected files: // File Pair.java public class Pair { private int x; private int y; public Pair(int theX, int theY) { this.x = theX; y = theY; } public int getX( ) { return x; } public int getY( ) { return y; } public void setX(int theX) { x = theX; } public void setY(int theY) { y = theY; } public String toString( ) { return String.format("(%d,%d)", getX( ), getY( )); } public boolean equals(Object o) { int otherX = ((Pair) o).x; int otherY = ((Pair) o).y; return x == otherX && y == otherY; } public static Pair add(Pair p, Pair q) { return new Pair(p.x + p.y, q.x + q.y); } } // TestPair.java public class TestPair { public static void main(String[ ] argv) { Pair u = new Pair(5, 7); Pair v = new Pair(1, 3); Pair w = new Pair(5, 7); System.out.println(u.getX( )); System.out.println(u); Pair s = Pair.add(u, v); System.out.println(s); System.out.println(v.equals(s)); System.out.println(u.equals(w)); System.out.println(u == w); } } 13. a. UML Diagram: +-----------------------------------+ | Vehicle | +-----------------------------------+ |-makeModel : String | |-vin : String | |-year : int | |-miles : int | +-----------------------------------+ |+Vehicle(String, String, int, int) | |+getMakeModel( ) : String | |+getVin( ) : String | |+getYear( ) : String | |+getMiles( ) : String | |+setMiles(int) | |+toString( ) : String | |+equals(Object) : boolean | +-----------------------------------+ b. Code for Vehicle.java. public class Vehicle { public String makeModel; public String vin; public int year; public int miles; public Vehicle(String theMakeModel, String theVin, int theYear, int theMiles) { makeModel = theMakeModel; vin = theVin; year = theYear; miles = theMiles; } public String getMakeModel( ) { return makeModel; } public String getVin( ) { return vin; } public int getYear( ) { return year; } public int getMiles( ) { return miles; } // Do not allow odometer rollback. public void setMiles(int theMiles) { if (miles < theMiles) miles = theMiles; } public String toString( ) { return String.format("%s %s %d %d", makeModel, vin, year, miles); } public boolean equals(Object o) { String otherVin = ((Vehicle) o).vin; return vin == otherVin; } } c. Code for TestVehicle.java public class TestVehicle { public static void main(String[ ] args) { Vehicle s = new Vehicle("Toyota Corolla", "AC34543ST", 2001, 55065); Vehicle t = new Vehicle("Ford Taurus", "VP84730UY", 1999, 75965); Vehicle u = new Vehicle("Toyota Corolla", "AC34543ST", 2001, 55065); System.out.println(s); System.out.println(t); System.out.println(u); System.out.println(s.getMakeModel( ) + " " + s.getVin( ) + " " + s.getYear( ) + " " + s.getMiles( )); t.setMiles(85000); System.out.println(s.equals(t)); System.out.println(s.equals(u)); } } 14. public class Duplicates { public static void main(String[ ] args) { Vehicle[ ] array = { new Vehicle("Toyota Corolla", "AC34543ST", 2001, 55065), new Vehicle("Ford Taurus", "VP84730UY", 1999, 75965), new Vehicle("Toyota Corolla", "AC34543ST", 2001, 55065), new Vehicle("Ford Taurus", "VP84730UY", 1999, 75965), new Vehicle("Ford Taurus", "VP84730UY", 1999, 75965), new Vehicle("Toyota Corolla", "AC34543ST", 2001, 55065), new Vehicle("Toyota Corolla", "AC34543ST", 2001, 55065), new Vehicle("Ford Taurus", "VP84730UY", 1999, 75965), new Vehicle("Toyota Corolla", "AC34543ST", 2001, 55065) }; for(int i = 0; i < array.length; i++) System.out.println(array[i]); removeDuplicates(array); System.out.println( ); for(int i = 0; i < array.length; i++) System.out.println(array[i]); } public static void removeDuplicates(Vehicle[ ] v) { for(int i = 0; i < v.length; i++) for(int j = i + 1; j < v.length; j++) if(v[i] != null && v[j] != null && v[i].equals(v[j])) v[j] = null; } } 15. Trace for InsertionSort a[0] a[1] a[2] a[3] temp ------+------+------+------+------- 43 11 25 33 11 11 43 25 33 25 11 25 43 33 33 11 25 33 43 Trace for SelectionSort a[0] a[1] a[2] a[3] -------+------+------+------ 43 11 25 33 11 43 25 33 11 25 43 33 11 25 33 43 16. public static void main(String[ ] args) { int sum = 0; for(int i = 0; i < args.length; i++) sum += args[i].length( ); System.out.println("Average word length = " + (double) sum / args.length); } 17. public static void main(String[ ] args) throws FileNotFoundException { Scanner s = new Scanner(new FileReader(args[0])); int sum = 0, count = 0; String word; while(s.hasNext( )) { word = s.next( ); sum += word.length( ); count++; } System.out.println("Average word length = " + (double) sum / count); } 18. public static void main(String[ ] args) throws FileNotFoundException { Scanner s = new Scanner(new FileReader(args[0])); PrintWriter pw = new PrintWriter(args[1]); String line, word; StringTokenizer st; while(s.hasNext( )) { line = s.nextLine( ); st = new StringTokenizer(line, " "); while(st.hasMoreTokens( )) { word = st.nextToken( ); if (word.length( ) == 4) pw.print("**** "); else pw.print(word + " "); } pw.println( ); } pw.close( ); } 19. horse dog