prelim2spring2006ans - breed=...

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

View Full Document Right Arrow Icon
CS 100J Prelim 2 Spring 2006 Answers Question 0. (answer omitted) Question 1. (a) All parameters and local vari- ables are created when the frame for the call is drawn, before execution of the method body. (b) y= y + (h – 1) + 101; (c) 1. How does it start? (How do you truthify the invariant?) 2. When can it stop? 3. How does the repetend make progress toward ter- mination? 4. How does the repetend maintain the invariant? Question 2. true, Error, 20, 30, “meow”, “” , “meow”, Error, “”, Error. Question 3 . /** = ob is a non-null dog with the same fields as this Dog */ public boolean equals(Object ob) { if (ob == null ) return false ; if (!(ob instanceof Dog)) return false ; Dog dob= (Dog) ob; return getName().equals(dob.getName()) && getAge() == dob.getAge() && breed.equals(dob.breed); } /** Constructor: a Dog that is a terrier, has name Spot, and is y years old */ public Dog( int y) { super ("Spot", y);
Background image of page 1
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: breed= "terrier"; } Question 4. /** Remove all CATs from v and return a Vector that contains the removed CATs */ public static Vector<Cat> remCats( Vector <Animal> v) { Vector<Cat> res= new Vector<Cat>(); int k= 0; // inv: v[0. .k-1] contains no Cats and // res contains the Cats that have been // removed from v[0. .k-1] while (k != v.size()) { Animal a= v.get(k); if (a instanceof Cat) { Cat c= (Cat)a; res.add(c); v.remove(k); } else k= k+1; } return res; } Question 5. /** Draw a sierpinski carpet of width and height w with top left corner at (x, y). Precondition: w is a power of 3 Precondition: the square to be drawn on is black and the pen color is white. */ public static void sc( int x, int y, int w) { if (w < 3) return ; int wn= w/3; for ( int k= 0; k != 9; k= k+1) { if (k != 4) { int i= k/3; int j= k%3; sc(x+i*wn, y+j*wn, wn); } else { graphics.fillRect( x+wn, y+wn, wn, wn); } } }...
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 University (Engineering School).

Ask a homework question - tutors are online