import java.util.*; import java.io.*; public class StudentInfo { public static void main(String[] args) throws FileNotFoundException { String[] names = new String[100]; int[] grades = new int[100]; int i = 0; String choice; Scanner prompt = new Scanner(System.in); Scanner data = new Scanner(new File("grades.txt")); int size = readData(data, names, grades); grades = resize(grades, size); names = resize(names, size); System.out.println(size); printPlain(names, grades); do { printMenu(); System.out.print("Enter Choice: "); choice = prompt.next(); if (choice.equalsIgnoreCase("C")) { System.out.println("Avg =" + calcAvg(grades)); } else if (choice.equalsIgnoreCase("P")) { printPlain(names, grades); } else if (choice.equalsIgnoreCase("L")) { int lowest = findLowest(grades); System.out.println("Lowest = " + names[lowest] + "\t" + grades[lowest]); } else if (choice.equalsIgnoreCase("H")) { int highest = findHighest(grades); System.out.println("Highest = " + names[highest] + "\t" + grades[highest]); } else if (choice.equalsIgnoreCase("S")) { search4Student(prompt, names, grades); } else if (choice.equalsIgnoreCase("V")) { printAbove(prompt, names, grades); } else if (choice.equalsIgnoreCase("B")) { printBelow(prompt, names, grades); } else if (choice.equalsIgnoreCase("N")) { selectionSort(names, grades); } else if (choice.equalsIgnoreCase("G")) { selectionSort(grades, names); } } while (!choice.equalsIgnoreCase("Q")); System.out.println("\nDone...goodbye."); } //---------------------------------------------------------- // Count all the names and scores of students with a grade // above or equal to an amount entered by the user. Return // the count. public static void printAbove(Scanner prompt, String[] names, int[] grades) { int count = 0; System.out.print("Enter a grade: "); int grd = prompt.nextInt(); for (int i = 0; i < grades.length; i++) { if (grades[i] >= grd) { count++; } } System.out.println("\nThere are " + count + " grades at or above " + grd); } //---------------------------------------------------------- // Print all the names and scores of students with a grade // above or equal to an amount entered by the user. public static void printBelow(Scanner prompt, String[] names, int[] grades) { int count = 0; System.out.print("Enter a grade: "); int grd = prompt.nextInt(); for (int i = 0; i < grades.length; i++) { if (grades[i] <= grd) { count++; } } System.out.println("\nThere are " + count + " grades at or below " + grd); } //---------------------------------------------------------- // Search for a specific student and return an index if they // are in the file. Returns an index to the student if found, // -1 otherwise. public static void search4Student(Scanner prompt, String[] names, int[] grades) { System.out.print("Enter a student name to search for: "); String inName = prompt.next(); int location = isStudent(inName, names); if (location == -1) { System.out.println("Student not found."); } else { System.out.printf("%-10s ", names[location]); System.out.printf("%-10d\n", grades[location]); } } //---------------------------------------------------------- // Search for a specific name in the names array. If found, // return the index of the name's location in the array. If // not found, return -1. public static int isStudent(String name, String[] names) { for (int i = 0; i < names.length; i++) { if (name.equals(names[i])) { return i; } } return -1; } //---------------------------------------------------------- // Locate the highest grade in the array. // Return the index of the lowest grade. public static int findHighest (int[] grades) { int highest = 0; for (int i = 1; i < grades.length; i++) { if (grades[i] > grades[highest]) { highest = i; } } return highest; } //---------------------------------------------------------- // Locate the lowest grade in the array. // Return the index of the lowest grade. public static int findLowest (int[] grades) { int lowest = 0; for (int i = 1; i < grades.length; i++) { if (grades[i] < grades[lowest]) { lowest = i; } } return lowest; } //--------------------------------------------------------- // Print out both arrays in two columns in a simple format. public static void printPlain(String[] names, int[] grades) { System.out.println(); for (int i = 0; i < grades.length; i ++ ) { System.out.printf("%-10s ", names[i]); System.out.printf("%-10d\n", grades[i]); // System.out.println(names[i] + "\t" + grades[i]); } System.out.println(); } //--------------------------------------------------------- // Calculate and return the average grade. public static double calcAvg(int[] grades) { int sum = 0; for (int i = 0; i < grades.length; i++) { sum += grades[i]; } return (double) sum / grades.length; } //---------------------------------------------------- // This method resizes an array. public static int[] resize(int[] a, int newSize) { int[] temp = new int[newSize]; for (int i = 0; i < newSize; i++) { temp[i] = a[i]; } return temp; } //---------------------------------------------------- // This method resizes an array. public static String[] resize(String[] a, int newSize) { String[] temp = new String[newSize]; for (int i = 0; i < newSize; i++) { temp[i] = a[i]; } return temp; } //---------------------------------------------------- // Read in data from the file into two arrays. // Return a count of the number of pairs read. public static int readData(Scanner data, String[] names, int[] grades) { int count = 0; while (data.hasNextLine()) { names[count] = data.next(); grades[count] = data.nextInt(); count++; } return count; } //----------------------------------------------------- // Output the menu. public static void printMenu() { System.out.println(); System.out.println("C - Calculate Average"); System.out.println("H - Print Highest Grade"); System.out.println("L - Print Lowest Grade"); System.out.println("S - Search for Student"); System.out.println("G - Print in Order by Grade"); System.out.println("N - Print in Order by Name"); System.out.println("P - Print Names and Grades"); System.out.println("V - Print Names and Grades Above"); System.out.println("B - Print Names and Grades Below"); System.out.println("Q - Quit"); System.out.println(); } //-------------------------------------------------- // Sorts by names public static void selectionSort(String[] n, int[] g) { int targetLoc; for (int i=g.length-1; i>0; i--) { targetLoc = findLargest(n, i+1); swap(n, g, targetLoc, i); } } //-------------------------------------------------- // Sorts by grades public static void selectionSort(int[] g, String[] n) { int targetLoc; for (int i=g.length-1; i>0; i--) { targetLoc = findLargest(g, i+1); swap(n, g, targetLoc, i); } } //-------------------------------------------------- // locates the largest value up through element size public static int findLargest(String[] n, int size) { int loc = 0; for (int i=1; i < size; i++) { if (n[i].compareTo(n[loc]) > 0) { loc = i; } } return loc; } //-------------------------------------------------- // locates the largest value up through element size public static int findLargest(int[] x, int size) { int loc = 0; for (int i=1; i < size; i++) { if (x[loc] < x[i]) { loc = i; } } return loc; } //----------------------------------------- // Swaps element i with element j public static void swap (String[] n, int[] g, int i, int j) { String tempN = n[i]; n[i] = n[j]; n[j] = tempN; int tempG = g[i]; g[i] = g[j]; g[j] = tempG; } }