Advertisement
Luninariel

Student Roster Manipulation - Java LinkList Class

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