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. You may do this using the PSP forms, or simply by keeping track in a text file or on paper and turning it in.
Starting with the StringList
class from your previous
homework, write the following methods:
substitute
, which takes in a StringList and two
Strings (old and new) and returns a
StringList formed by replacing every occurrence of old with
new.dictionarySort
, which takes in a
StringList and returns a new StringList with exactly the same Strings,
but in dictionary (aka "lexicographic") order.
Hint: Use the
compareTo
method that comes with the String class.
Another hint: You'll need a helper method.
This problem deals with voting in an election.
Candidate
class to represent a candidate in
an election: each Candidate should have a name and a number of votes.
Write a constructor, getters, toString
, the usual stuff.
CandidateList
class to represent a list
of such candidates. Write the usual stuff.
results
which takes in a
StringList of votes and returns a list of Candidate
s,
sorted by how many votes they got, with the most votes first and the
fewest votes last. If two candidates have the same number of votes,
they should both appear, but I don't care in which order.
For example, if the votes were
Anne, Bob, Charlie, Anne, Charlie, Mary, Bob, Charliethe answer should be either
[[Charlie,3], [Bob,2], [Anne,2], [Mary,1]]or
[[Charlie,3], [Anne,2], [Bob,2], [Mary,1]](Note that you don't have to produce a string formatted like this; my point is only that it should produce a list of Candidates with that information in that order.)
This problem deals with lists of lists.
StringListList
class to represent a list
of StringLists. Write the usual stuff.
subsets
method that takes in a
StringList and produces a list of all of its subsets. You may assume
that the given StringList doesn't have any repeats; don't worry about
alphabetical order or anything like that.
For example, if the original list were
[robin, bluebird, sparrow]the answer might be something like
[[],[robin],[bluebird],[sparrow],[robin,bluebird],[robin,sparrow],[bluebird,sparrow],[robin,bluebird,sparrow]]
scramble
method that takes in a StringList and
produces a list of all possible orderings of those strings. You may
assume that the given StringList doesn't have any repeats.
For example, if the original list were
[robin, bluebird, sparrow]the answer might be something like
[[robin,bluebird,sparrow],[robin,sparrow,bluebird],[bluebird,robin,sparrow],[bluebird,sparrow,robin],[sparrow,robin,bluebird],[sparrow,bluebird,robin]]
Pretty much the same as the past few assignments.