Untitled Document
Here are a few practice problems for collection classes, discussed in Chapter
10 of the text.
You can solve all of these with Java's LinkedList class. Problems (1) and (2)
start with simple
implementations and then add more complex methods. In particular,
the eliminateDuplicates()
method in (1d) and the sort() method at the end of problem (2) need more sophistication
than
the earlier parts of these problems. Problem (3) is not very difficult and will
give you some practice
using the double type, which we haven't done a lot with this semester. This
might appeal to those
of you who are interested in more mathematical programming.
1. a) Write a class IntList that holds a LinkedList of integers. Your class should have methods
public void add(int i) // adds i to the list
public void print() // prints the entire listWrite a main() method that does several adds, then prints the list.
b) Add to class IntList a method
public void addArray(int numbers[], int size) //adds the first "size" elements of the
// array to the listExtend the main() method to test this out.
c) Add to class IntList a method
public void read() // repeatedly prints a prompt, reads values from the keyboard
// and adds them to the list. This terminates when the user enters 0.Again, extend the main() method to test this out.
d) Add to class IntList a method
public void eliminateDuplicates() //starting at the beginning of the list, this
// looks at each entry, then passes through the list removing all other
// instances of this value. For example, if the list has values
// (2, 9, 1, 2, 1, 3, 2) this will change it to (2, 9, 1, 3)
2. Write a class Name with two String variables: first and last (representing the two parts of a name). This should have methods
public void print(); // prints the name
public void read(SimpleInput keyboard) // reads a line of text and parses it
// into first name and last name fields. If the line has length 0 it sets both
// fields to the empty string.
public boolean isRealName() //returns true if the last name field is the empty
// stringpublic boolean isLess(Name n) // returns true if the current name comes before
// n inhte usual alphabetic ordering
Then write a class NameList with methods
public void read() // creates a SimpleInput keyboard and uses it to read a list
// of names, terminated by a blank line
public void print() // prints the list of names
public void sort() // sorts the list of names
Add a main() method to class NameList to test out these methods.
3. Write another class Stats similar to IntList (from problem 1), only this
class should store a list of double values. There is a standard class Double,
similar to Integer, that can serve as a wrapper for double values; it has a
method toDouble() to unpack the numeric value it holds.
Add to the class methods
public double avg() // returns the average list values
public double stdDev() // returns the standard deviation of the list values.
// This can be computed by 1) summing the squares of the data values,
// 2)subtracting n times the square of the average value, where n is the number
// of values in the list; 3) dividing this difference by (n-1); 4) computing the
// square root of this quotient.