Homework Assignment 4
Adding File Interaction to AssociationLists
810:062
Computer Science II
Object-Oriented Programming
Due: Thursday, February 17, at 8:00 AM
Introduction
This assignment asks you to add to the AssociationList class
the ability to remember Associations between executions. We
will do that by giving AssociationList methods for loading
Associations from a file and saving them to a file.
Tasks
Add these behaviors to the AssociationList class:
- The user can ask an AssociationList for its
size.
- The user can ask an AssociationList to clear
itself, which results in the AssociationList containing no
Associations.
- The user can ask an AssociationList to
saveToFile, passing the name of the output file
as a String argument. In response, the AssociationList
writes all of its Associations to the target file. It does
not change its own state.
- The user can ask an AssociationList to
loadFromFile, passing the name of the input file
as a String argument. In response, the AssociationList
reads all of the Associations in the source file and adds them
to its collection. Any Associations that were already in the
AssociationList will still be there.
- The user can ask an AssociationList to
loadFromFileSorted, passing the name of the input
file as a String argument. In response, the
AssociationList reads all of the Associations
in the source file and adds them to its collection. It adds the
new Associations in ascending order of their keys as it
adds to its vector. Any Associations that were already
in the AssociationList will still be there, in the order
they already were in.
Do the following to demonstrate your code:
- Write a driver class named AssociationListDemo that
- creates an AssociationList,
- adds at least ten Associations in unsorted order
to the AssociationList,
- writes the AssociationList to a file named
"notes-unsorted.txt",
- clears the AssociationList,
- reads the Associations back into the
AssociationList in sorted order, and
- writes the AssociationList to a new
file named "notes-sorted.txt"
Programming Advice
Use the code from
Session 10
as a starting point for your code that reads and writes files.
Don't forget the new import directives that you'll need
to add to your class in order to use the new I/O classes.
As always, write your program in the style we have been using all
semester. First, write a test that expresses what you would like
your code to do. Then write the code to do it. Finally, refactor
your code to improve the design. Use the GenerateTest program
to create your test class.
Take small steps, and your tests will give you feedback as
soon as possible. For each task above, you should be able to write
at least two tests. For the tasks that involve files, you should
probably write even more.
For example, I would write saveToFile() by first writing a
test that writes an empty AssociationList
to a file. Why? Because then I can be sure that my code can create
a new text file successfully before I have to worry about how to write
actual Associations to the file.
For now, you will have to visually inspect the output file generated
by saveToFile(), because you don't yet know enough Java to
do it in a JUnit test. What do you imagine we will need to do to
write a test in Java?
Deliverables
By the due date and time, submit the files
- AssociationListTest.java
- AssociationList.java
- AssociationListDemo.java
- notes-unsorted.txt
- notes-sorted.txt
Be sure that your submission follows all
homework submission requirements.
Eugene Wallingford .....
wallingf@cs.uni.edu ....
February 10, 2005