In: Computer Science
Give an input file of student records in the following format:
<name>#<SID>#<major>#<GPA>
Smith Eugene#A9S65A#Chemistry#2.50
Brown Ezra#T56G0M#Business Administration#1.36
Brown Ferman#PI56T4#Accounting#2.68
Chavis Ertle#7HS56#Chemistry#3.00
Strickland DiChildersa#TWH54F#Business Administration#2.00
Stone Clark#BN78N3W#English#4.00
Childers Evelyn#U8M8N2#Criminal Justice#2.73
Stone Desmond#JHD9K2#Computer Science#3.23
Write a Java application to process the input file of student information. Design your solution using classes for student, student Database, and a driver class. Your application should output the average GPA and display the student list in the descending order of their GPA.
This Java application was done using Eclipse IDE Version: 2020-09 (4.17.0)
To create a new Java Application:
1. Open Eclipse IDE
2. Select a directory as workspace -> Click “Launch”
3. Click “File -> New -> Java Project”
4. In “New Java Project” dialog box, Enter the Project name as “StudentDatabaseProject” -> Click “Finish”
5. In “New module-info.java” dialog box, Click “Don’t Create”.
A new Java Project “StudentDatabaseProject” will be created
6. Right-click on “src” -> New -> Package
7. In “New Java Package” dialog box, Enter Name as Project -> Click Finish
8. Right-click on “Project” -> New -> Class
9. In “New Java Class” dialog box, Enter Name as “Driver” -> Click Finish
10. Right-click on “Project” -> New -> Class
11. In “New Java Class” dialog box, Enter Name as “Student” -> Click Finish
12. Right-click on “Project” -> New -> Class
13. In “New Java Class” dialog box, Enter Name as “StudentDatabase” -> Click Finish
Add the following code:
Driver.java
package Project;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.List;
import java.util.Scanner;
// The program starts here
public class Driver {
public static void main(String[] args) throws FileNotFoundException {
// Add the path of your .txt file here
File file = new File("D:\\StudentDB.txt");
Scanner sc = new Scanner(file);
// A StudentDatabase object is created
StudentDatabase sd1 = new StudentDatabase();
// Reads each line in the .txt file
while (sc.hasNextLine()){
String inputEachLine = sc.nextLine(); // Holds each line
// Each line is split with '#' and the value is stored in studInput of type String[]
String[] studInput = inputEachLine.split("#");
if(!studInput[3].equals("<GPA>")) { //This ignores the first line
// Student object is created for each value
Student s1 = new Student(studInput[0], studInput[1], studInput[2], Double.parseDouble(studInput[3]));
// Each Student Object is added to StudentDatabase
sd1.addStudObject(s1);
}
}
// Calls the averageGPA method in StudentDatabase to get the average of all the student's GPA
System.out.println("The average GPA of students is " + sd1.averageGPA() + "\n");
// Gets the student's details of type Student object in a list by calling the method sortDescending in StudentDatabase
List<Student> DescendingList= sd1.sortDescending();
// Each student detail is printed in descending order of their GPA
System.out.println("Student list is diplayed in descending order.");
for(int i = 0; i < DescendingList.size(); i++) {
System.out.println("Student {" + "name = " + DescendingList.get(i).getName() +
", SID = " + DescendingList.get(i).getSID() +
", major = " + DescendingList.get(i).getMajor() +
", GPA = " + DescendingList.get(i).getGPA() + '}');
}
}
}
Student.java
package Project;
// Student class
public class Student {
// Instance Private Variables name, SID, major and GPA
private String name;
private String SID;
private String major;
private double GPA;
// Constructor Declaration of Student Class with parameters String name, String SID, String major, double GPA
public Student(String name, String SID, String major, double GPA)
{
this.name = name;
this.SID = SID;
this.major = major;
this.GPA = GPA;
}
// Getter and Setter methods of the variables
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSID() {
return SID;
}
public void setSID(String SID) {
this.SID = SID;
}
public String getMajor() {
return major;
}
public void setMajor(String major) {
this.major = major;
}
public double getGPA() {
return GPA;
}
public void setGPA(double GPA) {
this.GPA = GPA;
}
}
StudentDatabase.java
package Project;
import java.util.ArrayList;
import java.util.List;
// StudentDatabase Class
public class StudentDatabase {
// Instance Private Variables studObject
private List<Student> studObject;
// Constructor Declaration of StudentDatabase Class
public StudentDatabase() {
this.studObject = new ArrayList<>();
}
// addStudObject with paramter studObj of type Student adds each object to the studObject instance variable
public void addStudObject(Student studObj){
this.studObject.add(studObj);
}
// averageGPA() finds the average of all the student's GPA in the studObject List
public double averageGPA(){
double sum = 0.0;
for(int i = 0; i < this.studObject.size(); i++){
sum += this.studObject.get(i).getGPA();
}
return sum / this.studObject.size();
}
// sortDescending() sorts the values in studObject List by descending order of their GPA
public List<Student> sortDescending(){
for(int i = 0 ; i < this.studObject.size(); i++)
{
for(int j = i+1 ; j< this.studObject.size();j++)
{
if(this.studObject.get(i).getGPA() < this.studObject.get(j).getGPA())
{
Student temp = this.studObject.get(i);
this.studObject.set(i, this.studObject.get(j));
this.studObject.set(j, temp);
}
}
}
return this.studObject;
}
}
OUTPUT: