Homework Assignment 2: Constructors and Command-line Arguments


Computer Science II (810:062)


Due: Friday, January 27, at 11:59 AM (noon)


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 MyMemoDatabase 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. )

For this homework, you will modify the MemoPadApp class to accept some optional command-line arguments. When running MemoPadApp, the possible command-line arguments that it will accept are:

Command-line Execution Meaning
java MemoPadApp
Use the original DefaultMemoDatabase
java MemoPadApp -d
Use the original DefaultMemoDatabase
java MemoPadApp -s
Use your original student version of MyMemoDatabase from homework 1
java MemoPadApp -s 100
Use your MyMemoDatabase that is created by a new constructor that limits the database to the specified maximum size (in this example it is 100)

Here is a Java-specific items that might help you:

Here is a statement that retrieves the integer value of the characters stored in the string stringValue:

Tasks

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

  1. Add a new constructor to your MyMemoDatabase 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!
  1. Eliminate any code duplication in your MyMemoDatabase 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.
  1. 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.
  1. 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.
  1. 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.
  1. Modify the main() method in the MemoPadApp driver to accept up to two optional arguments that control the kind of MemoDatabase to be used.
The following table describe the actions of MemoPadApp for each option.

Command-line Execution Actions taken by MemoPadApp
java MemoPadApp
MemoPadApp uses the default constructor of MemoPad
 
java MemoPadApp -d
MemoPadApp creates a DefaultMemoDatabase and passes it to the new MemoPad constructor
that takes a MemoDatabase parameter
java MemoPadApp -s
MemoPadApp creates an instance of your MyMemoDatabase (use default constructor) and passes it to the new MemoPad constructor
 
java MemoPadApp -s 100
MemoPadApp creates an instance of your MyMemoDatabase using the new constructor that takes an int parameter, and then passes it to the new MemoPad 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.


Deliverables

By the due date and time, submit the files

Be sure that your submission follows all homework submission requirements.