CSC 172
Homework 5
Assigned 27 Oct, due 13 Nov
(formerly due 10 Nov)
-
Before you start coding, fill out an estimate form in PSP.
As you go, maintain a log of kinds and numbers of defects, and time
spent in various phases of programming. When you're done (or along the
way, if you prefer), enter these into the on-line PSP forms.
-
For all the programming assignments, be sure to follow the design recipe.
Each method should be documented, and each concrete class (and perhaps
some abstract classes) should have a test method providing a good
test suite for the class. Choose meaningful names for methods and parameters,
and watch for opportunities to use methods you've already written. Be sure
to test your program thoroughly before turning it in. If it doesn't work,
tell
me when and how it doesn't work, and what you've tried in attempting
to solve the problem.
-
Using the IntList data structure given in class, implement the following
list-processing tasks as IntListVisitors:
-
countNegatives, which returns an int indicating how
many negative numbers were in the list. For example,
(count-negatives (list 3 8 -2 5 -1 8))
2
Putting this in Java syntax,
IntList mine = new IntCons(3,new IntCons(8,new IntCons(-2,new IntCons(5,
new IntCons(-1,new IntCons(8,new EmptyIntList()))))));
System.out.println (mine.visit (new CountNegatives()));
2
-
count-matches, which takes an int parameter and counts
how many times it appears in the list. For example,
(count-matches 8 (list 3 8 -2 5 -1 8))
2
Putting this in Java syntax,
System.out.println (mine.visit (new CountMatches(8)));
2
-
count-if, which takes an IntTester parameter (an interface
that contains a test on ints), and counts the items that pass the test.
For example, in Scheme we might say
(count-if odd? (list 3 8 -2 5 -1 8))
3
Or, in Java syntax,
System.out.println (mine.visit(new CountIf(new IntTester()
{ public boolean test (int x) { return (x % 2) != 0; }})));
3
Note that the definition of IntListVisitor given in class has
methods that return an IntList. Since all of these operations
return an int, you'll need to modify IntListVisitor
slightly.
-
Now let's try the same three operations on arrays of ints. Since array
isn't a class, you can't write methods for it, so instead I want you to
write these as static methods of a class named ArrayTestbed.
For example,
int[] mine = {3,8,-2,5,-1,8};
System.out.println ("There were " + countNegatives(mine) + " negative numbers."); // 2
System.out.println ("There were " + countMatches(mine,8) + " occurrences
of 8."); // 2
System.out.println ("There were " + countIf(mine,new IntTester() {
public boolean test(int x) { return (x%2)!=0;}}) + " odd numbers."); //
3
-
Implement the same three operations (countNegatives, countMatches,
and countIf) on ArrayLists of ints, using Iterators
and built-in methods as appropriate. To enforce that your lists can only
contain ints, write your own class
IntArrayList
following the pattern of the MouseList example in the Eckel book.
Example:
IntArrayList mine = new IntArrayList ();
mine.add(3); mine.add(8); mine.add(-2); mine.add(5); mine.add(-1);
mine.add(8);
// mine.add(new Posn(3,4)) would cause a compiler error, because IntArrayList's
// "add" method only accepts ints.
System.out.println ("There were " + mine.countNegatives() + " negative
numbers."); // 2
System.out.println ("There were " + mine.countMatches(8) + " occurrences
of 8."); // 2
System.out.println ("There were " + mine.countIf(new IntTester() {
public boolean test(int x) { return (x%2)!=0;}}) + " odd numbers."); //
3
Last modified:
Stephen Bloch / sbloch@adelphi.edu