Skip to content

Instantly share code, notes, and snippets.

@bhnascar
Last active May 1, 2016 21:55
Show Gist options
  • Save bhnascar/7f7a7714f82bde366ae27789c2995598 to your computer and use it in GitHub Desktop.
Save bhnascar/7f7a7714f82bde366ae27789c2995598 to your computer and use it in GitHub Desktop.
Free response #5
Let's imagine you're a really forgetful person, so you decide to make
a to-do list program to help you remember things.
Everything that you want to do has a description and a time. For
example, uh..."buy eggs at Costco on Wednesday". So let's represent
things that you want to do with the following class:
public class ThingToDo
{
private String description; // Ex. "Buy eggs at Costco"
private String day; // Ex. "Wednesday"
/* Creates a new ThingToDo object with the given description
* and date. */
public ThingToDo(String d1, String d2) {
description = d1;
day = d2;
}
/* Returns a description of the thing you need to do. */
public String getDescription() {
return description;
}
/* Returns the day that this thing should be done by. */
public String getDueDay() {
return day;
}
/* Other methods not shown. */
}
Now we're going to finish writing a class called ToDoList that helps
you keep track of things you want to do...which (in programming-speak)
means we're going to write a class that keeps track of a bunch of
ThingToDo objects.
Conceptually speaking, any to-do list program that's actually going to
be useful should have some basic functionality. You should be able to:
1. Add things that you need to do to a list
2. Remove things that you have finished from that list
3. Get (back) the whole list of things that you've put down
Finish writing the class below so that it it can do all of the
three things above. Note that I've already started you off by
creating an instance variable called "things" that can hold a bunch
of ThingToDo objects. You just need to write the part that will
add stuff to "things" and delete stuff from "things" in the proper
way.
public class TodoList
{
/* List of things to do. */
private List<ThingToDo> things = new ArrayList<ThingToDo>();
/* Part 1. Adds something to this to-do list. The user
* will specify what they want to do (description)
* and when they want to get it done (day).
*/
public void addSomething(String description, String day);
/* Part 2. Removes a completed item from this to-do list.
* The user will tell you what to remove (description).
*/
public void removeSomething(String description);
/* Part 3. Removes all items for a given day. The user
* will tell you which days to remove things from.
* This is an extra method which could be convenient to
* the user.
*/
public void removeAllThingsForDay(String day);
/* Part 4. Get a list of things that you should do on
* a given day. Returns an ArrayList of everything
* the user said they wanted to get done on the
* given day.
*/
public ArrayList<String> getThingsForDay(String day);
}
When the class is completed, I should be able to use it like this:
ToDoList list = new ToDoList();
list.addSomething("Buy eggs at Costco", "Sunday");
list.addSomething("Take the AP CS A test", "Tuesday");
list.addSomething("Go home and nap", "Tuesday");
list.removeSomething("Buy eggs at Costco");
// Imagine it's Tuesday and you want to know what you have to do
// today.
ArrayList<ThingToDo> stuff = list.getThingsForDay("Tuesday");
// stuff will contain: ["Take the AP CS A test", "Go home and nap"]
// Now it's after the AP and you just woke up so you take can take
// your Tuesday things off the list...
list.removeAllThingsForDay("Tuesday");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment