Here are some review questions to give you extra practice with what you have learned so far and help you prepare for the second midterm exam.
- Implement the following two static methods. Note that,
although the two methods compute the same function, the first one
clears the given NaturalNumber while the second
one restores it.
/** * Returns the product of the digits of {@code n}. * * @param n * {@code NaturalNumber} whose digits to multiply * @return the product of the digits of {@code n} * @clears n * @ensures productOfDigits1 = [product of the digits of n] */ private static NaturalNumber productOfDigits1(NaturalNumber n) {...}
/** * Returns the product of the digits of {@code n}. * * @param n * {@code NaturalNumber} whose digits to multiply * @return the product of the digits of {@code n} * @ensures productOfDigits2 = [product of the digits of n] */ private static NaturalNumber productOfDigits2(NaturalNumber n) {...}
- Implement the static method declared as follows:
/** * Reports the value of {@code n} as an {@code int}, when {@code n} is * small enough. * * @param n * the given {@code NaturalNumber} * @return the value * @requires n <= Integer.MAX_VALUE * @ensures toInt = n */ private static int toInt(NaturalNumber n) {...}
- Implement the static method declared as follows:
/** * Reports whether the given tag appears in the given {@code XMLTree}. * * @param xml * the {@code XMLTree} * @param tag * the tag name * @return true if the given tag appears in the given {@code XMLTree}, * false otherwise * @ensures <pre> * findTag = * [true if the given tag appears in the given {@code XMLTree}, false otherwise] * </pre> */ private static boolean findTag(XMLTree xml, String tag) {...}
- For each of the following terms (in no particular order),
provide a short definition.
- design-by-contract
- precondition
- postcondition
- testing
- debugging
- parameter mode
- clears
- replaces
- restores
- updates
- immutable type
- primitive type
- reference type
- object
- aliasing
- declared type/static type
- object type/dynamic type
- implements
- extends
- method overriding
- subinterface/derived interface/child interface
- superclass/base class/parent class
- polymorphism
- recursion
- Carefully trace over the following code. Draw a picture (of
the kind used on Slide 5 in Arrays
and References) of the value of the array after the
initialization code completes.
What is wrong with the code above and how would you fix it so that its behavior matches the comment?/* * Initialize an array of NaturalNumbers with values 1 through 5. */ NaturalNumber[] array = new NaturalNumber[5]; NaturalNumber count = new NaturalNumber2(1); for (int i = 0; i < array.length; i++) { array[i] = count; count.increment(); }
- Argue from the definition of extends that NaturalNumber extends Standard as shown on Slide 2 in Concepts of Object-Oriented Programming.
- Argue from the definitions of extends and implements that C4 implements I2 and that C3 implements I1 on Slides 11-12 in Concepts of Object-Oriented Programming.
Additional Questions
- Implement the static method declared as follows:
/** * Reports the value of {@code i} as a {@code NaturalNumber}, when {@code i} * is non-negative. * * @param i * the given value * @requires i >= 0 * @ensures fromInt = i */ private static NaturalNumber fromInt(int i) {...}
- Modify your implementation of the static method toInt
so that it is an instance method for NaturalNumber
declared as follows:
/** * Reports the value of {@code this} as an {@code int}, when {@code this} is * small enough. * * @return the value * @requires this <= Integer.MAX_VALUE * @ensures toInt = this */ public int toInt() {...}