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.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
- * - 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
- * - Print the LinkedList.
- *
- * @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
- LinkedListGenericManager<Student> StudentLinkList = new LinkedListGenericManager<Student>();
- //Create a LinkedList to contain Student's from Input.txt and Additions.Txt in Order
- LinkedListGenericManager<Student> StudentLinkOrder = new LinkedListGenericManager<Student>();
- //Create a LinkedList of Doubles to add in order
- LinkedListGenericManager<Double> DoubleOrder = new LinkedListGenericManager<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
- StudentLinkList.addnode(student1);
- StudentLinkOrder.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 = StudentLinkList.getnumber();
- for (int i = 1; i <= number; i++) {
- System.out.println(StudentLinkList.getnode(i));
- }
- DeleteNode(StudentLinkList, "45A3");
- DeleteNode(StudentLinkList, "42P4");
- System.out.println("\nStudents 45A3 & 42P4 Have Dropped The Class. Generating New Roster: \n");
- //Print the contents of the LinkeList including their % score and grade AFTER the students have dropped
- int number7 = StudentLinkList.getnumber();
- for (int i = 1; i <= number7-1; i++) {
- System.out.println(StudentLinkList.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
- StudentLinkList.addnode(student2);
- StudentLinkOrder.addinorder(student2);
- }
- //Reprint the LinkedList with the additional students now added
- int number2 = StudentLinkList.getnumber();
- for (int i = 1; i <= number2; i++) {
- System.out.println(StudentLinkList.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 = StudentLinkOrder.getnumber();
- for (int i = 1; i <= number3; i++) {
- System.out.println(StudentLinkOrder.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 ArrayList for doubles
- while (inputDoubles.hasNextLine()) {
- DoubleOrder.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 = DoubleOrder.getnumber();
- for (int i = number6; i >= 1; i--) {
- System.out.println(DoubleOrder.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(LinkedListGenericManager<Student> StudentLinkList, String ID) {
- int number5 = StudentLinkList.getnumber();
- for (int i = 1; i <= number5-1; i++) {
- if (StudentLinkList.getnode(i).getStudentID().equals(ID)) {
- StudentLinkList.RemoveNode(i);
- }
- }
- }
- //The LinkedList Generic Manager, will take student objects and manipulate them in place of an ArrayList
- public static class LinkedListGenericManager<T extends Comparable<T>> {
- MyNode<T> head = null;
- MyNode<T> tail = null;
- int number = 0;
- //Constructor for LinkedListGenericManager
- public LinkedListGenericManager() {
- head = null;
- tail = null;
- number = 0;
- }
- //Getter for Number
- public int getnumber() {
- return number;
- }
- //this adds a node to the linked list. It will always add to the front of the list
- public int addnode(T myGenericData) {
- addfront(myGenericData);
- return number;
- }
- //Adds a node to the front of the list.
- public void addfront(T myGenericData) {
- MyNode<T> newnode = new MyNode<T>(myGenericData);
- if (head == null) {
- head = newnode;
- tail = newnode;
- } else {
- newnode.next = head;
- head = newnode;
- }
- number++;
- return;
- }
- public T RemoveNode(int index) {
- MyNode<T> previous = head;
- for (int i = 1; i < index-1; i++) {
- previous = previous.next;
- }
- MyNode<T> current = previous.next;
- previous.next = current.next;
- number--;
- return current.nodevalue;
- }
- //Returns the node at the x'th position in the list
- public T getnode(int x) {
- if ((x < 0) || (x > number)) System.out.println("error in getnode" + x + "while list holds" + number);
- int ict = 1;
- MyNode<T> curnode;
- curnode = head;
- while (ict < x) {
- curnode = curnode.next;
- ict++;
- }
- return curnode.nodevalue;
- }
- //this function adds the new node in compareTo order
- public void addinorder(T myGenericData) {
- MyNode<T> newnode = new MyNode<T>(myGenericData);
- MyNode<T> cnode;
- MyNode<T> nnode;
- if (number == 0) {
- head = newnode;
- tail = newnode;
- number = 1;
- return;
- }
- if (myGenericData.compareTo(head.nodevalue) == 1) {
- newnode.next = head;
- head = newnode;
- number++;
- return;
- }
- if (myGenericData.compareTo(tail.nodevalue) == -1) {
- tail.next = newnode;
- tail = newnode;
- number++;
- return;
- }
- cnode = head;
- nnode = head.next;
- while (myGenericData.compareTo(nnode.nodevalue) != 1) {
- cnode = nnode;
- nnode = nnode.next;
- }
- cnode.next = newnode;
- newnode.next = nnode;
- number++;
- return;
- }
- // An internal class for constructing nodes in LinkListGenericManager
- public static class MyNode<T> {
- protected T nodevalue;
- protected MyNode<T> next;
- public MyNode(T myGenericData) {
- nodevalue = myGenericData;
- next = null;
- }
- }
- }
- //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