So I have a project in compsci but I keep getting an \"illegal start of expressi
ID: 3739542 • Letter: S
Question
So I have a project in compsci but I keep getting an "illegal start of expression" in the getToDoAsString" method but I cannot find the error, I feel like it's staring right at me but I can't see it. Here's the code in text and in attachments. Thanks in advance.
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
If we define public method inside a public method then we will receive illegal start of expression error
The method clearToDoList() defined at line number 109 have public method definitions inside it.
To solve this problem, please add "}" closing bracket after line number 115.
I have done the same for you.
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(TODOList.length > numItems){
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 <= initialLength && TODOList[position - 1] != null){
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(initialLength > 0 && 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(){
int numLoop = 0;
String[] result = new String[numItems];
while(numLoop < initialLength && TODOList[numLoop] != null){
result[numLoop] = TODOList[numLoop];
numLoop++;
}
return null;
}
/* Remove all items from the list, resulting in an empty list.
* The capacity of the list returns to the initial length.
*/
public void clearToDoList(){
int numLoop = 0;
while(numLoop < TODOList.length && TODOList[numLoop] != null){
TODOList[numLoop] = null;
numItems -= 1;
numLoop++;
}
}
/* 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()
{
int numLoop = 0;
String result = "";
if(TODOList[0] == null){
result = "no items";
return result;
}
while(numLoop < initialLength && TODOList[numLoop] != null){
result = result + numLoop + 1 + " " + TODOList[numLoop] + " ";
numLoop++;
}
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];
int numLoop = 0;
while(numLoop < inputList.length && inputList[numLoop] != null){
newArray[numLoop] = inputList[numLoop];
numLoop++;
}
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;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.