/* ************** Project 4 Advanced Version ************** ********* Instructor : Professor Zdravko Markov ********* ********* Submitted By : Salahuddin Akand ********* ********* Date : July 1st, 2000 ********* ********* Course No : CS500 ********* *** This program reads student information from keyboard *** *** and it can sort Students by Student's name *** *** or it can sort Students by their GPA *** *** by using methods from student class *** */ import student; import java.io.*; import java.util.*; class use_student{ public static void main (String[] args ) throws IOException { //Declare variables int swap = 0, i, n= 0, j = 0 , option = 1,sort; String [] a = new String [100]; String s1,s2,s3,s4,s5,s6,s7; BufferedReader stdin; stdin = new BufferedReader (new InputStreamReader (System.in)); student [] st = new student [100]; student t; //Take input from user while (option == 1) { System.out.println ("Enter student name, three course no and grades separated by comma: " + "\n" + "Ex: John Smith,CS111,A,CS112,B+,CS114,A- (lower case acceptable): " ); StringTokenizer str = new StringTokenizer(stdin.readLine(),","); while (str.hasMoreTokens()) { a[n] = str.nextToken().toUpperCase(); n = n + 1; } s1=a[0]; s2=a[1]; s3=a[2]; s4=a[3]; s5=a[4]; s6=a[5];s7=a[6]; st[j] = new student (s1,s2,s3,s4,s5,s6,s7); System.out.print ("Enter 1 for new student, 2 to continue : "); option = Integer.parseInt (stdin.readLine()); if (option == 1) { n = 0; j = j + 1; } else option = 2; } //Print student information for (i=0; i < (j + 1) ; i++) { System.out.println (st[i].get_info()); System.out.println ("GPA is : " +st[i].get_gpa()); } // Give option for multiple sorting option = 4; while (option == 4) { System.out.print ("Enter 6 to sort by student name, enter 7 to sort by GPA : "); sort = Integer.parseInt (stdin.readLine()); if (sort == 6) // Sort by student name using bubble sort method { do { swap = 0; for (i=0; i < j ; i++) { if (st[i].get_name().compareTo(st[i+1].get_name()) > 0) { t = st[i]; st[i] = st[i+1]; st[i+1] = t; swap++; } } } while (swap>0); //end of first sort System.out.println ("\nSorted by student name in ascending order: " ); for (i=0; i < j + 1; i++) //print student sorted by name { System.out.println (st[i].get_name() + " " + st[i].get_gpa()); } sort = 100; } else if (sort == 7) //sort by student GPA using bubble sort method { do { swap = 0; for (i=0; i < j; i++) { if (st[i].get_gpa() < st[i+1].get_gpa() ) { t = st[i]; st[i] = st[i+1]; st[i+1] = t; swap++; } } } while (swap>0); //end of 2nd sort System.out.println ("\nSorted by student GPA in descending order: " ); for (i=0; i < j+ 1; i++) //print student sorted by GPA { System.out.println (st[i].get_name() + " " + st[i].get_gpa()); } sort = 100; } else {System.out.print ("Invalid option! : "); option = 4;} System.out.print ("Enter 4 for another sort , enter 5 to exit : "); option = Integer.parseInt (stdin.readLine()); if (option!=4) option = 111; // exit the program } } }