Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I have a project in java but in jgrasp it fails several tests. I found the problem to be that I need to create a

I have a project in java but in jgrasp it fails several tests. I found the problem to be that I need to create a TODOList Test object but I'm not exacly sure where to start with it, any help?

Here is the code to work with:

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 /* YOUR Instance variable declarations here. */ 26 private String[] TODOList; 27 private int initialLength; 28 private int numItems; 29 30 /* Constructor that initializes the initialLength variable to the value passed in. 31 * It also initializes the toDoList with the initial length. 32 * Any other instance variables may be initialized as well. 33 */ 34 public TODOList(int initialLen){ 35 this.initialLength = initialLen; 36 this.TODOList = new String[initialLen]; 37 this.numItems = 0; 38 } 39 40 /* Add the item passed in to the end of the list. 41 * For example, if the toDoList list contained: "eat lunch", "walk dog", 42 * the next item added, "study Java", would result in this list: 43 * "eat lunch", "walk dog", "study Java" 44 * Suggestion: use instance variable to keep track of the index of the next available cell. 45 */ 46 public void addItem(String itemStr){ 47 if(TODOList.length > numItems){ 48 TODOList[numItems] = itemStr; 49 numItems += 1; 50 } 51 } 52 53 /* Overwrite the item at "position" to be the parameter "itemStr". 54 * Note: position is a positive integer > 0 that has to be a valid position 55 * in the toDoList. A valid position corresponds to an item stored in the 56 * toDoList. For example, if this was the list: 57 1 walk the cat 58 2 order doughnuts 59 3 go to the gym 60 4 wash dishes 61 * valid positions would be 1, 2, 3, 4. All other integers are invalid. 62 * This method returns true if a valid position was passed in, false otherwise. 63 */ 64 public boolean replaceItemAt(String itemStr, int position){ 65 if(position > 0 && position 0 && numItems > 0){ 81 numItems -= 1; 82 TODOList[numItems] = null; 83 return true; 84 } 85 return false; 86 } 87 88 /* 89 * This method returns the number of items stored in the item list. 90 * The method returns a String array that contains only the items that have been added. 91 * This array does not contain any NULL values. 92 * For example, if the toDoList list contained: "eat lunch", "walk dog", "study Java", NULL, NULL, 93 * the getToDoList method would return an array with these Strings: "eat lunch", "walk dog", "study Java". 94 * If the toDoList does not contain any items, this method returns a String array with zero length. 95 */ 96 public String[] getToDoList(){ 97 int numLoop = 0; 98 String[] result = new String[numItems]; 99 while(numLoop 0){ 152 return false; 153 } 154 return true; 155 } 156 157 /****** Private, "helper" method section ******/ 158 159 /* Creates a new array that is double the size of the array passed in, copies the data 160 * from that array to the new array, and returns the new array. 161 * Note that the new array will contain the items from the previous array followed by NULL values. 162 */ 163 private String[] expandList(String[] inputList){ 164 String[] newArray = new String[inputList.length * 2]; 165 int numLoop = 0; 166 while(numLoop image text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribed

1 /This class encapsulates a list of user-defined items that should be done- a "TODO" list. 2Each item on the list is represented by a String object. 3The list is implemented by a String array. The array is initialized in the constructor to an 4initial length that is passed to the constructor. The initial array contains only NULL values 5A 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 7Thus, the array may have fewer items (Strings) than its length. For example, assume the list has an initial length of 5. It looks like this: 10 NULL, NULL, NULL, NULL, NULL 12 Then, a user adds three items. It looks like this: 13"eat lunch"walk dog","study Java", NULL, NULL 14 15The 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 17or equal to its capacity. 18 19If a user wants to add more items to a list that has no more NULL values, i.e. no more room, 20the 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 f 24 25 26 27 28 29 30Constructor that initializes the initialLength variable to the value passed in /YOUR Instance variable declarations here./ private String[] TODOList; private int initialLength; private int numItems

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

Step: 3

blur-text-image

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

Secrets Of Analytical Leaders Insights From Information Insiders

Authors: Wayne Eckerson

1st Edition

1935504347, 9781935504344

More Books

Students also viewed these Databases questions