1. Using Greenfoot and TextWorld, make a
subclass Homework71
of Text
with a method
public int countWords(String wordToMatch)
that returns the
number of words identical to wordToMatch
. For example, when you
invoke countWords
with the argument "Alice"
(note
the double quotes) on Alice in Wonderland, you should get the answer 223.
Grading criteria:
Homework71
with a method
public int countWords(String wordToMatch)
?"Alice"
."Alice"
. 15 points
2. Make a subclass Homework72
of Text
with a
method public int countPairs(String word1, String word2)
that
returns the number of word pairs identical to the given words. For example,
when you invoke countPairs
with the arguments
"March"
and "Hare"
(note the quotes) on Alice in
Wonderland, you should get the answer 16. This is a bit tricky because you
get the words one at a time. The trick is to remember whether the preceding
word matched. Here is an outline:
public int countPairs(String word1, String word2) { . . . boolean justMatched = false; for (String word : getWords()) { if (word.equals(word1)) { justMatched = true; } else if (word.equals(word2)) { if (justMatched) // the previous one matched AND this one matched--we have a winner { . . . } . . . } else { . . . } } return . . .; }
Grading criteria:
Homework72
with a method
public int countMatches(String word1, String word2)
?"Mad"
and
"Hatter"
.20 points
Attach just the two class files Homework71.java and Homework72.java to
your submission. They will be in your TextWorld folder. Be sure to attach the
.java
files, not the .class
files!!!