Exceptions Worksheet - Hope College



Name________________________

All methods are from TreeMapBible that has field TreeMap verseMap. Assume that verseMap has n elements.

1. Give the worst-case complexity of the following method (assume references has m elements).

public VerseList getVerses(ArrayList references) {

VerseList temp = new VerseList(version, "some verses");

for (Reference ref : verseMap.keySet()) {

for (Reference r : references) {

if (ref.equals(r)) {

temp.add(verseMap.get(ref));

}

}

}

return temp;

}

2. Rewrite the previous method so that it is as efficient as possible and give the worst-case complexity of the new version. (Hint: the get method on a TreeMap takes O(log n) time on a map with n elements.)

3. The getVerses method has another problem—what if a Reference is invalid? The original version would have not included it in the results. Your version might include a Verse with the text set to null or it might include null in the list.

a. Which one of these three options do you think is best? Why?

b. Give the code that implements the second and third possibilities. Just give the relevant lines, and only include both if your version above did the first option.

4. Consider the following implementations of getVerse, which should return a Verse with the given Reference, or null if the verse isn’t in the given Bible object.

public Verse getVerse_1(Reference r) {

if (isValid(r)) {

return new Verse(r, verseMap.get(r).getText());

}

return null;

}

public Verse getVerse_2(Reference r) {

return new Verse(r, verseMap.get(r).getText());

}

public Verse getVerse_3(Reference r) {

return verseMap.get(r);

}

a. Which of these work as specified?

b. Two of them create a new Verse rather than just returning the Verse from the map. Why is this (creating a new version of an object to return) sometimes a good idea?

c. Explain why it actually isn’t necessary to create a new Verse. In other words, why don’t the reasons you gave in part b apply in this case?

d. Give the best (concise, but clear) implementation of getVerse that you can. (Hint: think about the things that can “go wrong”.)

5. Consider the following implementations of isValid, each of which does some things well, and other things not so well.

public boolean isValid_1(Reference ref) {

if (ref == null) {

return false;

}

if (theVerses.containsKey(ref)) {

return true;

}

return false;

}

public boolean isValid_2(Reference ref) {

if (theVerses.containsKey(ref) == true) {

return true;

}

return false;

}

public boolean isValid_3(Reference ref) {

return theVerses.containsKey(ref);

}

a. Specify what is wrong with each of these (i.e. errors or ways the code could be improved).

b. Write the best (correct, concise, and clear) implementation of isValid that you can.

6. What potential drawbacks are there with using TreeMap rather than TreeMap?

7. What potential benefits are there with using TreeMap rather than TreeMap?

................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download