0% found this document useful (0 votes)
397 views4 pages

6 Stage6

The document defines a Java class that contains methods for calculating statistics (average, median) and finding the best performing student for different modules. It contains test data for module codes, student IDs, numeric and letter marks. The main method checks the command line arguments and calls the appropriate method to calculate and print the requested statistic for the given module, using either the numeric or letter marks. The other methods implement the calculations by searching for the relevant data, sorting if needed, and performing the appropriate statistical calculations and comparisons.

Uploaded by

hio0o1
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
397 views4 pages

6 Stage6

The document defines a Java class that contains methods for calculating statistics (average, median) and finding the best performing student for different modules. It contains test data for module codes, student IDs, numeric and letter marks. The main method checks the command line arguments and calls the appropriate method to calculate and print the requested statistic for the given module, using either the numeric or letter marks. The other methods implement the calculations by searching for the relevant data, sorting if needed, and performing the appropriate statistical calculations and comparisons.

Uploaded by

hio0o1
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

import java.util.

*; public class Stage6 { // Test Data for the program private static String[] moduleCode = {"CMM3332", "CMM1111", "CMT1230", "CMM1234"}; private static String[] studentID = {"1", "2", "3", "4", "5", "6", "7", "8"}; private static int[][] marks = {{30, 40, 50, 60, 45, 55, 65, 75}, // mark for CMM3332 {80, 70, 60, 40, 34, 56, 78, 90}, // mark for CMM1111 {76, 45, 67, 89, 12, 45, 67, 54}, // mark for CMT1230 {56, 76, 54, 55, 50, 43, 66, 44}}; // mark for CMM1234 private static char[][] marks2 = {{'F', 'D', 'C', 'B', 'C', 'B', 'C', 'A'}, // mark for CMM3332 {'D', 'B', 'A', 'A', 'F', 'B', 'C', 'C'}, // mark for CMM1111 {'D', 'B', 'E', 'B', 'C', 'F', 'F', 'D'}, // mark for CMT1230 {'C', 'B', 'E', 'D', 'C', 'B', 'C', 'A'}}; // mark for CMM1234 public static void main(String[] argv) { if (argv.length != 3) { System.out.println("Usage: java Main5 {letter | numeric } {average | median | beststudent} <module code>"); System.exit(1); } float averageMark; if (argv[0].equals("numeric")) { if (argv[1].equals("average")) { // calculate and print the average mark of module averageMark = calculateModuleAverageMark(argv[2]); System.out.println("The average mark of module " + argv [2] + " = " + averageMark); } else if (argv[1].equals("median")) { // calculate and print the median of the module double medianMark = calculateModuleMedian(argv[2]); System.out.println("The median of module " + argv[2] + " = " + medianMark); } else if (argv[1].equals("beststudent")) { displayBestStudent(argv[2]); } else { System.out.println("Usage: java Main5 {letter | numeric } {average | median | beststudent} <module code>"); System.exit(1); } } else if (argv[0].equals("letter")) { if (argv[1].equals("average")) { // calculate and print the average mark of module averageMark = calculateModuleAverageMark2(argv[2]); System.out.println("The average mark of module " + argv [2] + " = " + averageMark); } else if (argv[1].equals("median")) { // calculate and print the median of the module double medianMark = calculateModuleMedian2(argv[2]); System.out.println("The median of module " + argv[2] + " = " + medianMark); } else if (argv[1].equals("beststudent")) {

displayBestStudent2(argv[2]); } else { System.out.println("Usage: java Main5 {letter | numeric } {average | median | beststudent} <module code>"); System.exit(1); } } } public static float calculateModuleAverageMark(String mCode) { int i; i = getModuleIndex(mCode); // find the index of the module float average = 0.0f; if (i >= 0) { // find the average for (int j = 0; j < marks[i].length; j++) { average += marks[i][j]; } return (average / marks[i].length); } else { return (0.0f); } } public static float calculateModuleAverageMark2(String mCode) { int i; i = getModuleIndex(mCode); // find the index of the module float average = 0.0f; if (i >= 0) { // find the average for (int j = 0; j < marks2[i].length; j++) { average += letterToNumeric(marks2[i][j]); } return (average / marks2[i].length); } else { return (0.0f); } } public static void displayBestStudent(String mCode) { int i, maxMark; i = getModuleIndex(mCode); // find the index of the module if (i >= 0) { maxMark = 0; for (int j = 0; j < marks[i].length; j++) // find the maximum mark { if (marks[i][j] > maxMark) { maxMark = marks[i][j]; } } System.out.println("In module " + mCode + ", the following students perform the best:"); for (int j = 0; j < marks[i].length; j++) // display all students with maximum mark { if (marks[i][j] == maxMark) { System.out.println("Student " + studentID[j]); } } } else { System.out.println("No such module " + mCode);

} } public static void displayBestStudent2(String mCode) { int i, maxMark; i = getModuleIndex(mCode); // find the index of the module if (i >= 0) { maxMark = 0; for (int j = 0; j < marks2[i].length; j++) // find the maximum mark { if (letterToNumeric(marks2[i][j]) > maxMark) { maxMark = letterToNumeric(marks2[i][j]); } } System.out.println("In module " + mCode + ", the following students perform the best:"); for (int j = 0; j < marks2[i].length; j++) // display all students with maximum mark { if (letterToNumeric(marks2[i][j]) == maxMark) { System.out.println("Student " + studentID[j]); } } } else { System.out.println("No such module " + mCode); } } public static float calculateModuleMedian(String mCode) { float medianMark; int i; i = getModuleIndex(mCode); // find the index of the module if (i >= 0) { int[] sortedMarks = marks[i]; Arrays.sort(sortedMarks); if (sortedMarks.length % 2 != 0) { medianMark = sortedMarks[(sortedMarks.length - 1) / 2]; } else { medianMark = (float) (sortedMarks[sortedMarks.length / 2 - 1] + sortedMarks[sortedMarks.length / 2]) / 2; } return medianMark; } else { return (0.0f); } } public static float calculateModuleMedian2(String mCode) { float medianMark; int i; i = getModuleIndex(mCode); // find the index of the module if (i >= 0) { int[] sortedMarks = new int[marks2[i].length]; for (int j = 0; j < marks2[i].length; j++) { sortedMarks[j] = letterToNumeric(marks2[i][j]); }

Arrays.sort(sortedMarks); if (sortedMarks.length % 2 != 0) { medianMark = sortedMarks[(sortedMarks.length - 1) / 2]; } else { medianMark = (float) (sortedMarks[sortedMarks.length / 2 - 1] + sortedMarks[sortedMarks.length / 2]) / 2; } return medianMark; } else { return (0.0f); } } public static int getModuleIndex(String mCode) { int i; for (i = 0; i < moduleCode.length; i++) { // find the index of the module if (moduleCode[i].equals(mCode)) { break; } } if (i < moduleCode.length) { return (i); } else { return (-1); } } public static int letterToNumeric(char grade) { if (grade == 'A') { return (5); } else if (grade == 'B') { return (4); } else if (grade == 'C') { return (3); } else if (grade == 'D') { return (2); } else if (grade == 'E') { return (1); } else { return (0); } } }

You might also like