Welcome to Lesson 19!
Learning Objectives
By the end of today's class, you should know...
- How do you access data at the head and tail of the List?
- How do you write the toString() method for a List?
The List Class Continued
Accessing Data at the Head of the List
- The purpose of the getHead access method is to return the data that is currently stored at the start of the List.
- Note that it throws an exception when the List is empty.
/**
* Returns the data stored at the head node
* @return the first element
* @throws IllegalStateException when List is empty
*/
public E getHead() throws IllegalStateException{
if (length == 0) {
throw new IllegalStateException("getHead(): List is empty.");
}
return head.data;
}
- When calling this method, you will need to place the method call inside of a try-catch.
Writing toString()
- toString() of the List class, is similar to toString() for the ListArray class.
- Create an empty String which will ultimately store the List as a String
- Create a temporary iterator to reference to the first node
- Using a loop, concatenate the data for each node
- Advance the temporary iterator
- Stop the loop when the iterator has gone off the end of the List
- Return the resulting String, with a new line appended to the end
Activity 19.1: List Class Part 1 (10 pts)
- Open up a new file List.java
- Take the starter code for the generic List class, as well as the constructors, insertHead, and the below toString methods and to create a List class.
- Fill in the the missing lines of code below to complete the toString method.
/**
* Used to display the contents of the
* List with each value separated by a blank space
* At the end of the List a new line
* @return the List as a String for display
*/
@Override public String toString() {
String result = "";
Node temp = head;
while(temp != null) {
result += fill in here
//fill in here
}
result += "\n";
return result;
}
- Next, write insertTail and add it to the class.
/**
* Inserts a new element at the tail
* @param data the data to insert at the
* tail of the list
*/
public void insertTail(E data) {
}
- Finally, write the get Tail method and add it to the class:
/**
* Returns the data stored at the tail node
* @return the last element
* @throws IllegalStateException when List is empty
*/
public E getTail() //update to throw an exception
{
}
- Now, create a file called ListTest.java. Add the below test code to this class.
/**
* ListTest.java
* @author
* @author
* CIS 36B
*/
public class ListTest {
public static void main(String[] args) {
List<String> l = new List<String>();
System.out.println("Should print a blank line: " + l);
try {
l.getHead();
} catch(IllegalStateException e) {
System.out.println(e);
}
try {
l.getTail();
} catch(IllegalStateException e) {
System.out.println(e);
}
l.insertHead("A");
System.out.println("A at head: " + l.getHead());
System.out.println("A at tail: " + l.getTail() + "\n");
l.insertHead("B");
System.out.println("B at head: " + l.getHead());
System.out.println("A at tail: " + l.getTail() + "\n");
l.insertHead("C");
System.out.println("C at head: " + l.getHead());
System.out.println("A at tail: " + l.getTail() + "\n");
l.insertTail("D");
System.out.println("C at head: " + l.getHead());
System.out.println("D at tail: " + l.getTail() + "\n");
l.insertTail("E");
System.out.println("C at head: " + l.getHead());
System.out.println("E at tail: " + l.getTail() + "\n");
System.out.println("Should print C B A D E: " + l);
}
}
- Run the test code to verify that your methods are working properly.
- Then, upload List.java to Canvas. Both partners need to submit to receive credit.
Wrap up
- Answer the practice exam questions on Canvas from today's lesson
Upcoming Assignments
- Activity 19.1 due Tuesday at 11:59pm
- Lesson 18-19 Practice Exam Questions due Tuesday at 11:59pm
- Quiz 7 due Friday at 11:59pm
- Peer Reviews of Lesson 18-19 Practice Exam Questions due Saturday at 11:59pm
- Course project and presentation due by Friday at 11:59pm
- Final Exam next Monday at 9:15am
~Have a Great Day!~
|
|