Question: In this assignment, you will modify the SinglyLinkedList implementation to use Generics. Create a new project: Assignment2 Create three new classes and copy/paste the attached

In this assignment, you will modify the SinglyLinkedList implementation to use Generics.

Create a new project: Assignment2

Create three new classes and copy/paste the attached source code for each:

SinglyLinkedList

Student

Course

Note: you will have syntax errors in the SinglyLinkedList class.

Modify the SinglyLinkedList class to use Generics. All the changes for this assignment will be to just his one class. You do not need to make any modifications to the other two classes. Also, no not modify any code in the main() method.

Upload just the SinglyLinkedList.java file to Cabrini Learn.

There is code in the textbook as well in as in the slides covering Doubly Linked Lists to help in completing this assignment.

You assignment is correct when you run it and get the following output:

The list is empty

[John Doe, 10012]

The list contains: ([John Doe, 10012], [Jane Smith, 10022])

The list contains: ([Joe Doakes, 10029], [John Doe, 10012], [Jane Smith, 10022])

The list is empty

[IST 285, Data Structures, 90092]

The list contains: ([IST 300, Networking, 90092], [IST 285, Data Structures, 90092])

The list contains: ([IST 380, Systems Analysis, 90099], [IST 300, Networking, 90092], [IST 285, Data Structures, 90092])

SinglyLinkedList.java source code

public class SinglyLinkedList { // ---------------- nested Node class ---------------- /** * Node of a singly linked list, which stores a reference to its element and * to the subsequent node in the list (or null if this is the last node). */ private static class Node { /** The element stored at this node */ private Student element; // reference to the element stored at this node /** A reference to the subsequent node in the list */ private Node next; // reference to the subsequent node in the list /** * Creates a node with the given element and next node. * * @param e * the element to be stored * @param n * reference to a node that should follow the new node */ public Node(Student e, Node n) { element = e; next = n; } // Accessor methods /** * Returns the element stored at the node. * * @return the element stored at the node */ public Student getElement() { return element; } /** * Returns the node that follows this one (or null if no such node). * * @return the following node */ public Node getNext() { return next; } // Modifier methods /** * Sets the node's next reference to point to Node n. * * @param n * the node that should follow this one */ public void setNext(Node n) { next = n; } } // ----------- end of nested Node class ----------- // instance variables of the SinglyLinkedList /** The head node of the list */ private Node head = null; // head node of the list (or null if empty) /** The last node of the list */ private Node tail = null; // last node of the list (or null if empty) /** Number of nodes in the list */ private int size = 0; // number of nodes in the list /** Constructs an initially empty list. */ public SinglyLinkedList() { } // constructs an initially empty list // access methods /** * Returns the number of elements in the linked list. * * @return number of elements in the linked list */ public int size() { return size; } /** * Tests whether the linked list is empty. * * @return true if the linked list is empty, false otherwise */ public boolean isEmpty() { return size == 0; } /** * Returns (but does not remove) the first element of the list * * @return element at the front of the list (or null if empty) */ public Student first() { // returns (but does not remove) the first element if (isEmpty()) return null; return head.getElement(); } /** * Returns (but does not remove) the last element of the list. * * @return element at the end of the list (or null if empty) */ public Student last() { // returns (but does not remove) the last element if (isEmpty()) return null; return tail.getElement(); } // update methods /** * Adds an element to the front of the list. * * @param e * the new element to add */ public void addFirst(Student e) { // adds element e to the front of the list head = new Node(e, head); // create and link a new node if (size == 0) tail = head; // special case: new node becomes tail also size++; } /** * Adds an element to the end of the list. * * @param e * the new element to add */ public void addLast(Student e) { // adds element e to the end of the list Node newest = new Node(e, null); // node will eventually be the tail if (isEmpty()) head = newest; // special case: previously empty list else tail.setNext(newest); // new node after existing tail tail = newest; // new node becomes the tail size++; } /** * Removes and returns the first element of the list. * * @return the removed element (or null if empty) */ public Student removeFirst() { // removes and returns the first element if (isEmpty()) return null; // nothing to remove Student answer = head.getElement(); head = head.getNext(); // will become null if list had only one node size--; if (size == 0) tail = null; // special case as list is now empty return answer; } /** * Produces a string representation of the contents of the list. This exists * for debugging purposes only. */ public String toString() { StringBuilder sb = new StringBuilder("("); Node walk = head; while (walk != null) { sb.append(walk.getElement()); if (walk != tail) sb.append(", "); walk = walk.getNext(); } sb.append(")"); return sb.toString(); } /* * WARNING: * DO NOT modify any code below this line !! * */ public static void main(String[] args) { // create and initialize our SinglyLinkedList SinglyLinkedList sl1 = new SinglyLinkedList<>(); System.out.println(sl1.isEmpty() ? "The list is empty" : sl1.last()); Student mc = new Student("John Doe", 10012); sl1.addLast(mc); System.out.println(sl1.isEmpty() ? "The list is empty" : sl1.last()); mc = new Student("Jane Smith", 10022); sl1.addLast(mc); System.out.println("The list contains: " + sl1); mc = new Student("Joe Doakes", 10029); sl1.addFirst(mc); System.out.println("The list contains: " + sl1); SinglyLinkedList sl2 = new SinglyLinkedList<>(); System.out.println(sl2.isEmpty() ? "The list is empty" : sl2.last()); Course cc = new Course("IST 285", "Data Structures", 90092); sl2.addLast(cc); System.out.println(sl2.isEmpty() ? "The list is empty" : sl2.last()); cc = new Course("IST 300", "Networking", 90092); sl2.addFirst(cc); System.out.println("The list contains: " + sl2); cc = new Course("IST 380", "Systems Analysis", 90099); sl2.addFirst(cc); System.out.println("The list contains: " + sl2); } } 

Student.java source code

public class Student { private String name; private int univID; /** * Creates a class. * * @param s * individual's name * @param u * individual's university ID */ Student(String s, int u) { name = new String(s); univID = u; } /** * Produces a string representation of the contents of the class. */ public String toString() { return "[" + name + ", " + univID + "]"; } } 

Course.java source code

public class Course { private String myName; private int myAge; /** * Creates a class. * * @param s * individual's name * @param n * individual's age */ Course(String s, int age) { myName = new String(s); myAge = age; } /** * Produces a string representation of the contents of the class. */ public String toString() { return "[" + myName + ", " + myAge + "]"; } } 

Please make sure everything is correct, thanks in advance!

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!