prelim2spring2005ans

prelim2spring2005ans - values in its fields as this Letter...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
CS 100J Prelim 2 Answers Question 0: (omitted) Question 1: (a) an assertion that remains invariantly true before and after each iteration of the loop. (b) x= x + (k + 1) + (k + 2); (c) x instanceof Car Question 2: public class MySum{ /** = sum of Integers in v */ public static int sumInteger(Vector v) { int index= 0; int sum= 0; //inv: sum = sum of Integers in //v[0. . index-1] while (index < v.size()) { if (v.get(index) instanceof Integer) { sum= sum + ((Integer)v.get(index)).intValue(); } index= index + 1; } return sum; } Question 3: (a) apparent type: the type with which the variable was declared. real type: the type of the object whose name is currently in the variable. (b) (1) true (2) 100 (3) BAD. A letter object cannot be assigned to an Email variable. (4) 49 (5) BAD. The apparent type of f is Document, but letterType() is not in class Document. Question 4: (a) /** = "obj is a non-null Letter with the same
Background image of page 1
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: values in its fields as this Letter */ public boolean equals(Object obj) { return obj != null && obj instanceof Letter && addressee == ((Letter)obj).addressee && getNumPages() == ((Letter)obj).getNumPages() && getTitle() == ((Letter)obj).getTitle(); } (b) Question 5: /** = "a String formed by the first character of each word in title". Precondition: title != null */ public String getTitleAcronym() { //precondition: title != null String s= ""; int k= 0; // inv: s contains the first char of every // word in title[0. .k-1] while ( k != title.length()) { if (k==0 || title.charAt(k-1)==' ') s= s + title.charAt(k); k= k + 1; } //postcondition: s contains the first char //of every word in title[0. .title.length()-1] return s; } obj equals: 1 letter1 null...
View Full Document

This note was uploaded on 06/01/2008 for the course CS 100 taught by Professor Fan/vanloan during the Fall '07 term at Cornell.

Ask a homework question - tutors are online