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: 3745080 • 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

Explanation / Answer

@Answer:

import java.io.BufferedReader;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
//Create a student class
public class Student
{
//Main method definition
public static void main(String[] args) throws Exception
{
//Creates a list
List<StudentDemo> studentArray = new ArrayList<StudentDemo>();
//Local variables to store first name, last name and age
String lastName;
String firstName;
int age = 0;
//To store a line of data read from file
String line;
//Open the studentInputFile.txt file for reading
FileReader reader = new FileReader("studentInputFile.txt");
BufferedReader bufferedReader = new BufferedReader(reader);
//To store the splited data by space
String[] split;
//Loops till end of line
while ((line = bufferedReader.readLine()) != null)
{
//Split the data with space
split = line.split(" ");
//Stores the first namem, last name and age
firstName = split[0];
lastName = split[1];
//Converts the age to integer
age = Integer.valueOf(split[2]);
//Calls the constructor to create object and add the data to array list studentArray
studentArray.add(new StudentDemo(firstName, lastName, age));
}//End of while
//Calls the sort method to sort the student data based on age
Collections.sort(studentArray);
System.out.println();
//Displays the sorted order student information
//Loops till end of the array list
for (StudentDemo aStudent : studentArray)
{
System.out.println(aStudent);
}//End of for loop
}//End of main method
//Class implements interface Comparable interface
static class StudentDemo implements Comparable<StudentDemo>
{
//Instance variables to store first name, last name and age
private String firstName;
private String lastName;
private int age;
//Parameterized Constructor
public StudentDemo(String firstName, String lastName, int age)
{
//Assigns data to instance variables
this.firstName = firstName;
this.age = age;
this.lastName = lastName;
}//End of constructor
  
//Method to return age
public int getAge()
{
return age;
}
//Method to return first name
public String getFirstName()
{
return firstName;
}
//Method to return last name
public String getLastName()
{
return lastName;
}

@Override
//Overrides the compareTo method of Comparable interface
public int compareTo(StudentDemo compares)
{
int compareage = ((StudentDemo)compares).getAge();
/* For Ascending order*/
return this.age - compareage;
}//End of method
//Overrides toString() method to display Student information
public String toString()
{
return "First Name: " + firstName + " Lsst Name: " + lastName + " Age: " + age;
}//End of method
}//End of class StudentDemo
}//End of class Student