Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Using the input file called StudentInputFile.txt, input these students into a li

ID: 3745463 • Letter: U

Question

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

What I have so far:

H3.java:

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;


public class H3 {
public static void PrintList(Node head)
{ Node curr=head;
int m;
while(curr !=null)
{
m= (int) curr.item;
System.out.println(m);
curr=curr.next;
//end while
}
}
  
public static Node addList(Node list, Node temp)
{
if(list == null)
list = temp;
else{temp.next = list;
list = temp;}
return list;
}
public static void main(String[] args) {
Node head=null;
Node tail = null;
Node curr = null;
File File1 = new File("StudentInputFile.txt");
Scanner file1Input = null;
try {
file1Input = new Scanner(File1);
}
catch (FileNotFoundException e) {
}
Node temp = new Node(curr);
while(file1Input.hasNextLine())
{
Student hStudent = (Student) head.item;
Student tempStudent = (Student) tail.item;
if (hStudent.compareTo(tempStudent)>0){
temp.next=head;
head = temp;
}
}
PrintList(head);
}

}

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;


public class H3 {
public static void PrintList(Node head)
{ Node curr=head;
int m;
while(curr !=null)
{
m= (int) curr.item;
System.out.println(m);
curr=curr.next;
//end while
}
}
  
public static Node addList(Node list, Node temp)
{
if(list == null)
list = temp;
else{temp.next = list;
list = temp;}
return list;
}
public static void main(String[] args) {
Node head=null;
Node tail = null;
Node curr = null;
File File1 = new File("StudentInputFile.txt");
Scanner file1Input = null;
try {
file1Input = new Scanner(File1);
}
catch (FileNotFoundException e) {
}
Node temp = new Node(curr);
while(file1Input.hasNextLine())
{
Student hStudent = (Student) head.item;
Student tempStudent = (Student) tail.item;
if (hStudent.compareTo(tempStudent)>0){
temp.next=head;
head = temp;
}
}
PrintList(head);
}

}

Node.java:

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

Student.java:

public class Student implements Comparable<Student>{
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 ( age < b.age )
{
return -1;
}
else if ( age > b.age )
{
return 1;
}
else
{
return 0;
}
}
}

Criteria that needs to be met:

compareTo method in Student

a method to add to list in sorted order

a method to printList

No use of arrays only linkedlists with nodes

Explanation / Answer


Given below is the completed code for the question.
To indent code in eclipse , select code by pressing ctrl+a and then indent using ctrl+i
Please do rate the answer if it was helpful. Thank you

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class H3 {
public static void PrintList(Node head) {
Node curr = head;
while (curr != null) {
System.out.println(curr.item);
curr = curr.next;
// end while
}
}

public static Node addList(Node head, Node temp) {
if (head == null)
head = temp;
else {
Node previous = null;
Node current = head;
while(current != null){
if(((Student)temp.item).compareTo((Student)current.item) < 0)
break;
previous = current;
current = current.next;
}

temp.next = current;
if(current == head) //insert as first node?
head = temp;
else
previous.next = temp;
}
return head;
}

public static void main(String[] args) {
Node head = null;
File File1 = new File("StudentInputFile.txt");
Scanner file1Input = null;
try {
file1Input = new Scanner(File1);
} catch (FileNotFoundException e) {
System.out.println(e.getMessage());
}

while (file1Input.hasNext()) {
Student s = new Student(file1Input.next() +" " + file1Input.next(), file1Input.nextInt());
head = addList(head, new Node(s));
}
file1Input.close();
PrintList(head);
}
}

output
-----
Name:Christy Nineteen Age:19
Name:Mark Nineteen Age:19
Name:Marty Twenty Age:20
Name:Graham TwentyFive Age:25
Name:Jessica ThirtyOne Age:31
Name:Eleanor ThirtyFive Age:35
Name:Margaret Forty Age:40
Name:Elizabeth Fifty Age:50
Name:Doris FiftyOne Age:51