COMP-202A, Fall 2010, All Sections FINAL EXAM SOLUTIONS SHORT QUESTIONS --------------- 1. a) Correct option: Does not cause a compilation error; an object is bound to the call Target object: this / the target of the call to method second() b) Correct option: Causes a compilation error Explanation: The call causes a compilation error because an attempt is made to call an instance method using a class name in front of the dot operator. Calls to an instance method must be bound to an instance of the class in which the method is define, and using the name of the class specifies no such instance. c) Correct option: Causes a compilation error Explanation: The this reserved word represents the target of a call to an instance method, but a static method such as third() does not have a target; therefore, the use of the this reserved word is forbidden in a static method. d) Correct option: Does not cause a compilation error; no object is bound to the call Explanation: The method being called here is third(), a static method, and calls to static methods are never bound to an object. 2. The problem lies with the line public void SerialNumber() which is found right after the member variable declarations of the SerialNumber class. The header of a constructor never specifies a return type. Therefore, the above line is the header for a method which happens to have the same name as the class, not the header of a constructor. As a result, this method does not get invoked when the new operator is used to create a new object, and no explicit constructor is declared in the SerialNumber class. This in turn causes a constructor which takes no parameters and initializes instance variables to default values to be provided for the class by the compiler. Considering that this constructor is the one that is called whenever a SerialNumber object is created, and that the default value assigned to instance variables of type int is 0, the value stored in the number instance variable of every SerialNumber which is created will be 0. To fix the problem, simply remove the reserved word void from the above line, so that it becomes: public SerialNumber() This will change the method into a proper constructor, thus ensuring that the value stored in the instance variable of every SerialNumber object is unique. 3. add(Vector2D) u + v == (4.0, 6.0) add(double, double) v + (5, 6.0) == (8.0, 10.0) add(int, int) u + (7, 8) == (14.0, 16.0) LONG QUESTIONS -------------- 4. a) - sequence (member / instance variable) b) - sequence (member / instance variable) - other (formal parameter) c) - sequence (local variable; the sequence member variable is shadowed by the sequence local variable and therefore not in scope) - other (formal parameter) - i (local variable) d) - sequence (local variable; again, the sequence member variable is shadowed by the sequence local variable and therefore not in scope) - other (formal parameter) e) - foo (formal parameter) - reader (local variable) 5. 1: {3, 2} 2: {2, 11} 3: {{2, 2}, {2, 11}} 4: [L: 101, R: 100, S: 106], [L: 104, R: 101, S: 106] 5: {[L: 37, R: 102, S: 106], [L: 41, R: 103, S: 106]} 6: {[L: 101, R: 104, S: 106], [L: 104, R: 101, S: 106]} PROGRAMMING ----------- 6. public class Loan { private Subscriber borrower; private String callNumber; private Date returnDate; private boolean returnStatus; public Loan(Subscriber myBorrower, String myNumber, Date myReturnDate, boolean myReturnStatus) { this.borrower = myBorrower; this.callNumber = myNumber; this.returnDate = myReturnDate; this.returnStatus = myReturnStatus; } public Subscriber getBorrower() { return this.borrower; } public String getCallNumber() { return this.callNumber; } public Date getReturnDate() { return this.returnDate; } public boolean isReturned() { return this.returnStatus; } public void setReturned(boolean newStatus) { this.returnStatus = newStatus; } } 7. import java.util.ArrayList; public class Subscriber { private String name; private ArrayList loans; public Subscriber(String myName) { this.name = myName; this.loans = new ArrayList(); } public String getName() { return name; } public Loan findLoan(String callNumber) { Loan theLoan; Loan element; int i; int size; theLoan = null; size = this.loans.size(); i = 0; while (i < size && theLoan == null) { element = this.loans.get(i); if (callNumber.equalsIgnoreCase(element.getCallNumber()) && !element.isReturned()) { theLoan = element; } else { i = i + 1; } } return theLoan; } public boolean addLoan(String callNumber, Date returnDate, boolean returnStatus) { boolean result; result = false; if (findLoan(callNumber) == null) { this.loans.add(new Loan(this, callNumber, returnDate, returnStatus)); result = true; } return result; } public ArrayList getLoans() { return new ArrayList(this.loans); } public ArrayList getLoans(LoanFilter filter) { ArrayList selectedLoans; Date returnDate; Loan element; int size; selectedLoans = new ArrayList(); size = this.loans.size(); for (int i = 0; i < size; i++) { element = this.loans.get(i); if (filter.accept(element)) { selectedLoans.add(element); } } return selectedLoans; } public String toString() { String result; result = name + ": " + this.loans.size() + " loan"; if (this.loans.size() > 1) { result = result + "s"; } return result; } } 8. import java.io.File; import java.util.Scanner; public class SubscriberFileReader { public static Subscriber readSubscriberFile(String fileName) throws java.io.IOException { Subscriber subscriber; Scanner reader; String callNumber; Date borrowedDate; Date returnDate; reader = new Scanner(new File(fileName)); subscriber = new Subscriber(reader.nextLine()); while(reader.hasNext()) { callNumber = reader.next(); returnDate = new Date(reader.nextInt(), reader.nextInt(), reader.nextInt()); subscriber.addLoan(callNumber, returnDate, reader.nextBoolean()); } reader.close(); return subscriber; } } 9. import java.io.File; import java.io.PrintStream; import java.util.ArrayList; public class ReportWriter { public static void generateReport(Subscriber[] subscribers, String fileName) throws java.io.IOException { PrintStream writer; ArrayList lateLoans; Loan element; Date returnDate; int size; int number; writer = new PrintStream(new File(fileName)); writer.println("LIST OF LATE DOCUMENTS"); writer.println(); writer.println("Date: " + new Date()); writer.println(); number = 0; for (int i = 0; i < subscribers.length; i++) { lateLoans = subscribers[i].getLoans(LoanFilter.LATE_LOANS); if (lateLoans.size() > 0) { writer.println("- Subscriber: " + subscribers[i].getName()); size = lateLoans.size(); for (int j = 0; i < size; i++) { element = lateLoans.get(j); writer.println("\t- " + element.getCallNumber() + " (Due: " + element.getReturnDate() + ")"); } number = number + size; } } writer.println(); writer.println("Total number of late documents: " + number); writer.close(); } } 10. public class ReportGenerator { public static void main(String[] args) throws java.io.IOException { Subscriber[] subscribers; if (args.length < 1) { System.err.println("Usage: java ReportGenerator []*"); } else { subscribers = new Subscriber[args.length - 1]; for (int i = 0; i < subscribers.length; i++) { subscribers[i] = SubscriberFileReader.readSubscriberFile(args[i + 1]); } ReportWriter.generateReport(subscribers, args[0]); } } }