In this assignment we'll re-do many of the problems from homework 6 using loops instead of recursion. We'll also re-visit the adages from the beginning of the semester.
Make a copy of your StringList
class and its
subclasses. Remove all the "interesting" methods
you wrote in homework 6,
leaving only the constructors and access methods. (You may want to
write setFirst
and setRest
methods, in
addition to the getFirst
and getRest
methods
already there.)
Then write all of
the following as methods in the StringList
class,
without adding anything to the subclasses and without any methods
calling themselves.
Hint: To test whether a list is empty, use the
instanceof
operator, e.g.
while (currentList instanceof NonEmptyStringList) { ... }
concatAll
, which returns a String formed by
concatenating all the strings in the list, in order.contains
, which takes in a String and returns a
boolean indicating whether that string exactly matches any of the
ones in the list.
prefixAll
, which takes in a String and returns a
StringList formed by sticking that string in front of each
of the strings in the list. For example, if the variable
people
contained the strings "Jones", "Brown", and
"Smith", then people.prefixAll("Mr. ")
would contain
the strings "Mr. Jones", "Mr. Brown", and "Mr. Smith".append
, which takes in another StringList and
returns a StringList formed from the elements of the recipient list
followed by those of the parameter list. For example, if the
variable people1
contained the strings "Joe" and
"Mary", and people2
contained the strings "Bill",
"Phyllis", and "Jane", then people1.append(people2)
would contain the strings "Joe", "Mary", "Bill", "Phyllis", and
"Jane", in that order.stutter
, which returns a list twice as long as the
one it was given, in which each string is repeated twice in
succession. For example, if the variable people
contained "Joe" and "Mary", then people.stutter()
would
contain "Joe", "Joe", "Mary", and "Mary" in that order.removeFirst
, which takes in a String and returns a
StringList just like the original, but in which the first occurrence
of the specified String is removed. (If the string doesn't appear
in the StringList at all, it should return a StringList just like
the original.)sort
, which returns a list containing the same
strings as the original, but in alphabetical order.
insertInOrder
which takes in a String, assumes that the
StringList it's working on is already in alphabetical order, and
produces a list with the new string inserted in its appropriate
place; this method too should use loops rather than recursion.
Modify your graphics program from Homework 6 to handle an indefinite number (a list) of bouncing objects using a loop rather than recursion. It should behave exactly like your graphics program from homework 6.
Write a class named LoopProblems
containing the following methods. (If you know what a
static
method is, you should probably make these
static
, because they operate on Strings or numbers
rather than user-defined objects. If you don't, don't worry about it.)
copies
, which takes in a natural number
(i.e. 0, 1, 2, 3, ...) and a String, and returns a
StringList with that many copies of the given String.
countSubstrings
, which takes in two Strings and tells
how many times the first appears in the second. For example,
countSubstrings("ab","xyabacabraabz")
should be 3,
and countSubstrings("aa","abaaab")
should be 2.
Hint: There's another version of the
built-in startsWith
method that takes in an integer
telling where to start matching.
I'm not even going to suggest doing the match
problem
with loops; it's horrible.
Re-read my adages page, choose two to four of the adages (or one of the longer papers by Parnas, Wirth, Dijkstra, or Miller) that you think you understand now, and write a well-structured essay of 4-6 paragraphs about it. What does it mean in the context of your experience? Do you understand it better than you did in September? Do you agree or disagree with the points it makes? Support your claims with specific examples from programs you've written.
Before you start writing the programs, estimate how long they'll be, how long they'll take you to write, and how many defects you'll encounter in them. Enter these estimates in the PSP form. (If this is a major difficulty for you, you can record it in a text file or on paper.)
While working on the program, record your errors and how much time you spend on various aspects of the programming process, again using the PSP form.
You will not be graded on how close your estimate is, or how many errors you make, or how much time you spend, as long as I think all three of them are realistic. You will be graded on whether you record all this stuff. And don't wait until the program is finished to write the estimate; it's time-stamped, and you'll lose credit if it's dated the night before the assignment is due.
You should have two or three BlueJ projects -- one with all the StringList methods, one with the modified version of homework 5, and perhaps another with the RecursionProblems class (or you could just add the Recursion Problems class to the same project as StringList, since one of the methods uses StringLists). Zip each of these folders and attach them to an email to me.
If you've entered your estimates, defects, and time use in PSP, I've already got the data so you have nothing else to turn in. If you prefer recording it some other way, turn that in with your program.
The essay should be turned in either by email or on paper. If you choose email, please use plain text, RTF, HTML, or Word format -- I don't have WordPerfect.