CSE2011F06/HR  Solution of #1
1. Headers (a) and (b) are clearly not typesafe because invoking
compareTo
needs a cast and that may
crash. Headers (c), (d) and (e) are typesafe but have differing versatility: (c) allows only objects that
can be compared with themselves such as a list of dates and a date, while (d) allows a list of times and
a time because time is comparable to a date. What (d) does not allow is a list of dates and a time, and
this case is handled by (e).
2. Prove by induction that:
a. The base case is a triangle. The induction hypothesis is that the predicate holds for a polygon
of
n
sides. For an
n+1
polygon, draw a chord between two vertices that are one vertex apart.
This divides it into a polygon of
n
sids and a triangle. The predicate holds for both.
b. Straightforward algebra.
c. The base case
i=0
holds because the loop skips (is not entered). And if the invariant holds
after
i
iterations, then
pow = a
i
at the top of the loop's body. The statements executed
afterwards are
pow = pow * a
, which makes
pow = a
i
* a = pow = a
i+1
, and
i+
+
, which makes
pow = a
i
. Hence, the invariant holds at the next iteration.
This preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
This is the end of the preview.
Sign up
to
access the rest of the document.
 Fall '09
 neugyen
 Insertion Sort, base case, worst case, KR smallest element

Click to edit the document details