Back to 111 Main Page

Mobius strip

Assignment 11

Task

Use objects in a useful program and explore dynamic collections of objects.

Steps

You are going to write a to-do list program.

Example

Here is a 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 (named FirstLastToDoListUI.java, where First and Last are replaced by your first and last name) and a to-do list class (named FirstLastToDoList.java. 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.) If you create any other classes, be sure to prepend your FirstLast to their names.

Since we still have not covered exception-handling, you don't have to worry about what happens if a user gives you a string when you ask for numbers. However, you do need to gracefully handle any errors that arise from the user giving you integers that are out of range.

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!

Hints and Suggestions

Aside from the division between the list logic and the user interface, how you implement this is up to you.

But here's what I'd recommend. First, create a class that will hold both a string and an urgency rating. (Remember, the first benefit of objects is that you can pass a set of data around in a single object/variable.) In fact, you can use mine if you want: ToDoListItem.java. (Don't forget to rename it to include your name though.)

Then you have to write a ToDoList class. I would recommend having a single instance variable: an ArrayList called list that will contain all the ToDoListItems.

Then, you can have the following methods:

  /**
   * Adds the given item to the appropriate location in the list based on
   * its urgency.
   */
  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.)
  }

  /**
   * Removes the item at the given index from this todo list.
   * Indexing starts at 1.
   * <p>
   * Returns the removed item.
   * If the given index is out of range, returns null instead.
   */
  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
  }

  /**
   * Changes the urgency of the item at the given index of this todo list.
   * The item's position in the list is adjusted according to its new
   * urgency.
   *
   * Returns true if the item was 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) {
    //A good "cheat" 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.
  }

  /**
   * 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() {
    //build a string by looping through the list, calling toString
    //on each item and adding that to the string you are going to return.
  }

}

You're on your own for the UI class. Have a look at the SimpleToDoListUI example above.

What to submit

Attach all the classes needed to run your program to an email. Be sure that all your filenames begin with FirstLast, which correspond to your first and last name, respectively.

FAQs

Do you have any more examples?
The SimpleToDoList above covers most of the basics of what you need to know. But check out NumberList.java for how to add numbers to an ArrayList while keeping it sorted.
How do I store items in the list?
We talked in lab about 3 different options. 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.
Do you have any general tips for expanding the SimpleToDoList (assuming you're using the ToDoListItem approach)?
  • Make sure you understand what the code already does before you start changing it.
  • As much as possible, work on writing/changing only one of the four ToDoList methods at a time. Make sure your program compiles and runs before and after each step. Change the UI only as much as you need to to support each new method you've written.
  • Start by renaming all three classes you will use: the UI, the ToDoList, and the ListItem. Add your first and last name to the classnames. Remember to change the constructor names too. (Now compile and run to make sure everything still works.)
  • Remember to change the type of objects contained in your ArrayList from String to your specific ListItem class.
  • Do not declare any additional variables in your ToDoList class aside from your ArrayList. You do not need them. Especially do not declare an item or urgency instance variable in your ToDoList. Your todo list as a whole does not have an urgency. It contains many urgencies, yes; but these are all stored in the ArrayList, not in instance variables.
  • Start coding with the ToDoList's toString() method, and add the index numbering to the output. (Note how the expression
      "" + list.get(i)
    
    is equivalent to
      list.get(i).toString()
    
    That is, when concatanating an object such as a ListItem to a String, that object's toString() method will be invoked implicitly to turn the object into a string.)
  • Then change the add method to the basic two-line form given in the FAQ below. (Edit your UI a bit so that you have at least the adding and displaying functionality working. At this point, you should have a program that lets you add items and their urgencies to a list, though not in sorted order.)
  • Then do remove, including any need UI changes. (Test that it works. Your program can now unordered-add and remove.)
  • Now finally come back to add and work on getting it to add in proper sorted order. (Remember to look at NumberList.java for an example of sorted adding.)
  • Do changeUrgency last. Double-check that everything is done now with your UI too.
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 FLToDoListItem. (Remember you need to add your first and last name to all files you use/create.) 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
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

Grading

Out of 20 points:

1 - Submission
Follows required submission policies.
1 - Coding Standards
Follows required coding standards.
2 - Compiles
4 - Displaying the list
Your program first displays the contents of the list time each time it asks for user input. The list shows all items, including each item's urgency rating. Items are numbered starting at 1.
5 - Adding to the list
You can add new items to the list, specifying any string and then an urgency rating (1 to 10, only) for that item. Items are inserted into the list in urgency order with high urgency (10) before low urgency (1). In the case of a tie, the new item comes after all items with the same urgency already in the list.
3 - Removing from the list
You can remove an item from the list by specifying the displayed index number of the item to remove.
3 - Changing item urgency
You can change the urgency rating of an item already in the list. The list is still correctly sorted by urgency after the change.
1 - Error-checking
Your program gracefully handles incorrect input of the same type as requested, such as numbers out of the valid range, incorrect menu choices, etc.


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