Homework Assignment 2

Implementing Constructors and Command-line Arguments


810:062
Computer Science II
Object-Oriented Programming


Due: Friday, September 9, at 11:59 PM


Introduction

We have at least three different goals for this assignment:

You will continue to work with the MemoPad program for this assignment, including the database class that you implemented for Homework 1. Feel free to improve or fix your class. If you feel that your solution is in such bad shape that fixing it up will take time away from this assignment, then check with me to see what you should do.

Here are a couple of Java-specific items that might help you:

As always, all instance variables are private.


Tasks

Before starting, make a copy of MemoPad.java that you can use in later steps.

  1. Add a new constructor to your MemoDatabase class that takes one argument: the maximum number of memos that can be stored.

    The constructor should use this argument when initializing the size of the array in the constructor. The existing constructor should still use a default value for the array size. Notice that the constant now becomes the default maximum, not the actual maximum!

    Test your new constructor by using it to create the database in the MemoPad class and then running the program. Verify that the database enforces the maximum size!

    Make sure that the class's default constructor still works, too, and that it still enforces its maximum number of entries.

  2. Eliminate any code duplication in your MemoDatabase class's constructors.

    Your two constructors almost certainly have a lot of code common -- they do almost exactly the same thing! Make the duplication go away. One way to do that is to factor any common behavior into a private method named initialize.

    Re-test both constructors by using them to create the database in the MemoPad class and then running the program.

  3. Add a new constructor to the MemoPad class that takes one argument: the MemoDatabase that it uses to store the memos.

    In this constructor, use the argument to initialize the database instance variable.

    Test your new constructor by using it to create the MemoPad in the main() method and then running the program.

  4. Change the original "default" constructor back to its original form, from before Homework 1: initialize the database variable to be a DefaultMemoDatabase.

    Test the default constructor by using it to create the MemoPad in the main() method and then running the program.

  5. Eliminate any code duplication in the MemoPad constructors.

    The two constructors almost certainly have a lot of code common -- they differ in only one line! Make the duplication go away. One way to do that is to factor any common behavior into a private method named initialize.

    Re-test both constructors by using them to create the MemoPad in the main() method and then running the program.

  6. Modify the main() method in the MemoPadApp driver to accept up to two optional arguments that control the kind of MemoDatabase to be used.

    One optional argument is the choice of database class.

    If no argument is given, create a default instance of MemoPad. For example:

              $ java MemoPadApp
    ... MemoPadApp uses the default constructor of MemoPad

    If the user specifies -d, create an instance of DefaultMemoDatabase and use it to initialize the MemoPad you create. For example:

              $ java MemoPadApp -d
    ... MemoPadApp creates a DefaultMemoDatabase and passes it to MemoPad's constructor

    If the user specifies -s, then the command-line may contain a second optional argument, an integer to specify the maximum number of entries in the database. Use this integer to create an instance of your database class. If no second argument is given, create a default instance of your array-based database class. In either case, use this database object to initialize the MemoPad you create. For example:

              $ java MemoPadApp -s
    ... MemoPadApp creates an instance of your MemoDatabase (use default constructor)
    and passes it to MemoPad's constructor
    $ java MemoPadApp -s 100 ... MemoPadApp creates an instance of your MemoDatabase (use the int constructor)
    and passes it to MemoPad's constructor

    If the user gives any other command-line argument, print an error message and return without creating any objects. For example:

              $ java MemoPadApp -oops
    Usage: java MemoPadApp
    java MemoPadApp -d
    java MemoPadApp -s [size]

    Notice that only the main() method knows about the command-line arguments, not the MemoPad class or either MemoDatabase class. main() will use these arguments in order to select the correct constructor(s) to use when building the MemoPad.

Do not modify any other class. Do not modify MemoPad in any other way. We will test your two classes 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.