In: Computer Science
Write two classes, ToDoList and Driver
ToDoList should implement the following UML class diagram:
ToDoList |
---|
-list:String[] -size:int |
+ToDoList() +add(String item):void +remove(String item): boolean +toString():String |
add(String item) should add the item as the last element in the list, updating size.
remove(String item) should remove the
item and return true, or, if the
item was not in the list, return
false.
To remove the item, check every
list element from 0 to
size-1, and if that item is equal to the parameter
using s1.equalsIgnoreCase(s2), loop from
i+1 to size, assigning each
element the value of the following element in the list, so
list[j-1] = list[j]. The size of
the list will be one less.
toString() should return a numbered list, with a "\n" after each list item.
HW22 should run as in the sample run below. Use static methods as needed to input the choice and the list item.
1. add an item
2. remove an item
3. print the list
4. quit
Enter your choice: j
Enter a number: 6
1 through 4 only: 3
Empty list
1. add an item
2. remove an item
3. print the list
4. quit
Enter your choice: 1
Enter a list item and press enter: Mow the lawn
1. add an item
2. remove an item
3. print the list
4. quit
Enter your choice: 1
Enter a list item and press enter: Wash the car
1. add an item
2. remove an item
3. print the list
4. quit
Enter your choice: 1
Enter a list item and press enter: Clean the kitchen
1. add an item
2. remove an item
3. print the list
4. quit
Enter your choice: 3
1. Mow the lawn
2. Wash the car
3. Clean the kitchen
1. add an item
2. remove an item
3. print the list
4. quit
Enter your choice: 2
Enter a list item and press enter: clean the kitchen
clean the kitchen removed
1. add an item
2. remove an item
3. print the list
4. quit
Enter your choice: 3
1. Mow the lawn
2. Wash the car
1. add an item
2. remove an item
3. print the list
4. quit
Enter your choice: 2
Enter a list item and press enter: Write a program
Could not remove Write a program
1. add an item
2. remove an item
3. print the list
4. quit
Enter your choice: 4
add(String item) should add the item as the last element in the list, updating size.
remove(String item) should remove the item and return true, or, if the item was not in the list, return
//------------ ToDoList.java ------------
public class ToDoList {
private String[] list;
private int size;
public ToDoList()
{
size = 0;
list = new String[1];
}
public void add(String item)
{
list[size] = item;
size++;
if(size == list.length)
{
String[] newList
= list.clone();
list = new
String[list.length * 2];
System.arraycopy(newList,0,list,0,newList.length);
}
}
public boolean remove(String item)
{
int i;
boolean found = false;
for(i = 0;i<size;i++)
{
if(list[i] !=
null && list[i].equalsIgnoreCase(item))
{
int j;
for(j = i;j<size-1;j++)
{
list[i] = list[j+1];
}
list[j]=null;
found = true;
break;
}
}
return found;
}
@Override
public String toString()
{
String res = "";
for(int i =
0;i<list.length;i++)
{
if(list[i] !=
null)
{
res += "\n"+(i+1)+". "+list[i];
}
}
return res;
}
}
//------------ Driver.java ------------
import java.util.Scanner;
public class Driver {
Scanner in = new Scanner(System.in);
public int getChoice()
{
System.out.print("\n1. add an
item\n" +
"2. remove an item\n" +
"3. print the list\n" +
"4. quit\n"
+"Enter your choice: ");
try
{
return
Integer.parseInt(in.nextLine().trim());
}
catch(Exception e)
{
return 0;
}
}
public String getItem()
{
System.out.print("\nEnter a list
item and press enter: ");
return in.nextLine().trim();
}
public static void main(String[] args)
{
Driver driver = new Driver();
ToDoList list = new
ToDoList();
int choice;
do
{
choice =
driver.getChoice();
String
item;
switch(choice)
{
case 1:
item =
driver.getItem();
if(item.equals(""))
{
System.out.println("\nList item must not empty and must not contain
only whitespaces");
continue;
}
list.add(item);
break;
case 2:
item =
driver.getItem();
if(item.equals(""))
{
System.out.println("\nList item must not empty");
continue;
}
if(list.remove(item))
{
System.out.println(item+" removed");
}
else
{
System.out.println("Could not remove "+item);
}
break;
case 3:
System.out.println((list+"").equals("") ? "\nEmpty list" :
list);
break;
case 4:
System.out.println("\nQuitting...");
break;
default:
System.out.println("\nEnter
choice 1 through 4 only");
break;
}
}while(choice != 4);
}
}