Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I have a project in java but in jgrasp it fails several tests. I found the probl

ID: 3741433 • Letter: I

Question

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.

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;

Explanation / Answer


I have fixed the code in the several methods. It should work now. Please check with the modified code and let me know if any issues.
To indent code in eclipse , select code by pressing ctrl+a and then indent using ctrl+i
Please do rate the answer if it was helpful. Thank you
/* This class encapsulates a list of user-defined items that should be done- a "TODO" list.
* Each item on the list is represented by a String object.
* The list is implemented by a String array. The array is initialized in the constructor to an
* initial length that is passed to the constructor. The initial array contains only NULL values.
* A user adds an item (a String) to the list by calling the addItem method, passing in a String
* that represents the to-do item.
* Thus, 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:
* NULL, NULL, NULL, NULL, NULL
*
* Then, a user adds three items. It looks like this:
* "eat lunch", "walk dog", "study Java", NULL, NULL
*
* The length of the list is 5, the number of items is 3. The NULL values are unoccupied cells.
* The capacity of the array is its length. The size of the data stored in the array is less than
* or equal to its capacity.
*
* If a user wants to add more items to a list that has no more NULL values, i.e. no more room,
* the expandArray method is called to double the length of the toDoList. The original Strings
* are in the same positions, but the new array has double the capacity- more room for adding items.
*/
public class TODOList {
/* YOUR Instance variable declarations here. */
private String[] TODOList;
private int initialLength;
private int numItems;
/* Constructor that initializes the initialLength variable to the value passed in.
* It also initializes the toDoList with the initial length.
* Any other instance variables may be initialized as well.
*/
public TODOList(int initialLen){
this.initialLength = initialLen;
this.TODOList = new String[initialLen];
this.numItems = 0;
}
/* Add the item passed in to the end of the list.
* For example, if the toDoList list contained: "eat lunch", "walk dog",
* the next item added, "study Java", would result in this list:
* "eat lunch", "walk dog", "study Java"
* Suggestion: use instance variable to keep track of the index of the next available cell.
*/
public void addItem(String itemStr){
if(isFull())
TODOList = expandList(TODOList);
TODOList[numItems] = itemStr;
numItems += 1;
}
/* Overwrite the item at "position" to be the parameter "itemStr".
* Note: position is a positive integer > 0 that has to be a valid position
* in the toDoList. A valid position corresponds to an item stored in the
* toDoList. For example, if this was the list:
1 walk the cat
2 order doughnuts
3 go to the gym
4 wash dishes
* valid positions would be 1, 2, 3, 4. All other integers are invalid.
* This method returns true if a valid position was passed in, false otherwise.
*/
public boolean replaceItemAt(String itemStr, int position){
if(position > 0 && position <= numItems){
TODOList[position -1] = itemStr;
return true;
}
return false;
}
/* Remove the last item in the toDoList.
* For example, if the toDoList list contained: "eat lunch", "walk dog", "study Java",
* removing the last item would result in this list:
* "eat lunch", "walk dog".
* This method returns true if there is at least one item in the list,
* false otherwise.
*/
public boolean removeLastItem(){
if(numItems > 0){
numItems -= 1;
TODOList[numItems] = null;
return true;
}
return false;  
}
/*
* This method returns the number of items stored in the item list.
* The method returns a String array that contains only the items that have been added.
* This array does not contain any NULL values.
* For example, if the toDoList list contained: "eat lunch", "walk dog", "study Java", NULL, NULL,
* the getToDoList method would return an array with these Strings: "eat lunch", "walk dog", "study Java".
* If the toDoList does not contain any items, this method returns a String array with zero length.
*/
public String[] getToDoList(){
String[] result = new String[numItems];
for(int i = 0; i < numItems; i++)
result[i] = TODOList[i];
return result;
}
/* Remove all items from the list, resulting in an empty list.
* The capacity of the list returns to the initial length.
*/
public void clearToDoList(){
numItems = 0;
TODOList = new String[initialLength];
}
/* Returns a String representation of the current item list according to
* these specifications:
* Each item is on one line, position number first followed by one blank,
* followed by the item String.
* For example:
1 walk the cat
2 order doughnuts
3 go to the gym
4 wash dishes
* If no items are on the list the String returned is: "no items".
*/
public String getToDoListAsString(){
String result = "";
if(numItems == 0){
result = "no items";
return result;
}
for(int i = 0; i < numItems; i++)
result += (i+1) + " " + TODOList[i] + " ";
return result;
}
/* Returns the number of items stored in the item list.
*/
public int getNumberOfItems(){
return numItems;
}
/* Returns true if the item list contains no items, false otherwise.
*/
public boolean isEmpty(){
if(numItems > 0){
return false;
}
return true;
}
/****** Private, "helper" method section ******/
/* Creates a new array that is double the size of the array passed in, copies the data
* from that array to the new array, and returns the new array.
* Note that the new array will contain the items from the previous array followed by NULL values.
*/
private String[] expandList(String[] inputList){
String[] newArray = new String[inputList.length * 2];
for(int i = 0; i < numItems; i++)
newArray[i] = inputList[i];
return newArray;
}
/* A full item list is an array where all cells contain an item. That
* means there is no cell that contains NULL.
* This method returns true if all cells in the array contain a String
* object, false otherwise.
*/
private boolean isFull(){
if(numItems == TODOList.length){
return true;
}
return false;
}
}