Homework 11

1. StringVector class (10 points)

Create a class called StringVector, which is a child of the Vector class (defined by Java). The new class definiton should include the following:

HINT: The first three methods should be implemented in one line, while the toStringArray() method will require a little more work. Utililize the methods inherited from the Vector class to make your job easier. Also, casting an object of type Object to an object of type String will be needed.

Important Notes:

  1. the line import java.util.Vector; must appear at the top of the StringVector.java source code file before anything else!
  2. the documentation for the Vector class, defined by Java, can found at: http://java.sun.com/j2se/1.3/docs/api/java/util/Vector.html


2. StringLister class (10 points)

Write a program that allows the user to enter an arbitrary number of strings (words, sentences, etc.) one line at a time, stores the strings in a single StringVector object, and then prints out a list of the strings entered in reverse order. The end of user input should be indicated when the user enters a string of length zero (i.e. just hits the enter key without typing anything in). A sample output might look like:

  Please enter a word (press 'Enter' to end input): hello

  Please enter a word (press 'Enter' to end input): this

  Please enter a word (press 'Enter' to end input): is

  Please enter a word (press 'Enter' to end input): so

  Please enter a word (press 'Enter' to end input): cool!

  Please enter a word (press 'Enter' to end input): 

  cool!
  so
  is
  this
  hello
  

3. StringSorter class [Extra Credit] (10 points)

Modify the program in Problem 2 to sort (alphabetical order) the strings stored in the StringVector and then print out the elements of the sorted vector in correct order (i.e not reverse order). A sample output might look like:

  Please enter a word (press 'Enter' to end input): hello

  Please enter a word (press 'Enter' to end input): this

  Please enter a word (press 'Enter' to end input): is

  Please enter a word (press 'Enter' to end input): so

  Please enter a word (press 'Enter' to end input): cool!

  Please enter a word (press 'Enter' to end input): 

  cool!
  hello
  is
  so
  this
  

HINT: refer to pp. 136-139 of the Savitch text for assistance on sorting strings in alphabetical order.