Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.File;
- import java.io.FileNotFoundException;
- import java.util.ArrayList;
- import java.util.Scanner;
- /**
- * This program performs the following functions:
- * - Will load student records from input.txt into their respective variables
- * - Will turn those variables into a Student Object
- * - Calculate their average % based on test scores
- * - Places the Student objects into an ArrayList representing the academic class.
- * - Lists the class with their % score and letter grade in the class.
- * - Remove the records matching ID 42P4 and 45A3
- * - Re-list the records with the removed students.
- * - Add the Student Records from Additions.txt
- * - Calculate the Year Rank based on hours completed.
- * - Calculate the GPA based on Current GPA and the current class hours.
- * - Adding, and sorting students is done using Generic Methods.
- * - Calculate the grades of the students and sort the ArrayList from Highest to lowest based on % score
- * - To prove the generic nature of the program, a list of doubles is brought in and sorted from smallest to largest
- * - Print the ArrayList.
- *
- * @author Carlos
- * @since 01/25/2019
- **/
- public class RosterManipulations {
- public static void main(String[] args) throws Exception {
- RosterManipulations me = new RosterManipulations();
- ArrayList<Student> AcademicClass = new ArrayList<Student>();
- //Create an arraylist of doubles to contain doubles.txt
- ArrayList<Double> myDoubles = new ArrayList<Double>();
- //Making a new instance of the StudentClassManager for Students
- StudentClassManager <Student> myStudents= me.new StudentClassManager<Student>();
- //Making a new instance of the StudentClassManager for Doubles
- StudentClassManager<Double>genericDoubles= me.new StudentClassManager<Double>();
- try {
- Scanner input = new Scanner(new File("src/main/input.txt"));
- //Gather the information from the records in input.txt and set them equal to their respective variables
- while (input.hasNextLine()) {
- String StudentID = input.next();
- String Studentname = input.next();
- int test1 = input.nextInt();
- int test2 = input.nextInt();
- int test3 = input.nextInt();
- int totalHours = input.nextInt();
- float oldGPA = input.nextFloat();
- //Place those variables into a student object
- Student student1 = me.new Student(StudentID, Studentname, test1, test2, test3, totalHours, oldGPA);
- //Place the student object into an array list
- myStudents.AddStudent(AcademicClass, student1);
- }
- System.out.println("\nOriginal Class Roster Before Modifications: \n");
- //Print the contents of the ArrayList including their % score and grade before any manipulation
- for (int i = 0; i < AcademicClass.size(); i++) {
- System.out.println(AcademicClass.get(i));
- }
- me.DeleteStudent(AcademicClass, "45A3");
- me.DeleteStudent(AcademicClass, "42P4");
- System.out.println("\nStudents 45A3 & 42P4 Have Dropped The Class. Generating New Roster: \n");
- //Print the contents of the ArrayList including their % score and grade AFTER the students have dropped
- for (int i = 0; i < AcademicClass.size(); i++) {
- System.out.println(AcademicClass.get(i));
- }
- System.out.println("\n New Students Have Joined The Class. Generating Roster: \n");
- Scanner secondInput = new Scanner(new File("src/main/Additions.txt"));
- while (secondInput.hasNextLine()) {
- String AddedID = secondInput.next();
- String AddedName = secondInput.next();
- int AddedTest1 = secondInput.nextInt();
- int AddedTest2 = secondInput.nextInt();
- int AddedTest3 = secondInput.nextInt();
- int AddedTotalHours = secondInput.nextInt();
- float AddedoldGPA = secondInput.nextFloat();
- Student student2 = me.new Student(AddedID, AddedName, AddedTest1, AddedTest2, AddedTest3, AddedTotalHours, AddedoldGPA);
- //Adding students the expected students to the Class
- myStudents.AddStudent(AcademicClass, student2);
- }
- //Reprint the array with the students now added
- for (int i = 0; i < AcademicClass.size(); i++) {
- System.out.println(AcademicClass.get(i));
- }
- myStudents.SortLarge(AcademicClass);
- System.out.println("\nRegenerating Roster Sorted By Average Grade\n");
- //print the contents of the array sorted by Larger to Smaller based on average %
- for (int i = 0; i < AcademicClass.size(); i++) {
- System.out.println(AcademicClass.get(i));
- }
- //Read the contents of doubles.txt
- Scanner inputDoubles = new Scanner(new File("src/main/doubles.txt"));
- //Add the contents of doubles.txt to the ArrayList for doubles
- while (inputDoubles.hasNextLine()){
- genericDoubles.AddStudent(myDoubles,inputDoubles.nextDouble());
- }
- //Sort the contents of doubles.txt
- genericDoubles.SortLarge(myDoubles);
- //Print the sorted contents of doubles.txt from Smaller to Larger
- System.out.println("Proving Generic Sort By Printing Sorted Doubles From Smallest to Largest\n");
- //Print the sorted contents of doubles.txt from Smaller to Larger
- for (int i=myDoubles.size()-1; i >= 0; i--){
- System.out.println(myDoubles.get(i));
- }
- } catch (FileNotFoundException e) {
- System.err.println("File Input.txt was not found");
- }
- }
- //Deletes a Student who has dropped the class from the array list.
- public static void DeleteStudent(ArrayList<Student> AcademicClass, String ID) {
- for (int i = 0; i < AcademicClass.size(); i++) {
- if (AcademicClass.get(i).getStudentID().equals(ID)) {
- AcademicClass.remove(i);
- }
- }
- }
- //StudentClassManager class will use generics to perform the same actions regardless of the input type.
- public class StudentClassManager <T extends Comparable<T>> {
- //Generically adds an object (in this case students) to the Academic Class ArrayList
- public void AddStudent(ArrayList<T> AcademicClass, T Student) {
- AcademicClass.add(Student);
- }
- //Generically sorts the object from Larger to Smaller
- public void SortLarge(ArrayList<T> AcademicClass) {
- T xsave;
- int isw = 1;
- while (isw == 1) {
- isw = 0;
- for (int i = 0; i < AcademicClass.size() - 1; i++) {
- switch (AcademicClass.get(i).compareTo(AcademicClass.get(i + 1))) {
- case 1://the objects in array x are in the right order
- break;
- case -1://objects out of order, they must be changed.
- // This is where to the place holder is used to swap values
- xsave = AcademicClass.get(i);
- AcademicClass.set(i, AcademicClass.get(i + 1));
- AcademicClass.set(i + 1, xsave);
- isw = 1;
- break;
- default://objects are equal no change
- }
- }
- }
- }
- }
- //The Student class turns student records into objects ao that they an be sorted into an ArrayLIst
- public class Student implements Comparable<Student> {
- String StudentID;
- String Studentname;
- int test1;
- int test2;
- int test3;
- int average;
- int totalHours;
- float oldGPA;
- @Override
- //toString so that objects can be printed.
- public String toString() {
- return " ID: " + StudentID + " Name: " + Studentname + "\n Test 1 Score: " + test1 + "\n Test 2 Score: " + test2 + "\n Test 3 Score: " + test3 + "\n Test Average: " + average + "%" + "\n Class Grade: " + averageGrade(average) + "\n Total Hours: " + totalHours + "\n GPA: " + calculatenewGPA(oldGPA, gradePoint(average), totalHours) + "\n Year Rank: " + yearRank(totalHours) + "\n";
- }
- //Calculates the NEW GPA of a student
- public float calculatenewGPA(float oldGPA, int gradePoint, int totalHours) {
- return ((oldGPA * totalHours) + (2) * gradePoint) / (totalHours + 2);
- }
- //Calculate the GradePoint Value of A GPA
- public int gradePoint(int average) {
- {
- int gradePoint = 0;
- if (average >= 90) {
- gradePoint = 4;
- } else if (average >= 80) {
- gradePoint = 3;
- } else if (average >= 70) {
- gradePoint = 2;
- } else if (average >= 60) {
- gradePoint = 1;
- } else if (average >= 0) {
- gradePoint = 0;
- }
- return gradePoint;
- }
- }
- //Calculate the Year Rank of each student
- public String yearRank(int totalHours) {
- String rank = null;
- if (totalHours <= 30) {
- rank = "Freshman";
- } else if (totalHours <= 60) {
- rank = "Sophomore";
- } else if (totalHours <= 90) {
- rank = "Junior";
- } else if (totalHours >= 91) {
- rank = "Senior";
- }
- return rank;
- }
- //Turn the average into a letter grade
- public String averageGrade(int average) {
- String averageLetter = null;
- if (average >= 90) {
- averageLetter = "A";
- } else if (average >= 80) {
- averageLetter = "B";
- } else if (average >= 70) {
- averageLetter = "C";
- } else if (average >= 60) {
- averageLetter = "D";
- } else if (average >= 0) {
- averageLetter = "F";
- }
- return averageLetter;
- }
- //Calculate the average grade of a student
- //Constructor for Student Class
- public Student(String StudentID, String Studentname, int test1, int test2, int test3, int totalHours, float oldGPA) {
- setStudentID(StudentID);
- setStudentname(Studentname);
- setTest1(test1);
- setTest2(test2);
- setTest3(test3);
- int average = (test1 + test2 + test3) / 3;
- setAverage(average);
- setTotalHours(totalHours);
- setoldGPA(oldGPA);
- }
- //Setters for the Student Class
- public void setStudentID(String StudentID) {
- this.StudentID = StudentID;
- }
- public void setStudentname(String Studentname) {
- this.Studentname = Studentname;
- }
- public void setTest1(int test1) {
- this.test1 = test1;
- }
- public void setTest2(int test2) {
- this.test2 = test2;
- }
- public void setTest3(int test3) {
- this.test3 = test3;
- }
- public void setAverage(int average) {
- this.average = average;
- }
- public void setTotalHours(int totalHours) {
- this.totalHours = totalHours;
- }
- public void setoldGPA(float oldGPA) {
- this.oldGPA = oldGPA;
- }
- //Getters for the Student Class
- public String getStudentID() {
- return StudentID;
- }
- public String getStudentname() {
- return Studentname;
- }
- public int getTest1() {
- return test1;
- }
- public int getTest2() {
- return test2;
- }
- public int getTest3() {
- return test3;
- }
- public int getAverage() {
- return average;
- }
- public int getTotalHours() {
- return totalHours;
- }
- public float getoldGPA() {
- return oldGPA;
- }
- public int compareTo(Student o) {
- return (this.getAverage() < o.getAverage() ? -1 :
- (this.getAverage() == o.getAverage() ? 0 : 1));
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement