prelim3ansspring2006 - CS100J Spring 2006 Sample answers...

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

View Full Document Right Arrow Icon
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)
Background image of page 1
This is the end of the preview. Sign up to access the rest of the document.
Ask a homework question - tutors are online