Question
Problem Statement: In this project, you are asked to write a Java application similar to the one you wrote in Project 2 that handles a
Problem Statement:
In this project, you are asked to write a Java application similar to the one you wrote in Project 2 that handles a list of students enrolled at a certain college. Your code in project 2 had used the built-in functionalities of the LinkedList class defined in the Java Collection Classes. In this project, you will still use a LinkedList as the main data structure that stores your data but you will provide your own implementation of a class called RecLinkedLink. In doing so, all the required methods to be defined in your RecLinkedList needs to be implemented using recursive algorithms. In other words, method such as size(), replace(), add(), remove, etc. MUST use recursion to achieve it functions. (check chapter 5 for details).
As done before, we will assume that each student is only represented by her/his first name. The names of all students are stored in a given data file called studentData.txt which is posted on Moodle next to this project statement. Your program must do the following task:
Define your own recursive implementation of LinkList in a separate file called RecLinkedList.java.
Create an object of RecLinkedList to be used in storing and handling students data.
Open the given file and read student data into the RecLinkedList object. In doing so,
your code needs to maintain the order of student data in the input file.
Verify the content of the student data (just read) by displaying them on the monitor
using a traditional for loop along with appropriate output messages.
Invoke various methods of the RecLinkedList class to retrieve then display the first
element and the last element of the list. Display appropriate messages on the monitor.
Page 1 of 3
Invoke various methods of the RecLinkedList class to remove the last element then display the size of the list after the removal operation. Display appropriate messages on the monitor.
Display the current list contents using the enhanced for loop along with appropriate output messages.
Create a ListIterator object, iter, to be used in accessing the RecLinkedList contents.
Advance the iterator for five positions then display the current list item (using iterator
method and NOT list method).
Insert a new student called Kelly at the current iterator position then display the
current list contents using the compact form to be used from this point on for any list
content display. Hint, use the implicit toString().
Execute the iter.previous() method followed by using the iter.set() method to set the
current element to Nancy. Then, display the current list contents.
Execute the iter.previous() method followed by the remove() method then display the
current list contents.
13. In a separate file, create a helper class called HelpLists. This class need to have the
code for two methods described below:
public void reverse(RecLinkedList strings). This method receives a
RecLinkedList object as its parameter then linearly access all the elements in the object while pushing them in a STACK object. You need to use the STACK data structure that you just created to retrieve the data from the stack and display them in reserve order on the monitor.
public void FIFO(RecLinkedList strings). This method receives a RecLinkedList object as its parameter then linearly access all the elements in the object while inserting them in a QUEUE object. You need to use the QUEUE data structure that you just created to retrieve the data from the queue and display them in order on the monitor.
In your main class, create an object of type HelpLists class then use it in the following.
Invoke the reverse method to reverse the order then display the reversed list contents.
Invoke the FIFO() method in order to create a queue and use it to ensure that the order
of the data in RecLinkedList will be displayed without change.
Project 2
public class StudentLinkedList {
public static void main(String args[]) throws FileNotFoundException {
/**
* LinkedList to be used in storing and handling students data
*/
LinkedList
/**
* inputFile The name of the scanner to open studentData.text and handle
* it*/
Scanner inputFile = new Scanner(new File("studentData.txt"));
/** Creating a ListIterator object */
ListIterator
/** helper, creates an object of class HelpLists */
HelpLists helper = new HelpLists();
/**
* location, int variable to later be use to identify the location of a
* given variable
*/
int location;
/** loop to add every element in the file to LinkedList students */
while (inputFile.hasNextLine()) {
students.add(inputFile.next());
}
/** closes inputFile */
inputFile.close();
/**
* Traditional for loop to print every element of the file backwards
* with its location
*/
for (int i = students.size() - 1; i >= 0; i--) {
System.out.println("Name of student #" + i + " is " + students.get(i));
}
/** prints the first student in the list */
System.out.println("Last element in the list is: " + students.getLast());
/** prints the last student in the file */
System.out.println("First element in the list is: " + students.getFirst());
/** asks for user input for a name to remove from the list */
System.out.println("Enter a student name to be removed from the list");
/** Creates a new scanner to read user input */
Scanner userInput = new Scanner(System.in);
/** nameToRemove, user input name to remove from the list */
String nameToRemove = userInput.next();
/**
* loop to find if the student name is there to be removed if found
* removes the name and prints the name that was removed else prints an
* error message
*/
if (students.remove(nameToRemove))
System.out.println("Name " + nameToRemove + " was found in the list then it was removed"
+ " The size of the list after the removal is: " + students.size());
else {
System.out.println("The element was not found");
}
/** closes scanner used to open user input */
userInput.close();
/** prints contents of LinkedList students using an enhanced for loop */
System.out.println(" Displaying list contents using enhanced for loop");
for (String names : students) {
System.out.println(names);
}
/** Creates a iterator Reference to linkedList students */
iterator = students.listIterator();
/** loop to iterate 3 times though the list */
int itrNum = 0;
while (itrNum < 3) {
iterator.next();
itrNum++;
}
/** prints the element healed by the iterator */
System.out.println("The current student after iterating three times is: " + iterator.previous());
/**
* adds Edward to the list and displays the content of the list after
* adding the name
*/
iterator.next();
iterator.add("Edward");
System.out.println("Displaying list Contents after adding 'Edward' :");
System.out.println(Arrays.toString(students.toArray()));
/**
* sets the current iterator to George and displays the content of the
* list
*/
iterator.previous();
iterator.set("George");
System.out.println("Displaying List Contents after setting the current element to George: ");
System.out.println(Arrays.toString(students.toArray()));
/** Displays list after previous then remove */
iterator.previous();
iterator.remove();
System.out.println("Displaying list after previous then remove: ");
System.out.println(Arrays.toString(students.toArray()));
/** Displays List Contents after rotating by 3 positions */
helper.rotateRight(students, 3);
System.out.println("Displaying List Contents after rotate by 3 positions:");
System.out.println(Arrays.toString(students.toArray()));
/** Displays List Contents after rotate by 6 positions */
helper.rotateRight(students, 6);
System.out.println("Displaying List Contents after rotate by 6 positions: ");
System.out.println(Arrays.toString(students.toArray()));
/**
* sets int variable location to the position of the first occurrence of
* the name Robert
*/
location = helper.LocateItem(students, "Robert");
/**
* prints error if location was not found, but if found, displays the
* name of the student searched for as well as the location it was found
*/
if (location == -1) {
System.out.println("Item not found");
} else {
System.out.println("Student named Robert was found and its last occurance is at location" + location);
System.out.println(Arrays.toString(students.toArray()));
}
}
}
public class HelpLists{
/**
* method to rotate a given list a given number of times
* @param studentNames the Linked List to be rotated
* @param n number of times to rotate the list
*/
public void rotateRight(LinkedList
{
for(int i = 0; i String t = studentNames.getLast(); studentNames.removeLast(); studentNames.addFirst(t); } } /** a method to find the location of the first occurrence of a given name * @param studentNames the linkedList to be searched * @param name the name of the student to be search for * @return */ public int LocateItem(LinkedList { int i=0; while(i String x = studentNames.get(i); if(x.equals(name)){ return i; } i++; } return -1; } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started