For all the programming assignments, be sure to follow the design recipe.
Each problem asks you either to create a new class with some methods,
or to add a new method to a class you've already written.
For classes, you need to identify the names and types
of the instance variables, then write each of the methods.
For each method, go through
the usual contract, examples,
skeleton, inventory,
body, test steps.
I recommend writing the examples by using the
tester
library, as discussed in class on Feb. 11, so you
can easily run all your tests and confirm that they all pass.
Be sure to choose meaningful names for methods and parameters, and watch for opportunities to re-use methods you, I, or the textbook have already written.
Also turn in a log of time spent and errors encountered in the assignment, with brief comments describing each error ("mismatched parentheses" is self-explanatory, but more complex errors might need more description). You may do this using the PSP forms, or simply by keeping track in a text file or on paper and turning it in.
concatAll
, which takes no explicit parameters and
concatenates all the Strings in the list into one big long
String.maxLength
, which takes no explicit parameters and
computes the maximum length of Strings in the list. You may want to
use the built-in Math.max
method, or you can do it
yourself with conditionals.contains
, which takes in a String and tells whether
or not that String appears as one of the elements of the
StringList.countMatches
, which takes in a String and tells
how many times (possibly zero) that String appears as an
element of the StringList.IntList
class analogous to
StringList
but with each element an int
.
Write the following methods:
length
and add
, by analogy with the
corresponding methods in StringList
.addUp
, which takes no explicit parameters and
returns the sum of all the numbers in the IntList.multiply
, which takes no explicit parameters and
returns the product of all the numbers in the IntList.
anyOver100
, which takes no explicit parameters and
returns whether any of the numbers in the list are over 100.allOver100
, which takes no explicit parameters and
returns whether all of the numbers in the list are over
100. (See previous hint.)countOver
, which takes an int parameter and tells
how many of the elements of the IntList are larger than this
number.largest
, which takes no explicit parameters and
returns the largest number in the list. BookList
class analogous to
StringList
and IntList
, but with each element
a Book
(from homework 3). Write the following methods:
length
and add
, by analogy with the
corresponding methods in StringList
anyBefore
, which takes in a number and tells
whether any of the books was published before that year.avgYear
, which takes no explicit parameters and
returns the average of the publication dates of all the books in the
list.
Hint: You'll probably need a "helper" method.addToEnd
, which takes in a String and returns a
StringList with that String added as the last entry.
Hint: This will be trickier than
add
, which added an entry at the front.lengths
, which takes no explicit parameters and
returns an IntList with the same number of elements as this
StringList. Each element of the resulting IntList should be the
length of the corresponding element of the StringList. checkExpect (new ESL().add("fnord").add("abc").add("beeblebrox").lengths(), new EIL().add(5).add(3).add(10));
prefixEach
, which takes a String and returns a
StringList with the same number of elements as this
.
Each element of the resulting StringList should be the given String
concatenated with the corresponding element of this
.
checkExpect (new ESL().add("fnord").add("abc").add("beeblebrox").prefixEach("xyz"), new ESL().add("xyzfnord").add("xyzabc").add("xyzbeeblebrox"));
append
, which takes a StringList and returns a
StringList containing all the elements of this
,
followed by the elements of the parameter StringList.
checkExpect (new ESL().add("abc").add("f").add("r").append( new ESL().add("xyz").add("fnord")), new ESL().add("xyz").add("fnord").add("abc").add("f").add("r"));
removeAll
, which takes a String and returns a
StringList containing all the elements of this
that do
not match the given String. checkExpect (new ESL().add("abc").add("f").add("abc").add("abc").add("g").add("xyz").removeAll("abc"), new ESL("f").add("g").add("xyz"));
removeFirst
, which takes a String and returns a
StringList containing all the elements of this
, minus the
first match (if any) of the given String. If the String
parameter doesn't appear at all, return the StringList unchanged.
checkExpect (new ESL().add("abc").add("f").add("abc").add("abc").add("g").add("xyz").removeFirst("abc"), new ESL().add("abc").add("f").add("abc").add("g").add("xyz"));(Remember,
add
adds things at the front, so
the last thing added is the "first" thing in the list.)
removeFirstN
, which takes an int n
and a String, and returns a StringList containing all the elements of
this
, minus the first n
matches of the given
String. If the String parameter doesn't appear at all, return the
StringList unchanged. If it appears n
or fewer times,
remove all of them.
checkExpect (new ESL().add("abc").add("f").add("abc").add("abc").add("g").add("xyz").removeFirstN(2,"abc"), new ESL().add("abc").add("f").add("g").add("xyz"));(Remember,
add
adds things at the front, so
the last thing added is the "first" thing in the list.)removeBefore
, which takes in a number and returns a
BookList containing all the elements of the original BookList
except those published before that year.You should end up with classes StringList
,
IntList
, BookList, two subclasses of each of those,
a test class for each, and the Book class copied from homework 3.
I recommend putting all of them in
one BlueJ project.
Zip them up and attach it to an e-mail to me.
If you've entered your time
and error logs using the on-line forms, I've already got them; if not,
e-mail me your log files.
Pretty much the same as the past few assignments.