Solved Questions for Introduction to Computing Using Java | CS 1110, Exams of Computer Science

Material Type: Exam; Class: Introduction to Computing Using Java; Subject: Computer Science; University: Cornell University; Term: Spring 2006;

Typology: Exams

Pre 2010

Uploaded on 08/30/2009

koofers-user-5fc
koofers-user-5fc 🇺🇸

10 documents

1 / 1

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CS 100J Prelim 2 Spring 2006 Answers
Question 0. (answer omitted)
Question 1. (a) All parameters and local
variables 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);
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);
}
}
}

Partial preview of the text

Download Solved Questions for Introduction to Computing Using Java | CS 1110 and more Exams Computer Science in PDF only on Docsity!

CS 100J Prelim 2 Spring 2006 Answers Question 0. (answer omitted) Question 1. (a) All parameters and local variables 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); breed= "terrier"; } Question 4. /* Remove all CATs from v and return a Vector that contains the removed CATs / public static Vector remCats( Vector v) { Vector res= new Vector(); 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+iwn, y+j*wn, wn); } else { graphics.fillRect( x+wn, y+wn, wn, wn); } } }