Question: Develop Java program for each of the following problems. Please name the programs as indicated and add proper program headers and output labels as shown
Develop Java program for each of the following problems. Please name the programs as indicated and add proper program headers and output labels as shown below.
Use only concepts and programming constructs/syntax we discussed in class.
Programming (30 points)
Write your Java Class named LinkedListYourName as follows.
Choose one of the given files (LinkedListGGGG.java or LinkedListOOOO.java)
- LinkedListGGGG.java is a LinkedList Class with Generic
- LinkedListOOOO.java is a LinkedList Class with Object element type
Change the filename and Class name with yours LinkedListYourName
Implement all class methods.
- add, addFirst, addLast, getFirst, getLast, remove, removeFirst, removeLast
You cannot change the name of methods, parameter type, return type.
Remove all written comments from the code, then write your comments.
Using code from outside sources receives NO credit. You must write and document your own code to meet the assignment requirements. Some methods are not in the slides.
Second, you must create your own test program named ListTestYourName
Your test program will runs with sentinal loop with user input.
| -------Linked List Test Program------ 0 Exit Program 1 Add First Node 2 Add Last Node 3 Add at Index 4 Remove First Node 5 Remove Last Node 6 Remove at Index 7 Print List Size Select Option: user input here |
The main menu will be similar as follows.
Test each method at least one execution for each, then show the status before & after execution with proper label.
Except for options 0 and 7 above, when calling a method on the list make sure the method prints the list content before and after executing the called method, For example, the output of testing method removeLast() would be displayed like this
(assuming the list has 10, 20, 30, 40, 50):
| Testing method removeLastNode() List content before removing last node is: 10 20 30 40 50 List content after removing last node is: 10 20 30 40 |
| List is Empty now. |
User choose remove option but there is no element in the List, you need to show the message like this
Again, you cannot change method names, return type and parameter types.
SEE BELOW THE CODE:
public class LinkedListGGGG
// Because LinkedList and Node use same element type
public void add(int index, E element) { /////////////////////////// // Write your code here! // /////////////////////////// // add an element(as a node) at given index } public void addFirst(E element) { /////////////////////////// // Write your code here! // /////////////////////////// // add an element(as a node) at the first of the list } public void addLast(E element) { /////////////////////////// // Write your code here! // /////////////////////////// // add an element(as a node) at the last of current list } public E getFirst() { /////////////////////////// // Write your code here! // /////////////////////////// // Return the element value of first node } public E getLast() { /////////////////////////// // Write your code here! // /////////////////////////// // Return the element value of first node } public void remove(int index) { /////////////////////////// // Write your code here! // /////////////////////////// // Remove an element(a node) at given index }
public E removeFirst() { /////////////////////////// // Write your code here! // /////////////////////////// // Return the element value of first node // Remove an element(a node) at given index } public E removeLast() { /////////////////////////// // Write your code here! // /////////////////////////// // Return the element value of first node // Remove an element(a node) at given index } public int size() { /////////////////////////// // Write your code here! // /////////////////////////// // Return how many elements in current list // Don't keep the size value, when size() method called, // retrieve whole list and count the number of element and return it } public String toString() { Node temp = head; String str = "["; while(temp.next != null) { str = str + temp.data + ", "; temp = temp.next; } str = str + temp.data; return str + "]"; } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
