Advertisement
Luninariel

Generics Example

Feb 6th, 2019
180
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 16.67 KB | None | 0 0
  1. package edu.missouriwestern.crandles.CSC285Homework2Part2;
  2.  
  3. import java.io.File;
  4. import java.io.FileNotFoundException;
  5. import java.io.PrintWriter;
  6. import java.util.ArrayList;
  7. import java.util.InputMismatchException;
  8. import java.util.Scanner;
  9.  
  10. public class AcademicClass {
  11. public static void main(String[] args) throws Exception {
  12. int i;
  13.  
  14. //Lets make the location where our output will be located
  15. PrintWriter outpt;
  16. outpt = new PrintWriter(new File("output.txt"));
  17. try {
  18. //lets initalize both our initial file where out Students are brought in and the file where the second set of students are located
  19. Scanner input = new Scanner(new File("input.txt"));
  20. Scanner input2 = new Scanner(new File("input2.txt"));
  21. Scanner inputInts = new Scanner(new File("inputInts.txt"));
  22. while (input.hasNext()) {//if there is info in the file read the values
  23. //Make the inital array where our students will be put. We need this to put into the generic array
  24. Student[] students = new Student[8];
  25. students[0] = new Student(input.next(), input.next(), input.nextInt(), input.nextInt(), input.nextInt(), outpt,input.nextInt(),input.nextFloat());
  26. students[1] = new Student(input.next(), input.next(), input.nextInt(), input.nextInt(), input.nextInt(), outpt,input.nextInt(),input.nextFloat());
  27. students[2] = new Student(input.next(), input.next(), input.nextInt(), input.nextInt(), input.nextInt(), outpt,input.nextInt(),input.nextFloat());
  28. students[3] = new Student(input.next(), input.next(), input.nextInt(), input.nextInt(), input.nextInt(), outpt,input.nextInt(),input.nextFloat());
  29. students[4] = new Student(input.next(), input.next(), input.nextInt(), input.nextInt(), input.nextInt(), outpt,input.nextInt(),input.nextFloat());
  30. students[5] = new Student(input.next(), input.next(), input.nextInt(), input.nextInt(), input.nextInt(), outpt,input.nextInt(),input.nextFloat());
  31. students[6] = new Student(input.next(), input.next(), input.nextInt(), input.nextInt(), input.nextInt(), outpt,input.nextInt(),input.nextFloat());
  32. students[7] = new Student(input.next(), input.next(), input.nextInt(), input.nextInt(), input.nextInt(), outpt,input.nextInt(),input.nextFloat());
  33.  
  34. Double[] ints = new Double[9];
  35. ints[0] = inputInts.nextDouble();
  36. ints[1] = inputInts.nextDouble();
  37. ints[2] = inputInts.nextDouble();
  38. ints[3] = inputInts.nextDouble();
  39. ints[4] = inputInts.nextDouble();
  40. ints[5] = inputInts.nextDouble();
  41. ints[6] = inputInts.nextDouble();
  42. ints[7] = inputInts.nextDouble();
  43. ints[8] = inputInts.nextDouble();
  44.  
  45. //for (i=0;i<=7;i++)students[i].print(outpt);
  46. //Lets make our GenericManager
  47. GenericManager<Student> myStudents = new GenericManager<Student>();
  48. //now lets store our student array into our GenericManager
  49. for (i = 0; i <= 7; i++) myStudents.setValue(students[i]);
  50.  
  51. //now lets read them back and print them
  52. System.out.println("\nThese are from myStudents");
  53. outpt.println("\nThese are from myStudents");
  54. System.out.println("These are from the new Generic Manager using the generic sort\n");
  55. outpt.println("These are from the new Generic Manager using the generic sort\n");
  56.  
  57. //Now lets get the average of the student grade and their corresponding grades
  58. for (i = 0; i <= 7; i++) {
  59. (myStudents.getValue(i)).CalcAverage();
  60. (myStudents.getValue(i)).CalcLetterGrade();
  61. (myStudents.getValue(i)).CalcClassLevel();
  62. (myStudents.getValue(i)).CalcNewGPA();
  63. (myStudents.getValue(i)).CalcGP();
  64. }//end of for
  65. //Now lets sort the students
  66. System.out.println("StudentID Name Score 1 Score 2 Score 3 Average Grade Hours GPA New GPA Class ");
  67. outpt.println("StudentID Name Score 1 Score 2 Score 3 Average Grade Hours GPA New GPA Class ");
  68. for (i=0;i<=7;i++) {
  69. myStudents.ManageAndSort();
  70. (myStudents.getValue(i)).print(outpt);
  71. }//end of for
  72. //Now we need to Delete students based on their StudentID
  73. System.out.println("\nTwo Students have dropped the class.\n Printing List Now:\n");
  74. outpt.println("\nTwo Students have dropped the class.\n Printing List Now:\n");
  75. System.out.println("StudentID Name Score 1 Score 2 Score 3 Average Grade Hours GPA New GPA Class ");
  76. outpt.println("StudentID Name Score 1 Score 2 Score 3 Average Grade Hours GPA New GPA Class ");
  77. for (i=0;i<=5;i++) {
  78. myStudents.DeleteStudent(students,"42P4");
  79. myStudents.DeleteStudent(students,"45A3");
  80. (myStudents.getValue(i)).print(outpt);
  81. }//end of for
  82. //Now we need to Add Students that are brought in from input2.txt
  83. System.out.println("\nA few Students have Added the class.\n Printing List Now:\n");
  84. outpt.println("\nA few Students have Added the class.\n Printing List Now:\n");
  85. for (i=0;i<=8;i++) {
  86. myStudents.AddStudent(students,input2,outpt);
  87. (myStudents.getValue(i)).CalcAverage();
  88. (myStudents.getValue(i)).CalcLetterGrade();
  89. (myStudents.getValue(i)).CalcClassLevel();
  90. (myStudents.getValue(i)).CalcNewGPA();
  91. (myStudents.getValue(i)).CalcGP();
  92. }//end of for
  93. //Now we do the final sort
  94. System.out.println("StudentID Name Score 1 Score 2 Score 3 Average Grade Hours GPA New GPA Class ");
  95. outpt.println("StudentID Name Score 1 Score 2 Score 3 Average Grade Hours GPA New GPA Class ");
  96. for (i=0;i<=8;i++){
  97. myStudents.ManageAndSort();
  98. (myStudents.getValue(i)).print(outpt);
  99. }//end of for
  100.  
  101. System.out.println("\nThis is a list of numbers that prove that the generic array is truly generic\n");
  102. outpt.println("\nThis is a list of numbers that prove that the generic array is truly generic\n");
  103. GenericManager<Double> myints=new GenericManager<Double>();
  104. for (i=0;i<=8;i++)myints.setValue(ints[i]);
  105. for (i=0;i<=9;i++){
  106. myints.ManageAndSort();
  107. System.out.println(myints.getValue(i));
  108. outpt.println(myints.getValue(i));
  109. }
  110. outpt.close();
  111. }//end of while
  112.  
  113. } catch (FileNotFoundException e) {
  114. //file not found or opened
  115. System.out.println("File not found");
  116. System.err.println("File not found");
  117. outpt.close();
  118. System.exit(11);
  119. }//end catch
  120. catch (InputMismatchException e) {
  121. //catch a bad read
  122. System.out.println("Error in reading from File");
  123. System.err.println("Error reading from File");
  124. System.exit(10);
  125. outpt.close();
  126. } finally {
  127. outpt.close();
  128. System.exit(2);
  129. }//end of catch
  130.  
  131.  
  132. }//end of main
  133. }
  134. //Now lets create the Abstract class so we can build the Students
  135. abstract class StudentRecords{
  136. public abstract int getAverage();
  137. }//end of avstract StudentRecords
  138. //Needed this so we could make the Student object and use the CompareTo
  139. class recordBuilder extends StudentRecords implements Comparable {
  140. /* StudentRecords is the parent of Student. This class is is compered by the average of their 3 test grades.
  141. */
  142. protected String studentName;
  143. protected String studentID;
  144. protected int testScore1;
  145. protected int testScore2;
  146. protected int testScore3;
  147. protected int totalHours;
  148. protected float GPA;
  149.  
  150. //create the constructor for recordBuilder
  151. public recordBuilder(String studentI, String studentN, int testS1, int testS2, int testS3, PrintWriter outf,int totalH, float StudentGPA) {
  152. studentName = studentN;
  153. studentID = studentI;
  154. testScore1 = testS1;
  155. testScore2 = testS2;
  156. testScore3 = testS3;
  157. totalHours = totalH;
  158. GPA = StudentGPA;
  159. }//end of recordBuilder constructor
  160. //Now create the compareTo function
  161.  
  162. //now implement the compareTo function
  163.  
  164. public int compareTo(Object o) {
  165. if (getAverage() > ((Student) o).getAverage())
  166. return 1;
  167. else
  168. if (getAverage() < ((Student) o).getAverage())
  169. return -1;
  170. else
  171. return 0;
  172. }//end of the compareTo function for recordBuilder
  173. //Now for setters and getters
  174. public void setTestScore1(int testScore1) {
  175. return;
  176. }//end of setTestScore1
  177. public void setTestScore2(int testScore2) {
  178. return;
  179. }//end of setTestScore2
  180. public void setTestScore3(int testScore3) {
  181. return;
  182. }//end of setTestScore3
  183. public void setTotalHours(int totalHours){
  184. return;
  185. }
  186. public int getTotalHours(){
  187. return totalHours;
  188. }
  189. public void setGPA(float GPA){
  190. return;
  191. }
  192. public float getGPA(){
  193. return GPA;
  194. }
  195. public int getTestScore1() {
  196. return testScore1;
  197. }//end of getTestScore1
  198. public int getTestScore2() {
  199. return testScore2;
  200. }//end of getTestScore2
  201. public int getTestScore3() {
  202. return testScore3;
  203. }//end of getTestScore3
  204. public String getStudentName() {
  205. return "Student";
  206. }//end of getStudentName
  207. public String getStudentID(){
  208. return "Student Name";
  209. }//end of getStudentID
  210. public void CalcAverage(){
  211. return;
  212. }//end of CalcAverage
  213. public int getAverage() {
  214. return 0;
  215. }//end of getAverage
  216. //need this for the output
  217. public void print(PrintWriter outf) {
  218. return;
  219. }//end of print
  220. }//end of recordBuilder
  221. //Lets build the Student class. recordBuilder is the parent of Student.
  222. class Student extends recordBuilder {
  223. protected char letterGrade;
  224. protected int GP;
  225. protected float NewGPA;
  226. protected String classLevel;
  227. protected int average;
  228.  
  229. //now lets build the constructor for Student
  230. public Student(String studentID, String studentName, int testScore1, int testScore2, int testScore3, PrintWriter outf, int totalHours, float GPA) {
  231. super(studentID,studentName,testScore1,testScore2,testScore3,outf,totalHours,GPA);
  232. }//end of Student Constructor
  233. //lets build the method to Calculate the average of the three given test grades
  234. public void CalcAverage(){
  235. average = (testScore1 + testScore2 + testScore3) / 3;
  236. }//end of CalcAverage
  237. public int getAverage() {
  238. return average;
  239. }//end of getAverage
  240. public void CalcNewGPA(){
  241. NewGPA = ((GPA*totalHours)+(2)*GP)/(totalHours+2);
  242. }
  243. public float getNewGPA(){
  244. return NewGPA;
  245. }
  246. //Now lets give a letter grade to the Students based on their Average
  247. public void CalcLetterGrade(){
  248. if (average>=90){
  249. letterGrade = 'A';
  250. }
  251. else if (average >= 80){
  252. letterGrade = 'B';
  253. }
  254. else if (average >= 70){
  255. letterGrade = 'C';
  256. }
  257. else if (average >= 60){
  258. letterGrade = 'D';
  259. }
  260. else letterGrade = 'F';
  261. }//end of CalcLetterGrade
  262. public void CalcClassLevel(){
  263. if (totalHours>=91){
  264. classLevel = "SR";
  265. }
  266. else if (totalHours>=61){
  267. classLevel = "JR";
  268. }
  269. else if (totalHours>=31){
  270. classLevel = "SO";
  271. }
  272. else classLevel ="FR";
  273. }
  274. public void CalcGP(){
  275. if (letterGrade=='A'){
  276. GP = 4;
  277. }
  278. else if (letterGrade == 'B'){
  279. GP = 3;
  280. }
  281. else if (letterGrade == 'C'){
  282. GP = 2;
  283. }
  284. else if (letterGrade == 'D'){
  285. GP = 1;
  286. }
  287. else GP = 0;
  288. }
  289. public int getGP(){
  290. return GP;
  291. }
  292. //public String getClassLevel(){
  293. // return classLevel;
  294. //}
  295. public char getLetterGrade(){
  296. return letterGrade;
  297. }//End of getLetterGrade
  298. public String getStudentID() {
  299. return studentID;
  300. }//end of getStudentID
  301. public String getStudentName() {
  302. return "StudentName ";
  303. }
  304. public void print(PrintWriter outf) {
  305. outf.printf("%-11s %-12s %-8d %-8d %-8d %-8d %-6c %-6d %-6.2f %-9.2f %-10s \n",studentID,studentName,testScore1,testScore2,testScore3,average,
  306. letterGrade,totalHours,GPA,NewGPA,classLevel);//,GP);
  307. System.out.printf("%-11s %-12s %-8d %-8d %-8d %-8d %-6c %-6d %-6.2f %-9.2f %-10s \n",studentID,studentName,testScore1,testScore2,testScore3,average,
  308. letterGrade, totalHours,GPA,NewGPA,classLevel);//,GP);
  309. return;
  310. }//end of print
  311. }//End of Student
  312. //Now lets build a GenericManager
  313. class GenericManager<T extends Comparable> {//NOTE THAT YOU MUST ADD THIS COMPARABLE TO USE COMPARETO FUNCTION
  314. //THAT COMES ALONG WITH T
  315. protected ArrayList<T> myList = new ArrayList<T>();
  316. protected int mcount;
  317.  
  318. public GenericManager() {
  319. //this is the generic constructor
  320. mcount = 0;//mcount is the next available value in array myarray
  321. }//End of GenericManager
  322.  
  323. public int setValue(T x) {//this places values in myarray
  324. myList.add(mcount++, x);
  325. return mcount;
  326. }//end of setValue
  327.  
  328. public T getValue(int i) {//this gets values from myarray
  329. if (i <= mcount) return myList.get(i);
  330. else
  331. return myList.get(0);
  332. }//end of getValue
  333. //This method allows us to delete a Student based on their StudentID
  334. public void DeleteStudent(Student[] myStudents,String StudentID){
  335. //T a, b, c, d;
  336. int i , xlast = myList.size();
  337. for (i = 0; i <= xlast - 2; i++) {
  338. if (myStudents[i].getStudentID().equals(StudentID)) {
  339. myList.remove(myStudents[i]);
  340. break;
  341. }//end of if
  342. }//end of for
  343. }//end of delete student
  344. //This method adds students from the second input text file.
  345. public void AddStudent(Student[] myStudents,Scanner input2,PrintWriter outpt) {
  346. Student a;
  347. int i = 0, xlast = myList.size();
  348. while (input2.hasNext()) {//if there is info in the file read the values
  349. a = myStudents[i] = (new Student(input2.next(), input2.next(), input2.nextInt(), input2.nextInt(), input2.nextInt(), outpt,input2.nextInt(),input2.nextFloat()));
  350. myList.add((T)a);
  351. i++;
  352. }
  353. }
  354. public void ManageAndSort() {
  355. /*This is a generic sort. It will sort anything that the manager manages But the objects being sorted
  356. must support the compareTo function
  357. */
  358. //this method will sort an array of Student objects based on their CompareTo function
  359. T xsave, ysave, a, b;
  360. int isw = 1, xlast = myList.size();
  361. while (isw == 1) {
  362. isw = 0;
  363. for (int i = 0; i <= xlast - 2; i++) {
  364. a = myList.get(i);
  365. b = myList.get(i + 1);
  366. switch (a.compareTo(b)) {
  367. case 1://the objects in array x are in the right order
  368. break;
  369. case -1://objects out of order, they must be changed.
  370. xsave = myList.get(i);
  371. ysave = myList.get(i + 1);
  372. myList.remove(i);
  373. myList.add(i, ysave);
  374. myList.remove(i + 1);
  375. myList.add(i + 1, xsave);
  376. isw = 1;
  377. break;
  378. default://objects are equal no change
  379. }//end of switch
  380. }//end of for
  381. }//end of while
  382. }//end of ManageAndSort
  383. }//end of GenericManager class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement