prelim2answer - CS100J Prelim II answers Netid djg17 Name...

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

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: CS100J Prelim II answers Netid djg17 Name David Gries 6. x y a1 a2 a1 Rhombus getSide() getAngle() Rhombus(int, int) side 5 angle 90 Square a b 25 1. (a) String line; 1. (b) int k= 1; // inv: all chars of line[0..k1] are the same while (k != line.length() && line.charAt(0) == line.charAt(k)) { k= k+1; } /* { all chars of line[0..k1] are the same and char line[k] doesn't exist or is different */ System.out.println(k); 2. See Section 2.7.2 of the text. assignment to b is illegal. area() Square(int) 3. // precondition: n > 0 b= 1; // invariant: b is a power of 2 and b <= n while (n >= 2*b) { b= 2*b; } // post: b is a power of 2 and b <= n and n < 2*b 4. body of constructor: { numerator= a; denominator= b; if (b < 0) { numerator= a; denominator= b; } body of equals: { this.reduce(); otherFrac.reduce(); return (numerator == 0 && otherFrac.numerator == 0) || (numerator == otherFrac.numerator && denominator == otherFrac.denominator); } 5. /** An instance is a square */ public class Square extends Rhombus { /** Constructor: square with side s */ public Square(int s) { super(s, 90); } /** = area of this square */ public int area() { return getSize() * getSize(); } a2 Rhombus getSide() getAngle() Rhombus(int, int) side 6 angle 90 Square area() Square(int) 7a. The repetend is the body of the while-loop. 7b. 1. How does it start? (How do we make the invariant true?) 2. When does it stop? (What condition, along with the invariant, ensures that the postcondition is true?) 3. How does it make progress? 4. How does it fix the invariant? (The repetend has to terminate with the invariant true.) } 6 ...
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