List Exercise
Use eclipse for this exercise.
Part 1
Copy all files from Files, List Exercise folder. Create a class called
MyList which will hold Strings. The field in MyList is
LLNode<String> head;
Create the following methods of MyList:
- default constructor: initialize head to null
- constructor with a String parm: create a list containing the parm
- toString: return a String that contains all Strings in the list, separated
by newlines
- void addAtFront(String newstr): add newstr to the front of the list
- boolean contains(String item): return true if item is in the list; return
false if not
Create a class to contain main; main should do the following:
- create a new MyList object that contains "blue"; for all of the
following steps use the methods of the MyList class (printing
the MyList will automatically call the toString method)
- add "green" to the front of your MyList
- add "red" to the front of your MyList
- print the MyList
- prompt for a word, add it to the front
- prompt for another word, add it to the front
- print the MyList
- prompt for a String, check whether it's in the MyList; print messages
for when the String is found and when it's not found
- print the MyList
Part 2
Add the following methods to your MyList class:
- void addAfter(String before, String newstr): add newstr to the list
after before; if before is not in the list, throw NoSuchElementException
- String removeFront(): remove the first item in the list and return it; throw
NoSuchElementException if the list is empty
Add the following to the end of your client code:
- add "purple" after "green"
- add "aqua" after "purple"
- print the MyList
- prompt for two Strings, add the first after the second; if the second
is not in the list (catch the exception) print a message stating
that first could not be added because second is not in the MyList
- print the MyList
- remove and then print each the first two items from the MyList
- print the MyList
Email Me |
Office Hours |
My Home Page |
Department Home |
MCC Home Page
© Copyright Emmi Schatz 2020