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.ListIterator;
- import java.util.Scanner;
- import java.util.*;
- /**
- * 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
- * - Calculate their letter grade based on their average %
- * - Calculate their new GPA using their old GPA and their current class hours.
- * - Calculate their year rank based on the number of hours the have.
- * - Places the Student objects into a LinkedList representing the academic class.
- * - Lists the class with their % score and letter grade in the class, as well as their new GPA and year Rank.
- * - Remove the records matching ID 42P4 and 45A3
- * - Re-list the records with the removed students.
- * - Add the Student Records from Additions.txt
- * - Sort the LinkedList from Highest to lowest based on % score
- * - To prove the Generic nature of the program, a list of doubles is read in and sorted from smallest to largest
- * - Print the LinkedLists.
- *
- * @author Carlos
- * @since 02/11/2019
- **/
- public class RosterManipulations {
- public static void main(String[] args) throws Exception {
- RosterManipulations me = new RosterManipulations();
- //Create a LinkedList to contain Student's from input.txt and Additions.txt
- JavaLinkedListManager<Student> JavaStudentList = new JavaLinkedListManager<Student>();
- //Create a LinkedList to contain Student's from Input.txt and Additions.Txt in Order
- JavaLinkedListManager<Student> JavaStudentListOrder = new JavaLinkedListManager<Student>();
- //Create a LinkedList of Doubles to add in order
- JavaLinkedListManager<Double> JavaDoubleOrder = new JavaLinkedListManager<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 that object into the LinkedList StudentLinkList
- JavaStudentList.AddNode(student1);
- JavaStudentListOrder.AddInOrder(student1);
- }
- System.out.println("\nOriginal Class Roster Before Modifications: \n");
- //Print the contents of the LinkedList including their % score and grade before any manipulation
- int number = JavaStudentList.GetNumber();
- for (int i = 0; i < number; i++) {
- System.out.println(JavaStudentList.GetNode(i));
- }
- DeleteNode(JavaStudentList, "45A3");
- DeleteNode(JavaStudentList, "42P4");
- DeleteNode(JavaStudentListOrder,"45A3");
- DeleteNode(JavaStudentListOrder,"42P4");
- System.out.println("\nStudents 45A3 & 42P4 Have Dropped The Class. Generating New Roster: \n");
- //Print the contents of the LinkedList including their % score and grade AFTER the students have dropped
- int number7 = JavaStudentList.GetNumber();
- for (int i = 0; i < number7; i++) {
- System.out.println(JavaStudentList.GetNode(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 the additional students to the LinkedList
- JavaStudentList.AddNode(student2);
- JavaStudentListOrder.AddInOrder(student2);
- }
- //Reprint the LinkedList with the additional students now added
- int number2 = JavaStudentList.GetNumber();
- for (int i = 0; i < number2; i++) {
- System.out.println(JavaStudentList.GetNode(i));
- }
- System.out.println("\nRegenerating Roster Sorted By Average Grade\n");
- //Print the Contents of the LinkedLists in order from Larger to Smaller based on Average %
- int number3 = JavaStudentListOrder.GetNumber();
- for (int i = 0; i < number3; i++) {
- System.out.println(JavaStudentListOrder.GetNode(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 LinkedList for doubles
- while (inputDoubles.hasNextLine()) {
- JavaDoubleOrder.AddInOrder(inputDoubles.nextDouble());
- }
- //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
- int number6 = JavaDoubleOrder.GetNumber();
- for (int i = number6 - 1; i >= 0; i--) {
- System.out.println(JavaDoubleOrder.GetNode(i));
- }
- } catch (FileNotFoundException e) {
- System.err.println("File Input.txt was not found");
- }
- }
- //Deletes a Student who has dropped the class from the LinkedList.
- public static void DeleteNode(JavaLinkedListManager<Student> JavaStudentList, String ID) {
- int number5 = JavaStudentList.GetNumber();
- for (int i = 0; i < number5; i++) {
- if (JavaStudentList.GetNode(i).getStudentID().equals(ID)) {
- JavaStudentList.RemoveNodeJava(i);
- return;
- }
- }
- }
- //Java's LinkedList Manager, Takes student objects and manipulates them in place of an ArrayList or manually writing a LinkedList Class
- public static class JavaLinkedListManager<T extends Comparable<T>> {
- protected LinkedList<T> JavaList = null;
- protected int number = 0;
- protected ListIterator<T> lptr = null;
- protected T y = null;
- //Constructor
- public JavaLinkedListManager() {
- JavaList = new LinkedList<T>();
- lptr = JavaList.listIterator();
- number = 0;
- }
- //Getter for Number
- public int GetNumber() {
- return number;
- }
- //Adds a Node to the LinkedList
- public int AddNode(T myGenericData) {
- JavaList.addLast(myGenericData);
- number++;
- return number;
- }
- //Finds a node in the Xth location of the LinkedList
- public T GetNode(int x) {
- if ((x < 0) || (x > number)) System.out.println("error in getnode" + x + "while list holds" + number);
- return JavaList.get(x);
- }
- //Adds to the LinkedList from Larger to Smaller using CompareTo
- public void AddInOrder(T myGenericData) {
- if (number == 0) {
- JavaList.addFirst(myGenericData);
- number = 1;
- return;
- }
- if (myGenericData.compareTo(JavaList.getFirst()) == 1) {
- JavaList.addFirst(myGenericData);
- number++;
- return;
- }
- if (myGenericData.compareTo(JavaList.getLast()) == -1) {
- JavaList.addLast(myGenericData);
- number++;
- return;
- }
- ListIterator lptr2 = JavaList.listIterator();
- for (int i = 0; i <= number - 1; i++) {
- y = (T) lptr2.next();
- if (myGenericData.compareTo(y) == 1) {
- lptr2.previous();
- lptr2.add(myGenericData);
- number++;
- return;
- }
- }
- }
- //Removes a node from the LinkedList
- public void RemoveNodeJava (int index){
- JavaList.remove(index);
- number--;
- return;
- }
- }
- //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;
- //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);
- }
- @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;
- }
- //Calculate the average grade of a student
- //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;
- }
- public void setoldGPA(float oldGPA) {
- this.oldGPA = oldGPA;
- }
- //Getters for the Student Class
- public String getStudentID() {
- return StudentID;
- }
- //Setters for the Student Class
- public void setStudentID(String StudentID) {
- this.StudentID = StudentID;
- }
- public String getStudentname() {
- return Studentname;
- }
- public void setStudentname(String Studentname) {
- this.Studentname = Studentname;
- }
- public int getTest1() {
- return test1;
- }
- public void setTest1(int test1) {
- this.test1 = test1;
- }
- public int getTest2() {
- return test2;
- }
- public void setTest2(int test2) {
- this.test2 = test2;
- }
- public int getTest3() {
- return test3;
- }
- public void setTest3(int test3) {
- this.test3 = test3;
- }
- public int getAverage() {
- return average;
- }
- public void setAverage(int average) {
- this.average = average;
- }
- public int getTotalHours() {
- return totalHours;
- }
- public void setTotalHours(int totalHours) {
- this.totalHours = 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