Advertisement
Luninariel

Student Roster Manipulation - Linked List

Feb 11th, 2019
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 14.87 KB | None | 0 0
  1. import java.io.File;
  2. import java.io.FileNotFoundException;
  3. import java.util.Scanner;
  4.  
  5. /**
  6. * This program performs the following functions:
  7. * - Will load student records from input.txt into their respective variables
  8. * - Will turn those variables into a Student Object
  9. * - Calculate their average % based on test scores
  10. * - Calculate their letter grade based on their average %
  11. * - Calculate their new GPA using their old GPA and their current class hours.
  12. * - Calculate their year rank based on the number of hours the have.
  13. * - Places the Student objects into a LinkedList representing the academic class.
  14. * - Lists the class with their % score and letter grade in the class, as well as their new GPA and year Rank.
  15. * - Remove the records matching ID 42P4 and 45A3
  16. * - Re-list the records with the removed students.
  17. * - Add the Student Records from Additions.txt
  18. * - Sort the LinkedList from Highest to lowest based on % score
  19. * - Print the LinkedList.
  20. *
  21. * @author Carlos
  22. * @since 02/11/2019
  23. **/
  24. public class RosterManipulations {
  25.  
  26. public static void main(String[] args) throws Exception {
  27. RosterManipulations me = new RosterManipulations();
  28.  
  29. //Create a LinkedList to contain Student's from input.txt and Additions.txt
  30. LinkedListGenericManager<Student> StudentLinkList = new LinkedListGenericManager<Student>();
  31.  
  32. //Create a LinkedList to contain Student's from Input.txt and Additions.Txt in Order
  33. LinkedListGenericManager<Student> StudentLinkOrder = new LinkedListGenericManager<Student>();
  34.  
  35. //Create a LinkedList of Doubles to add in order
  36. LinkedListGenericManager<Double> DoubleOrder = new LinkedListGenericManager<Double>();
  37.  
  38. try {
  39. Scanner input = new Scanner(new File("src/main/input.txt"));
  40.  
  41. //Gather the information from the records in input.txt and set them equal to their respective variables
  42. while (input.hasNextLine()) {
  43. String StudentID = input.next();
  44. String Studentname = input.next();
  45. int test1 = input.nextInt();
  46. int test2 = input.nextInt();
  47. int test3 = input.nextInt();
  48. int totalHours = input.nextInt();
  49. float oldGPA = input.nextFloat();
  50.  
  51. //Place those variables into a student object
  52. Student student1 = me.new Student(StudentID, Studentname, test1, test2, test3, totalHours, oldGPA);
  53.  
  54. //Place that object into the LinkedList StudentLinkList
  55. StudentLinkList.addnode(student1);
  56. StudentLinkOrder.addinorder(student1);
  57. }
  58.  
  59. System.out.println("\nOriginal Class Roster Before Modifications: \n");
  60. //Print the contents of the LinkedList including their % score and grade before any manipulation
  61. int number = StudentLinkList.getnumber();
  62. for (int i = 1; i <= number; i++) {
  63. System.out.println(StudentLinkList.getnode(i));
  64. }
  65.  
  66. DeleteNode(StudentLinkList, "45A3");
  67. DeleteNode(StudentLinkList, "42P4");
  68. System.out.println("\nStudents 45A3 & 42P4 Have Dropped The Class. Generating New Roster: \n");
  69. //Print the contents of the LinkeList including their % score and grade AFTER the students have dropped
  70. int number7 = StudentLinkList.getnumber();
  71. for (int i = 1; i <= number7-1; i++) {
  72. System.out.println(StudentLinkList.getnode(i));
  73. }
  74.  
  75. System.out.println("\n New Students Have Joined The Class. Generating Roster: \n");
  76.  
  77. Scanner secondInput = new Scanner(new File("src/main/Additions.txt"));
  78. while (secondInput.hasNextLine()) {
  79. String AddedID = secondInput.next();
  80. String AddedName = secondInput.next();
  81. int AddedTest1 = secondInput.nextInt();
  82. int AddedTest2 = secondInput.nextInt();
  83. int AddedTest3 = secondInput.nextInt();
  84. int AddedTotalHours = secondInput.nextInt();
  85. float AddedoldGPA = secondInput.nextFloat();
  86.  
  87. Student student2 = me.new Student(AddedID, AddedName, AddedTest1, AddedTest2, AddedTest3, AddedTotalHours, AddedoldGPA);
  88.  
  89. //Adding the additional students to the LinkedList
  90. StudentLinkList.addnode(student2);
  91. StudentLinkOrder.addinorder(student2);
  92. }
  93.  
  94. //Reprint the LinkedList with the additional students now added
  95. int number2 = StudentLinkList.getnumber();
  96. for (int i = 1; i <= number2; i++) {
  97. System.out.println(StudentLinkList.getnode(i));
  98. }
  99.  
  100.  
  101. System.out.println("\nRegenerating Roster Sorted By Average Grade\n");
  102. //Print the Contents of the LinkedLists in order from Larger to Smaller based on Average %
  103. int number3 = StudentLinkOrder.getnumber();
  104. for (int i = 1; i <= number3; i++) {
  105. System.out.println(StudentLinkOrder.getnode(i));
  106. }
  107.  
  108.  
  109. //Read the contents of doubles.txt
  110. Scanner inputDoubles = new Scanner(new File("src/main/doubles.txt"));
  111. //Add the contents of doubles.txt to the ArrayList for doubles
  112. while (inputDoubles.hasNextLine()) {
  113. DoubleOrder.addinorder(inputDoubles.nextDouble());
  114. }
  115.  
  116. //Print the sorted contents of doubles.txt from Smaller to Larger
  117. System.out.println("Proving Generic Sort By Printing Sorted Doubles From Smallest to Largest\n");
  118.  
  119. //Print the sorted contents of doubles.txt from Smaller to Larger
  120. int number6 = DoubleOrder.getnumber();
  121. for (int i = number6; i >= 1; i--) {
  122. System.out.println(DoubleOrder.getnode(i));
  123. }
  124.  
  125.  
  126. } catch (FileNotFoundException e) {
  127. System.err.println("File Input.txt was not found");
  128. }
  129.  
  130. }
  131.  
  132. //Deletes a Student who has dropped the class from the LinkedList.
  133. public static void DeleteNode(LinkedListGenericManager<Student> StudentLinkList, String ID) {
  134. int number5 = StudentLinkList.getnumber();
  135. for (int i = 1; i <= number5-1; i++) {
  136. if (StudentLinkList.getnode(i).getStudentID().equals(ID)) {
  137. StudentLinkList.RemoveNode(i);
  138.  
  139. }
  140. }
  141.  
  142.  
  143. }
  144.  
  145. //The LinkedList Generic Manager, will take student objects and manipulate them in place of an ArrayList
  146. public static class LinkedListGenericManager<T extends Comparable<T>> {
  147. MyNode<T> head = null;
  148. MyNode<T> tail = null;
  149. int number = 0;
  150.  
  151. //Constructor for LinkedListGenericManager
  152. public LinkedListGenericManager() {
  153. head = null;
  154. tail = null;
  155. number = 0;
  156. }
  157.  
  158. //Getter for Number
  159. public int getnumber() {
  160. return number;
  161. }
  162.  
  163. //this adds a node to the linked list. It will always add to the front of the list
  164. public int addnode(T myGenericData) {
  165. addfront(myGenericData);
  166. return number;
  167. }
  168.  
  169. //Adds a node to the front of the list.
  170. public void addfront(T myGenericData) {
  171. MyNode<T> newnode = new MyNode<T>(myGenericData);
  172. if (head == null) {
  173. head = newnode;
  174. tail = newnode;
  175. } else {
  176. newnode.next = head;
  177. head = newnode;
  178. }
  179. number++;
  180. return;
  181. }
  182.  
  183. public T RemoveNode(int index) {
  184. MyNode<T> previous = head;
  185.  
  186. for (int i = 1; i < index-1; i++) {
  187. previous = previous.next;
  188. }
  189. MyNode<T> current = previous.next;
  190. previous.next = current.next;
  191. number--;
  192. return current.nodevalue;
  193. }
  194.  
  195.  
  196. //Returns the node at the x'th position in the list
  197. public T getnode(int x) {
  198. if ((x < 0) || (x > number)) System.out.println("error in getnode" + x + "while list holds" + number);
  199. int ict = 1;
  200. MyNode<T> curnode;
  201. curnode = head;
  202. while (ict < x) {
  203. curnode = curnode.next;
  204. ict++;
  205. }
  206. return curnode.nodevalue;
  207. }
  208.  
  209. //this function adds the new node in compareTo order
  210. public void addinorder(T myGenericData) {
  211. MyNode<T> newnode = new MyNode<T>(myGenericData);
  212. MyNode<T> cnode;
  213. MyNode<T> nnode;
  214.  
  215. if (number == 0) {
  216. head = newnode;
  217. tail = newnode;
  218. number = 1;
  219. return;
  220. }
  221.  
  222.  
  223. if (myGenericData.compareTo(head.nodevalue) == 1) {
  224. newnode.next = head;
  225. head = newnode;
  226. number++;
  227. return;
  228. }
  229. if (myGenericData.compareTo(tail.nodevalue) == -1) {
  230. tail.next = newnode;
  231. tail = newnode;
  232. number++;
  233. return;
  234. }
  235.  
  236. cnode = head;
  237. nnode = head.next;
  238.  
  239. while (myGenericData.compareTo(nnode.nodevalue) != 1) {
  240. cnode = nnode;
  241. nnode = nnode.next;
  242. }
  243. cnode.next = newnode;
  244. newnode.next = nnode;
  245. number++;
  246. return;
  247. }
  248.  
  249. // An internal class for constructing nodes in LinkListGenericManager
  250. public static class MyNode<T> {
  251. protected T nodevalue;
  252. protected MyNode<T> next;
  253.  
  254. public MyNode(T myGenericData) {
  255. nodevalue = myGenericData;
  256. next = null;
  257. }
  258. }
  259.  
  260. }
  261.  
  262. //The Student class turns student records into objects ao that they an be sorted into an ArrayLIst
  263. public class Student implements Comparable<Student> {
  264. String StudentID;
  265. String Studentname;
  266. int test1;
  267. int test2;
  268. int test3;
  269. int average;
  270. int totalHours;
  271. float oldGPA;
  272.  
  273. //Constructor for Student Class
  274. public Student(String StudentID, String Studentname, int test1, int test2, int test3, int totalHours, float oldGPA) {
  275. setStudentID(StudentID);
  276. setStudentname(Studentname);
  277. setTest1(test1);
  278. setTest2(test2);
  279. setTest3(test3);
  280. int average = (test1 + test2 + test3) / 3;
  281. setAverage(average);
  282. setTotalHours(totalHours);
  283. setoldGPA(oldGPA);
  284. }
  285.  
  286. @Override
  287. //toString so that objects can be printed.
  288. public String toString() {
  289. 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";
  290. }
  291.  
  292. //Calculates the NEW GPA of a student
  293. public float calculatenewGPA(float oldGPA, int gradePoint, int totalHours) {
  294. return ((oldGPA * totalHours) + (2) * gradePoint) / (totalHours + 2);
  295. }
  296.  
  297. //Calculate the GradePoint Value of A GPA
  298. public int gradePoint(int average) {
  299. {
  300. int gradePoint = 0;
  301. if (average >= 90) {
  302. gradePoint = 4;
  303. } else if (average >= 80) {
  304. gradePoint = 3;
  305. } else if (average >= 70) {
  306. gradePoint = 2;
  307. } else if (average >= 60) {
  308. gradePoint = 1;
  309. } else if (average >= 0) {
  310. gradePoint = 0;
  311. }
  312. return gradePoint;
  313. }
  314. }
  315.  
  316. //Calculate the Year Rank of each student
  317. public String yearRank(int totalHours) {
  318. String rank = null;
  319. if (totalHours <= 30) {
  320. rank = "Freshman";
  321. } else if (totalHours <= 60) {
  322. rank = "Sophomore";
  323. } else if (totalHours <= 90) {
  324. rank = "Junior";
  325. } else if (totalHours >= 91) {
  326. rank = "Senior";
  327. }
  328. return rank;
  329. }
  330.  
  331. //Calculate the average grade of a student
  332.  
  333. //Turn the average into a letter grade
  334. public String averageGrade(int average) {
  335. String averageLetter = null;
  336. if (average >= 90) {
  337. averageLetter = "A";
  338. } else if (average >= 80) {
  339. averageLetter = "B";
  340. } else if (average >= 70) {
  341. averageLetter = "C";
  342. } else if (average >= 60) {
  343. averageLetter = "D";
  344. } else if (average >= 0) {
  345. averageLetter = "F";
  346. }
  347. return averageLetter;
  348. }
  349.  
  350. public void setoldGPA(float oldGPA) {
  351. this.oldGPA = oldGPA;
  352. }
  353.  
  354. //Getters for the Student Class
  355. public String getStudentID() {
  356. return StudentID;
  357. }
  358.  
  359. //Setters for the Student Class
  360. public void setStudentID(String StudentID) {
  361. this.StudentID = StudentID;
  362. }
  363.  
  364. public String getStudentname() {
  365. return Studentname;
  366. }
  367.  
  368. public void setStudentname(String Studentname) {
  369. this.Studentname = Studentname;
  370. }
  371.  
  372. public int getTest1() {
  373. return test1;
  374. }
  375.  
  376. public void setTest1(int test1) {
  377. this.test1 = test1;
  378. }
  379.  
  380. public int getTest2() {
  381. return test2;
  382. }
  383.  
  384. public void setTest2(int test2) {
  385. this.test2 = test2;
  386. }
  387.  
  388. public int getTest3() {
  389. return test3;
  390. }
  391.  
  392. public void setTest3(int test3) {
  393. this.test3 = test3;
  394. }
  395.  
  396. public int getAverage() {
  397. return average;
  398. }
  399.  
  400. public void setAverage(int average) {
  401. this.average = average;
  402. }
  403.  
  404. public int getTotalHours() {
  405. return totalHours;
  406. }
  407.  
  408. public void setTotalHours(int totalHours) {
  409. this.totalHours = totalHours;
  410. }
  411.  
  412. public float getoldGPA() {
  413. return oldGPA;
  414. }
  415.  
  416.  
  417. public int compareTo(Student o) {
  418. return (this.getAverage() < o.getAverage() ? -1 :
  419. (this.getAverage() == o.getAverage() ? 0 : 1));
  420. }
  421. }
  422. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement