StudentSort Modify the Student and studentList classes to do the following: sort
ID: 3761624 • Letter: S
Question
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
import java.util.*;
import java.lang.*;
import java.io.*;
//Students.java
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;
}
}
//studentList.java
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));
int ch;
System.out.println("")
int counter;
boolean continueInput;
int contCounter;
do {
do {
System.out.println("Please enter what you want to 1. Sort by LastName 2. Sort by GPA ");
System.out.println(" Enter your choice (1/2) : ");
ch = scan.nextline(System.in);
switch (ch) {
case 1:
//Sorting the list by LastName
Collections.sort(list, new Comparator() {
public int compare(Object o1, Object o2) {
String fn1 = ((Person) o1).getfirstName();
String fn2 = ((Person) o2).getfirstName();
int res = fn1.compareTo(fn2);
if (res != 0) {
return res;
} else {
String ln1 = ((Person) o1).getlastName();
String ln2 = ((Person) o2).getlastName();
return ln1.compareTo(ln2);
}
}
});
counter = 0;
break;
case 2:
//Sorting the list by GPA
Collections.sort(list, new Comparator() {
public int compare(Object o1, Object o2) {
String fn1 = ((Person) o1).getgpa();
String fn2 = ((Person) o2).getgpa();
int res = fn1.compareTo(fn2);
if (res != 0) {
return res;
} else {
String ln1 = ((Person) o1).getgpa();
String ln2 = ((Person) o2).getgpa();
return ln1.compareTo(ln2);
}
}
});
counter = 0;
break;
default:
System.out.println("Improper input, please enter only ADD, REMOVE, LIST, or SAVE.");
counter = 1;
}
} while (counter == 1);
do {
System.out.print(" Do you want to continue? (y/n): ");
switch (stdin.next().toLowerCase()) {
case "y":
contCounter = 0;
continueInput = true;
break;
case "n":
contCounter = 0;
continueInput = false;
break;
default:
contCounter = 1;
continueInput = false;
System.out.print(" Please only enter 'y' or 'n'.");
}
} while (contCounter == 1);
} while (continueInput);
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));
}
}
}