Question: PLEASE HELP ON JAVA: Using the input file called StudentInputFile.txt, input these students into a linked list sorted according to their age. In each case

PLEASE HELP ON JAVA:

Using the input file called StudentInputFile.txt, input these students into a linked list sorted according to their age. In each case I made the student's last name their age as a string so when the input file says "John" "Twenty" 20 this means the student's first name is John, last name is Twenty, and age is 20. This is so that when you output the list you can easily tell if it is sorted by looking at the last names.

I have given you a class called Student. You will have to add a compareTo method so this class can implement comparable. Do NOT add a getAge() method to the class. In main I want you to be able to compare two Student objects.

Do not use an array or any other data structure other than the list. Do not add them to the list and then sort the list. I want you to add each node to the list in the "correct" place in order to keep the list sorted as you are building it. Then output the contents of the list.

StudentInputFile.txt

Marty Twenty 20

Elizabeth Fifty 50

Christy Nineteen 19

Graham TwentyFive 25

Doris FiftyOne 51

Eleanor ThirtyFive 35

Mark Nineteen 19

Margaret Forty 40

Jessica ThirtyOne 31

THIS IS WHAT I HAVE SO FAR:

Student Class:

public class Student implements Comparable { private String name; private int age; public Student(String n, int a){ name = n; age = a; } public String toString(){ return "Name: "+name + "Age "+ age; } public int compareTo(Student b){ if(ageb.age){ return 1; } else return 0; } }

NODE CLASS:

public class Node { Object item; Node next; Node(Object newItem){ item = newItem; next=null; } Node(Object newItem, Node nextNode){ item = newItem; next = nextNode; } }

MAIN:

public static void main(String[] args) { Node head = null; File inFile = new File("StudentInputFile.txt"); Scanner Input = null; try{ Input = new Scanner(inFile); } catch(FileNotFoundException e){ System.out.println(e.getMessage()); } while(Input.hasNext()){ // SAYS DEFERENCING NULL POINTER Student a = new Student(Input.next() + " "+ Input.next(),Input.nextInt()); head=addList(head, new Node(a)); } Input.close(); PrintList(head); } public static void PrintList(Node head){ Node curr = head; while(curr!= head){ System.out.println(curr.item); curr=curr.next; } } public static Node addList(Node head, Node temp){ if(head==null){ head = temp; } else{ Node prev = null; Node current = null; while(current!= null){ if(((Student)temp.item).compareTo((Student)current.item)<0) break; prev=current; current=current.next; } temp.next = current; if(current== head) head=temp; else prev.next = temp;// SAYS DEFERNCING NULL POINTER } return head; } }

RUBRIC:

compareTo method in Student

correctly input from file, use of try/catch

method to add to list in sorted order. If no method, just statements in main.

printList method

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!