prelim2fall2005ans - values in its fields as this...

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 + (k + 1); (c) b instanceof Employee Question 2: public class MyCount { /** = no of elements of v that are Integers with value 0 */ public static int howMany(Vector v) { int c= 0; // inv: c = no. of elements of v[0. .i-1] // that are Integers with value 0 for ( int i= 0; i != v.size(); i= i+1) { if (v.get(i) instanceof Integer && ((Integer)v.get(i)).intValue() == 0) { c= c+1; } } return c; } } 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) 197 (3) BAD. Can’t cast a Documentary to a Short. (4) 239 (5) BAD. The apparent type of f is Movie, but documentaryType() is not defined in class Movie. Question 4: (a) /** obj is a non-null Documentary with 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 Documentary */ public boolean equals(Object obj) { if (obj == null || !(obj instanceof Documentary)) return false ; Documentary docObj= (Documentary)obj; return getTitle().equals(docObj.getTitle()) && getLength() == docObj.getLength() && topic.equals(docObj.topic); } (b) d1 Question 5: /** = title but with vowels removed (except at beginning of a word) */ public String shortTitle() { String s= ""; int k= 0; // inv: s contains title[0,. .k-1] but with vowels // removed (except at word begins) while (k != title.length() ) { if (title.charAt(k) != 'a' && title.charAt(k) != 'e' && title.charAt(k) != 'i' && title.charAt(k) != 'o' && title.charAt(k) != 'u' ) s= s + title.charAt(k); else if (k == 0 || title.charAt(k-1) == ' ') s= s + title.charAt(k); k= k+1; } return s; } Obj docObj equals: 1 A0 null ? A0...
View Full Document

Ask a homework question - tutors are online