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.Arrays;
- import java.util.Collections;
- 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 Record: 67T4 ClouseB 80 75 98
- * - Add the Student Record: 45P5 GarrisonJ 75 78 72
- * - Add the Student Record: 89P0 SingerA 85 95 99
- * - Calculate the grades of the students and sort the ArrayList from Highest to lowest based on % score
- * - 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>();
- 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
- me.AddStudent(AcademicClass,StudentID,Studentname,test1,test2,test3, totalHours, oldGPA);
- }
- 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));
- }
- DeleteStudent(AcademicClass,"45A3");
- 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
- me.AddStudent(AcademicClass,AddedID,AddedName,AddedTest1,AddedTest2,AddedTest3, AddedTotalHours, AddedoldGPA); }
- //Reprint the array with the students now added
- for(int i=0; i<AcademicClass.size(); i++){
- System.out.println(AcademicClass.get(i));
- }
- 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));
- }
- } catch (FileNotFoundException e) {
- System.err.println("File Input.txt was not found");
- }
- }
- //Sort the array from large to small based on test average
- public static void SortLarge(ArrayList<Student>AcademicClass){
- Student 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
- }
- }
- }
- }
- //Adds a Student who has joined the class to the ArrayList
- public void AddStudent(ArrayList<Student>AcademicClass, String ID, String Name, int T1, int T2, int T3, int tHours, float oldGPA){
- Student newKid = new Student(ID, Name, T1, T2, T3, tHours, oldGPA);
- AcademicClass.add(newKid);
- }
- //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);
- }
- }
- }
- //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