1 Apply your knowledge of Java s precedence relations to determine to answer the following 1 Primitive Operations Principles questions a What is printed by the following statement System out println 2 4 4 2 b Add parentheses to the expression 2 4 4 2 so that it computes 6 a 4 Solution 2 4 4 2 2 For this question and its parts use the following de nitions public static int op int x int y return x y public static double op double x double y return x y public static double op int x double y return 2 x y a What is the result of evaluating System out println op op 1 2 2 b What is the result of evaluating System out println op op 1 2 2 0 a 5 b 10 0 2 Questions about Classes 1 Recall that any attempt to divide a number by 0 throws an ArithmeticException The class ArithmeticException is a subclass of Java s RuntimeException class With this in mind assume that the statement badMethodCall in the code fragment below always generates various ArithmeticExceptions but in no particular order but at least one of these is a divide by zero exception try badMethodCall generates several Arithmetic Exceptions but in no particular order catch exception 1 catch exception 2 return return Assume that the programmer writes try badMethodCall catch RuntimeException re System err println Caught a runtime exception return catch ArithmeticError ae System err println Caught an Arithmetic exception return Which of the following statements is true A Sometimes the program prints Caught an Arithmetic exception but sometimes the program prints Caught a runtime exception B The program always prints Caught a runtime exception C The program always prints Caught an Artithmetic exception D We do not have enough information to choose from any of these possibilities Page 2 2 Given the following partial de nitions public class Person implements Comparable Person public class Student extends Person private String studentID public class Teacher extends Person private String facultyID a Write the equals method on the class Person Solution public boolean equals Object obj if this other return true if obj null return false if this getClass obj getClass return false Person otherPerson Person obj return this compareTo otherPerson 0 c Override the equals method on the Teacher class so that two Teachers are equal if they are equal according to the class Person and have equal teacherIDs Solution public boolean equals Object other if this other return true if other null return null if this getClass other getClass return false Teacher otherTeacher Teacher other return super equals other this teachersID equals otherTeacher teachersID b Page 3 3 Using Arrays and ArrayLists 1 In the space below write the rightPartition method that takes an array of Comparable objects and a Comparable object called pivot The rightPartition method then returns a new ArrayList that contains all of the objects from the original ArrayList that are greater the pivot public static ArrayList Comparable rightPartition Comparable array Comparable pivot begin here Solution public static ArrayList Comparable rightPartition Comparable array Comparable pivot ArrayList Comparable returnArray new ArrayList Comparable for int index 0 index list length index if array index compareTo pivot 0 returnArray add array index return returnArray Page 4 2 Write the eitherOr method that takes two ArrayLists of distinct integers meaning that no integer appears more than once in either list and returns a new ArrayList of integers that appear in one or the other For example eitherOr 1 2 3 2 3 4 1 2 3 4 eitherOr 1 2 3 1 2 3 eitherOr 3 2 1 1 2 3 1 2 3 etc Note for this question the order of elements in any of these ArrayLists is unimportant You may assume that each parameter is an ArrayList that contains no duplicate integers You may only use the ArrayList operators discussed in class in your implementation public static eitherOr ArrayList Integer list1 ArrayList Integer list2 Solution public static eitherOr ArrayList Integer list1 ArrayList Integer list2 ArrayList Integer returnList new ArrayList Integer for int index 0 index list2 size index returnList add list2 get index clone not allowed returnList is now contains elements from list2 for Integer elementOfList1 list1 if returnList contains elementOfList1 returnList add elementOfList1 return returnList 4 Additional concepts not included in review but tested Make sure that you review common de nitions in particular class variables which are static variables de ned within a class reference versus shallow versus deep copy schemes and logical expression On this last note pay attention to how negation Java s operator can be removed from common arithemetic comparisons a b a b a b c d a b c d etc Page 5
View Full Document