Docsity

Sample Answers for Prelim 3 - Introduction to Computing Using Java | CS 1110, Papers of Computer Science

Toggle sidebar
bg1
CS100J Spring 2006 Sample answers for prelim 3
1.
public static int numberAncestors(Rhino r) {
int num= 0;
if (r.father != null)
num= num + 1 + numberAncestors(r.father);
if (r.mother != null)
num= num + 1 + numberAncestors(r.mother);
return num;
}
2a. 5 instances of Box are created: 4 horizontal ones
and 1 vertical one.
2b. 4*4 buttons are created.
2c.
3.
public static boolean isTridiagonal(int[][] m) {
for (int k= 0; k != m.length; k= k+1) {
// return false if row k contains a non-zero
// where it should have a 0.
for (int i= 0; i != m.length; i= i+1) {
if (i != k-1 && i != k &&
i != k+1 && m[k][i] != 0) {
return false;
}
}
}
return true;
}
4a.
/** Constructor: integer i in Mod n arithmetic
Precondition: n > 1. There are no restrictions on i.*/
public Mod(int i, int modulus) {
this.i= i;
this.modulus= modulus;
modularize();
}
/** If r1 and r2 do not have the same modulus,
return null; otherwise return r1 + r2 */
public static Mod add(Mod r1, Mod r2) {
if (r1.modulus != r2.modulus)
return null;
return new Mod(r1.i + r2.i, r1.modulus);
}
/** = r is a non-null Mod with the same modulus
and integer as this one */
public boolean equals(Object ob) {
return ob != null &&
ob instanceof Mod &&
i == ((ob)r).i &&
modulus == ((ob)r).modulus;
}
4b1. Make a class abstract so that it cannot be
instantiated (one cannot create objects of the
class).
public abstract class Example {
…
}
4b2. Page 148 of the text says this: The first
statement of a subclass constructor must be a call
on a superclass constructor. If you do not put one
in, Java inserts this one: super(); .” This is done to
enforce the principle that fields in the superclass
should be initialized before those in the subclass.
4b3. A local variable of a method is created when
the frame for the method call is drawn (or created
inside the computer).
4b4. A static variable goes in the file drawer of the
class in which it is declared, and it has to be there
throughout execution. Hence, it is created just
before execution of a program begins.
5. /** = index i that satisfies b[0..i] ≤ x < b[i+1..]
*/
public static int bsearch(int[] b, int x) {
int i= -1; int j= b.length;
// invariant: b[0..i] ≤ x and x < b[j..]
while (i+1 != j) {
int e= (i+j)/2;
// {-1 ≤ i < e < j ≤ b.length}
if (b[e] <= x) i= e;
else j= e;
}
return i;
}

Partial preview of the text

Download Sample Answers for Prelim 3 - Introduction to Computing Using Java | CS 1110 and more Papers Computer Science in PDF only on Docsity!

CS100J Spring 2006 Sample answers for prelim 3

public static int numberAncestors(Rhino r) { int num= 0; if (r.father != null ) num= num + 1 + numberAncestors(r.father); if (r.mother != null ) num= num + 1 + numberAncestors(r.mother); return num; } 2a. 5 instances of Box are created: 4 horizontal ones and 1 vertical one. 2b. 4*4 buttons are created. **2c.

public static boolean** isTridiagonal(int[][] m) { for ( int k= 0; k != m.length; k= k+1) { // return false if row k contains a non-zero // where it should have a 0. for ( int i= 0; i != m.length; i= i+1) { if (i != k-1 && i != k && i != k+1 && m[k][i] != 0) { return false ; } } } return true ; } 4a. /** Constructor: integer i in Mod n arithmetic Precondition: n > 1. There are no restrictions on i./ public Mod( int i, int modulus) { this .i= i; this .modulus= modulus; modularize(); } /* If r1 and r2 do not have the same modulus, return null; otherwise return r1 + r2 / public static Mod add(Mod r1, Mod r2) { if (r1.modulus != r2.modulus) return null ; return new Mod(r1.i + r2.i, r1.modulus); } /* = r is a non-null Mod with the same modulus and integer as this one */ public boolean equals(Object ob) { return ob != null && ob instanceof Mod && i == ((ob)r).i && modulus == ((ob)r).modulus; }

4b1. Make a class abstract so that it cannot be

instantiated (one cannot create objects of the

class).

public abstract class Example {

4b2. Page 148 of the text says this: The first

statement of a subclass constructor must be a call

on a superclass constructor. If you do not put one

in, Java inserts this one: super (); .” This is done to

enforce the principle that fields in the superclass

should be initialized before those in the subclass.

4b3. A local variable of a method is created when

the frame for the method call is drawn (or created

inside the computer).

4b4. A static variable goes in the file drawer of the

class in which it is declared, and it has to be there

throughout execution. Hence, it is created just

before execution of a program begins.

5. /** = index i that satisfies b[0..i] ≤ x < b[i+1..]

public static int bsearch( int [] b, int x) {

int i= - 1; int j= b.length;

// invariant: b[0..i] ≤ x and x < b[j..]

while (i+1 != j) {

int e= (i+j)/2;

// {- 1 ≤ i < e < j ≤ b.length}

if (b[e] <= x) i= e;

else j= e;

return i;