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

Lab 12: StudentSort Modify the Student and studentList classes to do the followi

ID: 3761058 • Letter: L

Question

Lab 12:
StudentSort
Modify the Student and studentList classes to do the following:
sort the list by lastName and print the list;
sort the list by gpa and print the list;

//*******************************************************
// Programmer: Anders Lenardson
// Program: Student.java
//*******************************************************
public class Students
{
   // instance variables
   String firstName;
   String lastName;
   String major;
   Double gpa;
  
   // Constructor
   public Students (String first, String last, String strMajor, Double strGPA)
   {
       firstName = first;
       lastName = last;
       major = strMajor;
       gpa = strGPA;
   }
  
   // methods
   public String toString()
   {
       String output = String.format("%-15s %-15s %-15s %3.2f", firstName, lastName, major, gpa);
       return output;
   }
}

//Programmer: Anders Lenardson
//Program: Student List

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;

public class studentList
{
   public static void main(String[] args) throws IOException
   {
       String fileName;
       Scanner fileScan, lineScan;
       String line;
       String firstName, lastName, major, strGPA;
       double gpa;
       Students newStudent;
      
       Scanner scan = new Scanner(System.in);
       ArrayList<Students> studentarray = new ArrayList<Students>();
      
       System.out.println("Enter file name: ");
       fileName = scan.nextLine();
       fileScan = new Scanner (new File(fileName));
      
       while (fileScan.hasNext())
       {
           lastName = " ";
           firstName = " ";
           major = " ";
           gpa = 0;
           line = fileScan.nextLine();
           lineScan = new Scanner(line);
           lineScan.useDelimiter(",");
           if (lineScan.hasNext())
               lastName = lineScan.next();
           else continue;
           if (lineScan.hasNext())
               firstName = lineScan.next();
           else continue;
           if (lineScan.hasNext())
               major = lineScan.next();
           else continue;
           if (lineScan.hasNext())
           {
               strGPA = lineScan.next();
               gpa = Double.parseDouble(strGPA);
           }
           else continue;
          
           newStudent = new Students(lastName, firstName, major, gpa);
           //System.out.println(newStudent);
           studentarray.add(newStudent);
       }
      
       for (int i = 0; i < studentarray.size(); i++)
       {
           System.out.println(studentarray.get(i));
       }
   }
}

Explanation / Answer

public class Student
{
protected String lastName;
protected String firstName;

Student()
{
lastName =new String("unknown");
firstName =new String("unknown");
} //end of constructor

Student(String f, String l)
{
this.lastName = l;
this.lastName = f;
}//end of overloaded constructor



public boolean equals(Student o)
{
return this.firstName.equals(o.firstName) && this.lastName.equals(o.lastName);
}//end of equals


public int compareTo(Student n)
{
int lastCmp = this.lastName.compareTo(n.lastName);
return (lastCmp == 0 ? firstName.compareTo(n.firstName) : lastCmp);
}//end of compareTo

public String toString()
{
return ("Firstname-> "+firstName+" LastName-> "+lastName);
}//end of toString method


}// end of Student

ReadAndSortData.java Program --------->

import java.io.*;
import java.util.*;
import java.util.Scanner;

public class ReadDataAndSort
{

public static void main(String [] args)
{
System.out.println("Welcome to name sorter");
String first = new String();
String last = new String();
//create ArrayList object or Integers
ArrayList<Student> arrList = new ArrayList<Student>();
int count = 0;
java.io.File sourceFile = new java.io.File("students.txt");


String temp = new String();
try
{
Scanner input = new Scanner(sourceFile);

while (input.hasNext())
{
first = input.next();
last = input.next();

System.out.print("DEBUG statement first-> "+first); //used for debugging
System.out.println(" last -> "+last+" $$"); //used for debugging

Student Student(last,first);
arrList.add(oneStudent);
}//end of while loop to read all the data from the disk


for (Student E : arrList)
System.out.println(E); //verify all the input data was read in from disk



input.close();//done reading all the names from disk


}//end of try block


catch (FileNotFoundException xx)
{
System.out.println("Error ---> Data file does not exist");
}
catch (NumberFormatException xxx)
{
System.out.println("Error ---> Can NOT parse string data to an integer");
System.out.println("String data is in temp->"+temp);
}
catch (Exception error)
{
System.out.println("some RUN-TIME error has occured");
System.out.println("Error ---> "+error);
}//end of catch

Iterator<Student> it = arrList.iterator();
System.out.println("-------------------------------------");

Student [] unsortedArray = new Student[arrList.size()];

while (it.hasNext()) //create a loop move an arrayList into an array
{
Student anObj = it.next();//get Object out of ArrayList
unsortedArray[count] = anObj; // work on this line of code......;

count = count + 1;
System.out.println(anObj);//only used for testing
}//end of while loop to empty arrayList named arrList

for (int i = 0 ; i< unsortedArray.length; ++i)
System.out.println(unsortedArray[i]);//debug

bubbleSort(unsortedArray, unsortedArray.length);

/* public void writeFileToDisk(int x)
{
file = ("students2.txt");
try
{
File outFile = new File(file);
PrintWriter output = new PrintWriter(outFile);
output.println(E + " ");
output.close();
}
catch (Exception error)
{
}//End Of Error Catching
}//End Of Write */



}//end of main method

private static void bubbleSort (unsortedArray,int length)
{
int temp;
int counter;
int index;

for (counter = 0; counter<length - 1; counter ++)
{
for(index = 0; index<length - 1 - counter; index ++)
{
if(unsortedArray[index] .compareTo(unsortedArray[index + 1]) > 0)
{
temp = unsortedArray[index];
unsortedArray[index] = unsortedArray[index + 1];
unsortedArray[index + 1] = temp;
}//End Of If Loop For Swap
}//End Of Inner Loop
}//End Of Outer Loop
}
}//end of class