Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package edu.missouriwestern.crandles.CSC285Homework2Part2;
- import java.io.File;
- import java.io.FileNotFoundException;
- import java.io.PrintWriter;
- import java.util.ArrayList;
- import java.util.InputMismatchException;
- import java.util.Scanner;
- public class AcademicClass {
- public static void main(String[] args) throws Exception {
- int i;
- //Lets make the location where our output will be located
- PrintWriter outpt;
- outpt = new PrintWriter(new File("output.txt"));
- try {
- //lets initalize both our initial file where out Students are brought in and the file where the second set of students are located
- Scanner input = new Scanner(new File("input.txt"));
- Scanner input2 = new Scanner(new File("input2.txt"));
- Scanner inputInts = new Scanner(new File("inputInts.txt"));
- while (input.hasNext()) {//if there is info in the file read the values
- //Make the inital array where our students will be put. We need this to put into the generic array
- Student[] students = new Student[8];
- students[0] = new Student(input.next(), input.next(), input.nextInt(), input.nextInt(), input.nextInt(), outpt,input.nextInt(),input.nextFloat());
- students[1] = new Student(input.next(), input.next(), input.nextInt(), input.nextInt(), input.nextInt(), outpt,input.nextInt(),input.nextFloat());
- students[2] = new Student(input.next(), input.next(), input.nextInt(), input.nextInt(), input.nextInt(), outpt,input.nextInt(),input.nextFloat());
- students[3] = new Student(input.next(), input.next(), input.nextInt(), input.nextInt(), input.nextInt(), outpt,input.nextInt(),input.nextFloat());
- students[4] = new Student(input.next(), input.next(), input.nextInt(), input.nextInt(), input.nextInt(), outpt,input.nextInt(),input.nextFloat());
- students[5] = new Student(input.next(), input.next(), input.nextInt(), input.nextInt(), input.nextInt(), outpt,input.nextInt(),input.nextFloat());
- students[6] = new Student(input.next(), input.next(), input.nextInt(), input.nextInt(), input.nextInt(), outpt,input.nextInt(),input.nextFloat());
- students[7] = new Student(input.next(), input.next(), input.nextInt(), input.nextInt(), input.nextInt(), outpt,input.nextInt(),input.nextFloat());
- Double[] ints = new Double[9];
- ints[0] = inputInts.nextDouble();
- ints[1] = inputInts.nextDouble();
- ints[2] = inputInts.nextDouble();
- ints[3] = inputInts.nextDouble();
- ints[4] = inputInts.nextDouble();
- ints[5] = inputInts.nextDouble();
- ints[6] = inputInts.nextDouble();
- ints[7] = inputInts.nextDouble();
- ints[8] = inputInts.nextDouble();
- //for (i=0;i<=7;i++)students[i].print(outpt);
- //Lets make our GenericManager
- GenericManager<Student> myStudents = new GenericManager<Student>();
- //now lets store our student array into our GenericManager
- for (i = 0; i <= 7; i++) myStudents.setValue(students[i]);
- //now lets read them back and print them
- System.out.println("\nThese are from myStudents");
- outpt.println("\nThese are from myStudents");
- System.out.println("These are from the new Generic Manager using the generic sort\n");
- outpt.println("These are from the new Generic Manager using the generic sort\n");
- //Now lets get the average of the student grade and their corresponding grades
- for (i = 0; i <= 7; i++) {
- (myStudents.getValue(i)).CalcAverage();
- (myStudents.getValue(i)).CalcLetterGrade();
- (myStudents.getValue(i)).CalcClassLevel();
- (myStudents.getValue(i)).CalcNewGPA();
- (myStudents.getValue(i)).CalcGP();
- }//end of for
- //Now lets sort the students
- System.out.println("StudentID Name Score 1 Score 2 Score 3 Average Grade Hours GPA New GPA Class ");
- outpt.println("StudentID Name Score 1 Score 2 Score 3 Average Grade Hours GPA New GPA Class ");
- for (i=0;i<=7;i++) {
- myStudents.ManageAndSort();
- (myStudents.getValue(i)).print(outpt);
- }//end of for
- //Now we need to Delete students based on their StudentID
- System.out.println("\nTwo Students have dropped the class.\n Printing List Now:\n");
- outpt.println("\nTwo Students have dropped the class.\n Printing List Now:\n");
- System.out.println("StudentID Name Score 1 Score 2 Score 3 Average Grade Hours GPA New GPA Class ");
- outpt.println("StudentID Name Score 1 Score 2 Score 3 Average Grade Hours GPA New GPA Class ");
- for (i=0;i<=5;i++) {
- myStudents.DeleteStudent(students,"42P4");
- myStudents.DeleteStudent(students,"45A3");
- (myStudents.getValue(i)).print(outpt);
- }//end of for
- //Now we need to Add Students that are brought in from input2.txt
- System.out.println("\nA few Students have Added the class.\n Printing List Now:\n");
- outpt.println("\nA few Students have Added the class.\n Printing List Now:\n");
- for (i=0;i<=8;i++) {
- myStudents.AddStudent(students,input2,outpt);
- (myStudents.getValue(i)).CalcAverage();
- (myStudents.getValue(i)).CalcLetterGrade();
- (myStudents.getValue(i)).CalcClassLevel();
- (myStudents.getValue(i)).CalcNewGPA();
- (myStudents.getValue(i)).CalcGP();
- }//end of for
- //Now we do the final sort
- System.out.println("StudentID Name Score 1 Score 2 Score 3 Average Grade Hours GPA New GPA Class ");
- outpt.println("StudentID Name Score 1 Score 2 Score 3 Average Grade Hours GPA New GPA Class ");
- for (i=0;i<=8;i++){
- myStudents.ManageAndSort();
- (myStudents.getValue(i)).print(outpt);
- }//end of for
- System.out.println("\nThis is a list of numbers that prove that the generic array is truly generic\n");
- outpt.println("\nThis is a list of numbers that prove that the generic array is truly generic\n");
- GenericManager<Double> myints=new GenericManager<Double>();
- for (i=0;i<=8;i++)myints.setValue(ints[i]);
- for (i=0;i<=9;i++){
- myints.ManageAndSort();
- System.out.println(myints.getValue(i));
- outpt.println(myints.getValue(i));
- }
- outpt.close();
- }//end of while
- } catch (FileNotFoundException e) {
- //file not found or opened
- System.out.println("File not found");
- System.err.println("File not found");
- outpt.close();
- System.exit(11);
- }//end catch
- catch (InputMismatchException e) {
- //catch a bad read
- System.out.println("Error in reading from File");
- System.err.println("Error reading from File");
- System.exit(10);
- outpt.close();
- } finally {
- outpt.close();
- System.exit(2);
- }//end of catch
- }//end of main
- }
- //Now lets create the Abstract class so we can build the Students
- abstract class StudentRecords{
- public abstract int getAverage();
- }//end of avstract StudentRecords
- //Needed this so we could make the Student object and use the CompareTo
- class recordBuilder extends StudentRecords implements Comparable {
- /* StudentRecords is the parent of Student. This class is is compered by the average of their 3 test grades.
- */
- protected String studentName;
- protected String studentID;
- protected int testScore1;
- protected int testScore2;
- protected int testScore3;
- protected int totalHours;
- protected float GPA;
- //create the constructor for recordBuilder
- public recordBuilder(String studentI, String studentN, int testS1, int testS2, int testS3, PrintWriter outf,int totalH, float StudentGPA) {
- studentName = studentN;
- studentID = studentI;
- testScore1 = testS1;
- testScore2 = testS2;
- testScore3 = testS3;
- totalHours = totalH;
- GPA = StudentGPA;
- }//end of recordBuilder constructor
- //Now create the compareTo function
- //now implement the compareTo function
- public int compareTo(Object o) {
- if (getAverage() > ((Student) o).getAverage())
- return 1;
- else
- if (getAverage() < ((Student) o).getAverage())
- return -1;
- else
- return 0;
- }//end of the compareTo function for recordBuilder
- //Now for setters and getters
- public void setTestScore1(int testScore1) {
- return;
- }//end of setTestScore1
- public void setTestScore2(int testScore2) {
- return;
- }//end of setTestScore2
- public void setTestScore3(int testScore3) {
- return;
- }//end of setTestScore3
- public void setTotalHours(int totalHours){
- return;
- }
- public int getTotalHours(){
- return totalHours;
- }
- public void setGPA(float GPA){
- return;
- }
- public float getGPA(){
- return GPA;
- }
- public int getTestScore1() {
- return testScore1;
- }//end of getTestScore1
- public int getTestScore2() {
- return testScore2;
- }//end of getTestScore2
- public int getTestScore3() {
- return testScore3;
- }//end of getTestScore3
- public String getStudentName() {
- return "Student";
- }//end of getStudentName
- public String getStudentID(){
- return "Student Name";
- }//end of getStudentID
- public void CalcAverage(){
- return;
- }//end of CalcAverage
- public int getAverage() {
- return 0;
- }//end of getAverage
- //need this for the output
- public void print(PrintWriter outf) {
- return;
- }//end of print
- }//end of recordBuilder
- //Lets build the Student class. recordBuilder is the parent of Student.
- class Student extends recordBuilder {
- protected char letterGrade;
- protected int GP;
- protected float NewGPA;
- protected String classLevel;
- protected int average;
- //now lets build the constructor for Student
- public Student(String studentID, String studentName, int testScore1, int testScore2, int testScore3, PrintWriter outf, int totalHours, float GPA) {
- super(studentID,studentName,testScore1,testScore2,testScore3,outf,totalHours,GPA);
- }//end of Student Constructor
- //lets build the method to Calculate the average of the three given test grades
- public void CalcAverage(){
- average = (testScore1 + testScore2 + testScore3) / 3;
- }//end of CalcAverage
- public int getAverage() {
- return average;
- }//end of getAverage
- public void CalcNewGPA(){
- NewGPA = ((GPA*totalHours)+(2)*GP)/(totalHours+2);
- }
- public float getNewGPA(){
- return NewGPA;
- }
- //Now lets give a letter grade to the Students based on their Average
- public void CalcLetterGrade(){
- if (average>=90){
- letterGrade = 'A';
- }
- else if (average >= 80){
- letterGrade = 'B';
- }
- else if (average >= 70){
- letterGrade = 'C';
- }
- else if (average >= 60){
- letterGrade = 'D';
- }
- else letterGrade = 'F';
- }//end of CalcLetterGrade
- public void CalcClassLevel(){
- if (totalHours>=91){
- classLevel = "SR";
- }
- else if (totalHours>=61){
- classLevel = "JR";
- }
- else if (totalHours>=31){
- classLevel = "SO";
- }
- else classLevel ="FR";
- }
- public void CalcGP(){
- if (letterGrade=='A'){
- GP = 4;
- }
- else if (letterGrade == 'B'){
- GP = 3;
- }
- else if (letterGrade == 'C'){
- GP = 2;
- }
- else if (letterGrade == 'D'){
- GP = 1;
- }
- else GP = 0;
- }
- public int getGP(){
- return GP;
- }
- //public String getClassLevel(){
- // return classLevel;
- //}
- public char getLetterGrade(){
- return letterGrade;
- }//End of getLetterGrade
- public String getStudentID() {
- return studentID;
- }//end of getStudentID
- public String getStudentName() {
- return "StudentName ";
- }
- public void print(PrintWriter outf) {
- outf.printf("%-11s %-12s %-8d %-8d %-8d %-8d %-6c %-6d %-6.2f %-9.2f %-10s \n",studentID,studentName,testScore1,testScore2,testScore3,average,
- letterGrade,totalHours,GPA,NewGPA,classLevel);//,GP);
- System.out.printf("%-11s %-12s %-8d %-8d %-8d %-8d %-6c %-6d %-6.2f %-9.2f %-10s \n",studentID,studentName,testScore1,testScore2,testScore3,average,
- letterGrade, totalHours,GPA,NewGPA,classLevel);//,GP);
- return;
- }//end of print
- }//End of Student
- //Now lets build a GenericManager
- class GenericManager<T extends Comparable> {//NOTE THAT YOU MUST ADD THIS COMPARABLE TO USE COMPARETO FUNCTION
- //THAT COMES ALONG WITH T
- protected ArrayList<T> myList = new ArrayList<T>();
- protected int mcount;
- public GenericManager() {
- //this is the generic constructor
- mcount = 0;//mcount is the next available value in array myarray
- }//End of GenericManager
- public int setValue(T x) {//this places values in myarray
- myList.add(mcount++, x);
- return mcount;
- }//end of setValue
- public T getValue(int i) {//this gets values from myarray
- if (i <= mcount) return myList.get(i);
- else
- return myList.get(0);
- }//end of getValue
- //This method allows us to delete a Student based on their StudentID
- public void DeleteStudent(Student[] myStudents,String StudentID){
- //T a, b, c, d;
- int i , xlast = myList.size();
- for (i = 0; i <= xlast - 2; i++) {
- if (myStudents[i].getStudentID().equals(StudentID)) {
- myList.remove(myStudents[i]);
- break;
- }//end of if
- }//end of for
- }//end of delete student
- //This method adds students from the second input text file.
- public void AddStudent(Student[] myStudents,Scanner input2,PrintWriter outpt) {
- Student a;
- int i = 0, xlast = myList.size();
- while (input2.hasNext()) {//if there is info in the file read the values
- a = myStudents[i] = (new Student(input2.next(), input2.next(), input2.nextInt(), input2.nextInt(), input2.nextInt(), outpt,input2.nextInt(),input2.nextFloat()));
- myList.add((T)a);
- i++;
- }
- }
- public void ManageAndSort() {
- /*This is a generic sort. It will sort anything that the manager manages But the objects being sorted
- must support the compareTo function
- */
- //this method will sort an array of Student objects based on their CompareTo function
- T xsave, ysave, a, b;
- int isw = 1, xlast = myList.size();
- while (isw == 1) {
- isw = 0;
- for (int i = 0; i <= xlast - 2; i++) {
- a = myList.get(i);
- b = myList.get(i + 1);
- switch (a.compareTo(b)) {
- case 1://the objects in array x are in the right order
- break;
- case -1://objects out of order, they must be changed.
- xsave = myList.get(i);
- ysave = myList.get(i + 1);
- myList.remove(i);
- myList.add(i, ysave);
- myList.remove(i + 1);
- myList.add(i + 1, xsave);
- isw = 1;
- break;
- default://objects are equal no change
- }//end of switch
- }//end of for
- }//end of while
- }//end of ManageAndSort
- }//end of GenericManager class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement