Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Compute TODOList.java ONLY TODOList.java 1 /* This class encapsulates a list of user-defined items that should be done- a TODO list. 2 * Each item

Compute TODOList.java ONLY TODOList.java 1 /* This class encapsulates a list of user-defined items that should be done- a "TODO" list. 2 * Each item on the list is represented by a String object. 3 * The list is implemented by a String array. The array is initialized in the constructor to an 4 * initial length that is passed to the constructor. The initial array contains only NULL values. 5 * A user adds an item (a String) to the list by calling the addItem method, passing in a String 6 * that represents the to-do item. 7 * Thus, the array may have fewer items (Strings) than its length. 8 * 9 * For example, assume the list has an initial length of 5. It looks like this: 10 * NULL, NULL, NULL, NULL, NULL 11 * 12 * Then, a user adds three items. It looks like this: 13 * "eat lunch", "walk dog", "study Java", NULL, NULL 14 * 15 * The length of the list is 5, the number of items is 3. The NULL values are unoccupied cells. 16 * The capacity of the array is its length. The size of the data stored in the array is less than 17 * or equal to its capacity. 18 * 19 * If a user wants to add more items to a list that has no more NULL values, i.e. no more room, 20 * the expandArray method is called to double the length of the toDoList. The original Strings 21 * are in the same positions, but the new array has double the capacity- more room for adding items. 22 */ 23 public class TODOList { 24 25 26 /* YOUR Instance variable declarations here. */ 27 28 29 /* Constructor that initializes the initialLength variable to the value passed in. 30 * It also initializes the toDoList with the initial length. 31 * Any other instance variables may be initialized as well. 32 */ 33 public TODOList(int initialLen){ 34 35 } 36 37 /* Add the item passed in to the end of the list. 38 * For example, if the toDoList list contained: "eat lunch", "walk dog", 39 * the next item added, "study Java", would result in this list: 40 * "eat lunch", "walk dog", "study Java" 41 * Suggestion: use instance variable to keep track of the index of the next available cell. 42 */ 43 public void addItem(String itemStr){ 44 45 } 46 47 48 49 50 /* Overwrite the item at "position" to be the parameter "itemStr". 51 * Note: position is a positive integer > 0 that has to be a valid position 52 * in the toDoList. A valid position corresponds to an item stored in the 53 * toDoList. For example, if this was the list: 54 1 walk the cat 55 2 order doughnuts 56 3 go to the gym 57 4 wash dishes 58 * valid positions would be 1, 2, 3, 4. All other integers are invalid. 59 * This method returns true if a valid position was passed in, false otherwise. 60 */ 61 public boolean replaceItemAt(String itemStr, int position){ 62 return false; 63 } 64 65 /* Remove the last item in the toDoList. 66 * For example, if the toDoList list contained: "eat lunch", "walk dog", "study Java", 67 * removing the last item would result in this list: 68 * "eat lunch", "walk dog". 69 * This method returns true if there is at least one item in the list, 70 * false otherwise. 71 */ 72 public boolean removeLastItem(){ 73 return false; 74 } 75 76 /* 77 * Return a list that contains only the items that have been added. 78 * This list does not contain any NULL values. 79 * For example, if the toDoList list contained: "eat lunch", "walk dog", "study Java", NULL, NULL 80 * This method would return this array: "eat lunch", "walk dog", "study Java" 81 * If the toDoList does not contain any items, this method returns a String array with zero length. 82 */ 83 public String[] getToDoList(){ 84 return null; 85 } 86 87 /* Remove all items from the list, resulting in an empty list. 88 * The capacity of the list returns to the initial length. 89 */ 90 public void clearToDoList(){ 91 } 92 93 /* Returns a String representation of the current item list according to 94 * these specifications: 95 * Each item is on one line, position number first followed by one blank, 96 * followed by the item String. 97 * For example: 98 1 walk the cat 99 2 order doughnuts 100 3 go to the gym 101 4 wash dishes 102 * If no items are on the list the String returned is: "no items". 103 */ 104 public String getToDoListAsString(){ 105 return null; 106 } 107 108 /* Returns the number of items stored in the item list. 109 */ 110 public int getNumberOfItems(){ 111 return 5000; 112 } 113 114 /* Returns true if the item list contains no items, false otherwise. 115 */ 116 public boolean isEmpty(){ 117 return false; 118 } 119 120 /****** Private, "helper" method section ******/ 121 122 /* Creates a new array that is double the size of the array passed in, copies the data 123 * from that array to the new array, and returns the new array. 124 * Note that the new array will contain the items from the previous array followed by NULL values. 125 */ 126 private String[] expandList(String[] inputList){ 127 return null; 128 } 129 130 /* A full item list is an array where all cells contain an item. That 131 * means there is no cell that contains NULL. 132 * This method returns true if all cells in the array contain a String 133 * object, false otherwise. 134 */ 135 private boolean isFull(){ 136 return true; 137 } 138 } TODOListMain.java

1 import java.util.Scanner; 2 /* This class runs a console interface between a user and 3 * an instance of an EventSchedule. 4 */ 5 public class TODOListMain { 6 7 public static void main(String[] args){ 8 9 System.out.println("TODO List"); 10 Scanner scan = new Scanner(System.in); 11 int initialSize = 5; 12 TODOList toDoList = new TODOList(initialSize); 13 14 boolean keepGoing = true; 15 String userStr = ""; 16 int position; 17 18 while(keepGoing) { 19 System.out.println("Main Menu:"); 20 System.out.println("Enter A to add an item."); 21 System.out.println("Enter R to replace an item."); 22 System.out.println("Enter L to remove the last item."); 23 System.out.println("Enter P to print all item."); 24 System.out.println("Enter C to clear all items."); 25 System.out.println("Enter X to quit."); 26 System.out.println(""); 27 userStr = scan.nextLine(); 28 29 if (userStr.equalsIgnoreCase("A")){ 30 System.out.println("Enter the item: "); 31 userStr = scan.nextLine(); 32 toDoList.addItem(userStr); 33 } 34 else if (userStr.equalsIgnoreCase("R")){ 35 System.out.println("Enter the number of the item to be replaced:"); 36 position = Integer.parseInt(scan.nextLine()); 37 System.out.println("Enter the replacement item:"); 38 userStr = scan.nextLine(); 39 if(toDoList.replaceItemAt(userStr, position)) 40 System.out.println("Item replaced."); 41 else 42 System.out.println("Item not replaced- invalid number."); 43 } 44 else if (userStr.equalsIgnoreCase("L")){ 45 if(toDoList.removeLastItem()) 46 System.out.println("Last item removed."); 47 else 48 System.out.println("Item not removed- list empty."); 49 } 50 else if (userStr.equalsIgnoreCase("P")){ 51 System.out.println("Your items: "); 52 System.out.println(toDoList.getToDoListAsString()); 53 } 54 else if (userStr.equalsIgnoreCase("C")){ 55 toDoList.clearToDoList(); 56 System.out.println("Items cleared."); 57 } 58 else if(userStr.equalsIgnoreCase("X")) 59 keepGoing = false; 60 else 61 System.out.println("Unrecognized input."); 62 } 63 System.out.println("Bye for now."); 64 scan.close(); 65 } 66 }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

a. What are S, F, and P? Pg45

Answered: 1 week ago

Question

d. How were you expected to contribute to family life?

Answered: 1 week ago