Homework Assignment 3

Interacting with Files


810:062
Computer Science II
Object-Oriented Programming


Due: Friday, February 3, at 11:59 AM


Introduction

This assignment asks you to extend the MemoPad program with an important behavior that MemoPads need: the ability to remember memos between runs. We'll do that by adding two responsibilities to the MemoDatabase interface: loading from a file and saving to a file.

For this assignment, download the starting Java files for the MemoPadApp in the form of the zip file homework03-template.zip. This file contains:

To "unzip" this file, use the unzip command at your Unix prompt, or double-click on the file in Linux or Windows.

To this directory, you will need to add your MyMemoDatabase class from Homework 2.


Tasks

  1. Change MemoPadApp.java to create instances of your MemoDatabase class.

    On Lines 8 and 10, MemoPadApp creates instances of a MemoDatabase for the MemoPad. Put your class's name in place of my class's name. If you named your class ArrayBasedMemoDatabase, you don't have to do anything!

  2. Add empty save() and load() methods to your MemoDatabase class.

    This should ensure that your class now implements the extended MemoDatabase interface. Compile and run the program to verify this. Of course, the Save and Load buttons won't do anything yet, but...

  3. Write the save() method for your class.

    This method takes a String argument, the name of the file to write. When the user presses the Save to File button, the MemoPad sends the database a save() message, with the current value of the Key: field as the filename.

    You get to decide the format of the data that your MemoDatabase object writes to the file. For example, my program writes key/value pairs to a file in this format: eugene>>wallingford
    philip>>east
    ben>>schafer
    john>>mccormick
    janet>>drake

    where each line is in the form "key>>value". The >> characters separate the key from the value. Of course, this means that I shouldn't use the > character in my keys or values, because my database class might confuse it with the delimiters in my file. (If you want to implement a solution that isn't as prone to this problem, great!)

    This is just an example. You don't have to use >> as the delimiter of your fields, or even write only one memo per line. Just be sure that your user can use spaces in her keys and values.

    You can use the code from  Session 4 and Session 5 as a starting point for your code that writes to a file.

    Don't forget the new import directives that you'll need to add to your class in order to use the various I/O classes that you need for this task.

  4. Use your improved class to generate three to five data files.

    Compile the program and run it several times, each time saving a new file of memos. Name your files memos1.dat, memos2.dat, .... This will both enable you to verify that your method works and generate test data for the next task!

    Be sure that each file tests something different about your program. For example, it would be useful to have an empty file and at least one file with a large number of memos in it, to test the "boundary conditions" of the program.

  5. Write the load() method for your class.

    This method also takes as an argument the name of a file, taken from the current value of the Key: field. When the user presses the Load from File button, the MemoPad sends the database a load() message.

    This method should read records of the same format as the data written to file by the save() method.

    You can use the code from  Session 4 and Session 5 as a starting point for your code that reads from a file. Keep in mind, though, that you won't need a loop around your messages to the StringTokenizer, because you know exactly how many tokens occur on each line.

    Don't forget the new import directives that you'll need to add to your class in order to use the StringTokenizer and the various I/O classes needed for this task.

  6. Use your new-and-improved class to read each of your data files.

    Compile the program and run it several times. Verify that your program can read each of the data files you created in Task 4. You should also be able to load multiple data files into the same memo pad. (*) Also verify that your program can load and save several times during the same execution.

    (*) This is a handy feature for combining memo pads created at different times or on different machines.

You should not modify any other class. In particular, you should not modify MemoPad in any way, without the express written consent of me.  I plan to grade your updated class by running it with the original versions of all the other files when we test them.

Deliverables

By the due date and time, submit the files

Be sure that your submission follows all homework submission requirements.