Back to 111 Main Page

Mobius strip

Assignment 15

Task

Use encapsulation and multiple objects in an actual program by writing a primitive to-do list.

Steps

Example

Here is a very simple example:

This example allows a user to add whatever String they choose to the end of the list. They can also remove the first item on the list. This program assumes people always add things to the end of their to-do lists, and always complete items in a top-down order. Few people actually work this way all the time.

Your Job

Therefore, your job is to extend the functionality of this program to include the following features:

  • Every item added to the to-do list should include a user-specified urgency rating from 1 to 10.
  • The list should be kept in sorted order, with items with the highest urgency at the top of the list. The user only provides an item and its urgency; your program then determines where to insert the item in the list. If there is an urgency tie with an item already in the list, the new item should be placed after all other items with the same urgency.
  • When the list is printed, each item should be numbered (indexed), starting at 1. Each item should also display its urgency rating.
  • The user should be able to remove any item from the list using the item's number (index).
  • A user should be able to change an item's urgency. If they do so, the item changes its place appropriately in the to-do list.

As in the example above, you should have a user interface class and a to-do list class. The user interface class must be public, named UsernameA15.java, and contain your main method. Your to-do list class must be named ToDoList. Put this class in the same file as your UI, but leave off the word public. You may add any additional, non-public support classes that you need to this file. (Putting more than one class in the same file is usually bad programming practice, but we're doing it so you can upload all your code as a single file.) Make sure that the classes are separate in the file, and not nested within each other!

The list: ToDoList

Your ToDoList must have these methods. The javadoc comment explain what each should do:

  /**
   * Constructs a new empty to-do list.
   */
  public ToDoList() {
  }

  /**
   * Adds the given item to the appropriate location in the list based on
   * its urgency.
   */
  public void add(String item, int urgency) {
  }

  /**
   * Removes the item at the given index from this todo list.
   * Indexing starts at 1.
   *
   * Returns the removed item.
   * If the given index is out of range, returns null instead.
   */
  public String remove(int index) {
  }

  /**
   * Changes the urgency of the item at the given index of this todo list.
   * Indexing starts at 1.  The item's position in the list is adjusted
   * according to its new urgency.
   *
   * Returns true if the item was changed.  Returns false if it was not,
   * such as when the given index was out of range.
   */
  public boolean changeUrgency(int index, int newUrgency) {
  }

  /**
   * Returns a string representation of this todo list,
   * listing one item per line, including an initial index number.
   * If the list is empty, returns an empty string ("").
   */
  public String toString() {
  }

How you implement this internally is up to you. I'd recommend using an ArrayList of ToDoListItems, but it's up to you. (If you decide to use this ToDoListItem class, be sure to copy it into your file.)

Clarification (31 Oct 2008): changeUrgency, like remove, should take 1-based indexes. (I added a sentence on this to the javadocs above.)

The UI: UsernameA15

All printing to the screen or reading in from the keyboard should be occur only in your UI class. (We may revisit this program later, writing a different UI front-end for the same to-do list back-end.)

The UI should print the current contents of the to-do list and then a menu of options and ask the user to choose an option: 1) Add, 2) Remove, 3) Change urgency, or 0) Quit. When adding an item, ask for the item and then the urgency. Ensure that all entered numbers are valid and in range; otherwise, report the error and have the user try again.

Sample Output

TO-DO LIST:

OPTIONS:
1. Add an item to the list.
2. Remove an item from the list.
3. Change an item's urgency rating.
0. Quit.
Choose an option: 1
Enter an item to add to the list: Wash the dog
Enter an urgency rating [1 - 10]: 5

TO-DO LIST:
1. [5] Wash the dog

OPTIONS:
1. Add an item to the list.
2. Remove an item from the list.
3. Change an item's urgency rating.
0. Quit.
Choose an option: 1
Enter an item to add to the list: Wash the car
Enter an urgency rating [1 - 10]: 4

TO-DO LIST:
1. [5] Wash the dog
2. [4] Wash the car

OPTIONS:
1. Add an item to the list.
2. Remove an item from the list.
3. Change an item's urgency rating.
0. Quit.
Choose an option: 1
Enter an item to add to the list: Test my program
Enter an urgency rating [1 - 10]: 8

TO-DO LIST:
1. [8] Test my program
2. [5] Wash the dog
3. [4] Wash the car

OPTIONS:
1. Add an item to the list.
2. Remove an item from the list.
3. Change an item's urgency rating.
0. Quit.
Choose an option: 3
Enter the number of the item to change: 3
Enter an urgency rating [1 - 10]: 6

TO-DO LIST:
1. [8] Test my program
2. [6] Wash the car
3. [5] Wash the dog

OPTIONS:
1. Add an item to the list.
2. Remove an item from the list.
3. Change an item's urgency rating.
0. Quit.
Choose an option: 2
Enter the number of the item to remove: 3
Removed "Wash the dog" from list.

TO-DO LIST:
1. [8] Test my program
2. [6] Wash the car

OPTIONS:
1. Add an item to the list.
2. Remove an item from the list.
3. Change an item's urgency rating.
0. Quit.
Choose an option: 4
That is not a valid choice.  Please try again.
Choose an option: 2
Enter the number of the item to remove: 6
Could not remove entry: 6 did not correspond to an item in the list.

TO-DO LIST:
1. [8] Test my program
2. [6] Wash the car

OPTIONS:
1. Add an item to the list.
2. Remove an item from the list.
3. Change an item's urgency rating.
0. Quit.
Choose an option: 0

Thanks for using the To-Do list!

What to submit

Upload your UsernameA15.java file to Tamarin. This single file should contain all the separate classes needed to run your program.

Grading

Out of 10 points:

1 - Compiles
Your program compiles successfully (no errors)
5 - ToDoList
Your ToDoList class is named correctly (0.5) and contains a constructor that takes no parameters (0.5). It must contain the four methods as specified above:
Add (2): Items are inserted into the list (1) in urgency order (high urgency before low urgency) (0.5). In the case of a tie, the new item comes after all items with the same urgency already in the list (0.5).
Remove (1): Only the item at the specificied index is removed.
Change urgency (1): The urgency of the item at the given index is changed (0.5), and its position in the list changes accordingly (0.5).
toString (1): Items are listed one per line, including their urgencies, indexed starting at one.
4 - User interface
Your program first displays the contents of the list time each time it asks for user input. (1). Menu options are 1) Add, 2) Remove, 3) Change, and 0) Quit, each then performing the appropriate task (2). When entering a new item, asks for a String first and then the urgency (number) (0.5). Gracefully handles bad input. (0.5)

FAQs

How do I store items in the list?
The first approach is to use two ArrayLists: one for the items (Strings) and another for the urgencies (Integers). The downside of this technique is that you then need to keep the two lists in sync.

The second approach is to use one ArrayList of Strings, and just append the urgency to the end of each item before adding it to the list. The downside of this method is that it take a fair amount of String processing to compare urgencies of items already in the list.

The third approach--and the one I recommend--to use one ArrayList of ToDoListItems. The downside of this method is that you need to use instances of another class (though the class is already written for you), and so you must understand what this class does.
Can I use the code you gave us?
Yes. You can copy and paste the whole ToDoListItem class unchanged (if you choose to use it); just leave my name on it (since you didn't write it). Also, if you want to, you can start with the SimpleToDoList and the UI code and edit it to make your ToDoList (though you'll end up nearly completely rewriting it).
How do I copy the extra classes into the same file?
Just copy and paste the complete code for each class and paste them into the same file. Leave the word public on your UsernameA15 class, but remove the public from the other class(es). You only need to remove the public from the class itself; leave the public for any methods within each class.

You may have to move any import statements all together to the top of the file.

Also, make sure each class is closed before you define the next one. (It's possible to have nested classes in java, but this will probably give you errors complaining about using non-static variables in a static context or some such thing).
Could you give a little more help regarding the ToDoList methods?
public void add(String item, int urgency) {
  //Create a new ToDoListItem containing the passed item and urgency.
  //Then loop through the list one item at a time, comparing each item's
  //  urgency to the urgency of the item you want to add.
  //Once you get past all the items of greater or equal urgency, insert
  //  the new item at that index in the list.
  //
  //(If you get stuck, just add the item at the end of the list for now.
  // Get your toString and remove to work so you can see your list.  Then
  // come back and deal with getting this method to add in sorted order.)
}

public String remove(int index) {
  //if the index is in range of the list size
  //  remove the item at that index from the list and
  //  return the string that was in it
  //otherwise
  //  return null
}

public boolean changeUrgency(int index, int newUrgency) {
  //A good shortcut on this is to remove the item at the given index
  //from the list, change its urgency, and then add it back in.
  //Do the adding and removing using the methods you already wrote
  //and the item will be added back in based on its new urgency.
}

public String toString() {
  //build a string by looping through the list, calling toString
  //on each item and adding that to the string you are going to return.
}
Could you give me a hint for the add method?
Assuming you're using ToDoListItems, you can change the type contained by your ArrayList from String to ToDoListItem. Then you can do this:
  public void add(String item, int urgency) {
    //Create a new ToDoListItem containing the passed item and urgency.
    ToDoListItem toAdd = new ToDoListItem(item, urgency);
    //add this item to the list
    list.add(toAdd);
  }
This will at least let you add items with an urgency to your list. However, it does not add the item in sorted order, but just tacks it on the end of the list. (Play around with your program at this point and make sure you understand how it works.)

As you then build on this method to make it add in sorted order, consider the following expressions:
list.get(i) -- the ToDoListItem in the list at index i
list.get(i).getUrgency() -- the urgency of the item at index i
toAdd.getUrgency() -- the urgency of the item you're currently trying to add into the list
i < list.size() -- because you don't want to fall off the end of your list while looping through it

Also, you will need to use the other form of the add method in ArrayList to add something at a place other than the end of the list. Check the API for more.
I'm having compile trouble with the return type of the ToDoList's remove method.
The ToDoList's remove method says it returns a String. But remember that your ArrayList (probably) contains ToDoListItems. So this line:
  return list.remove(index);
won't compile because it's returning a ToDoListItem, not a String. Instead, you want to return only the String part the removed list item:
  ToDoListItem removed = list.remove(index);
  return removed.getItem();  //return only String part
What should the ToDoList constructor do?
Initialize any instance variables you have. One way to do this is as SimpleToDoList does: creates any ArrayLists you need.

Technically, you can instead initialize your instance variables when you declare them. If you did this, there'd be nothing left to do in your constructor, and so it could be empty. And, since Java inserts an empty default constructor that takes no parameters if you do not write a constructor, it means it's possible that you don't even need to write a constructor for your ToDoList.


~ztomasze Index : TA Details: ICS111: Assignment 15
http://www2.hawaii.edu/~ztomasze
Last Edited: 02 Nov 2008
©2008 by Z. Tomaszewski.