In: Computer Science
IN JAVA WITH COMMENTS, The assignment: This program inputs the names of 5 students and the (integer) grades they earned in 3 tests. It then outputs the average grade for each student. It also outputs the highest grade a student earned in each test, and the average of all grades in each test.
Your output should look like this:
Mary's average grade was 78.8%
Harry's average grade was 67.7%
etc... :
In test 1 the highest grade was 93% and the average was 89.2%
In test 2........etc
Your main method should be modular.
Write a method that inputs the 5 names into an array (one dimensional), and returns this array to main
Write a method that inputs the 15 (integer) test grades into a (two-dimensional) array, and returns this array to main.
Write a method that calculates the students averages and stores them in an array , and returns this array to main
Write a method that calculates the average grade for each test and stores them in an array, and returns this array to main
Write a method that determines the highest grade for each test and stores this in an array , and returns this array to main. MAIN will print all the information. The printing could be done easily by the methods instead of having to store the information in an array, but this will give you practice in arrays and methods - all useful for the final exam.
Thanks a lot, please include the comments.
Full working code:
import java.util.Scanner; public class TestScores { /* Take input all the names of the students from user and return that array */ public static String[] inputNames() { Scanner sc = new Scanner(System.in); String names[] = new String[5]; for(int i =0 ; i<5; i++) { names[i] = sc.next(); } return names; } //Takes input test scores of 15 test. 3 Test for 5 students i.e 5 X 3 matrix public static int[][] testScores() { Scanner sc = new Scanner(System.in); int [][]scores = new int[5][3]; for(int i = 0 ; i < 5 ; i++) { for(int j = 0 ; j < 3 ; j++) { scores[i][j] = sc.nextInt(); } } return scores; } //Calculate average score of every students grade and returns that array public static double[] averageScores(int [][]scores) { double []avgScore = new double[5]; for(int i = 0 ; i < 5 ; i++) { int sum = 0; for(int j = 0 ; j < 3 ; j++) { sum += scores[i][j]; } avgScore[i] = sum/3; } return avgScore; } //Average test score of every 3 tests. public static double[] averageTestGrade(int [][]scores) { double []averageTestGrade = new double[3]; for(int i = 0 ; i < 3 ; i++) { int sum = 0; for(int j = 0; j < 5 ; j++) { sum = sum + scores[j][i]; } averageTestGrade[i] = sum/5; } return averageTestGrade; } //Returns the array of 3 elements with highest score in individual test public static int[] highestTestScore(int [][]scores) { int highestTestScore[] = new int[3]; for(int i = 0 ; i < 3 ; i++) { int max = scores[0][i]; for(int j = 1; j < 5 ; j++) { if(scores[j][i] > max) max=scores[j][i]; } highestTestScore[i] = max; } return highestTestScore; } //Driver Method where all the static method constructed above are being called wherever necessary public static void main (String[] args) { TestScores s = new TestScores(); String []names = new String[5]; names = s.inputNames(); int testScores[][] = new int [5][3]; testScores = testScores(); double averageScores[] = new double[5]; averageScores = averageScores(testScores); double averageTestGrade[] = new double[3]; averageTestGrade = averageTestGrade(testScores); int highestTestScore[] = new int[3]; highestTestScore = highestTestScore(testScores); //Display students grade for(int i = 0; i< 5; i++) System.out.println(names[i]+"'s average grade was "+averageScores[i]); //Display each test data for(int i = 0 ; i< 3 ; i++) { System.out.println("In Test "+(i+1)+" the highest grade was "+highestTestScore[i]+"% and the average was "+averageTestGrade[i]+"%"); } } }
Input:
Vijay Shivani Kanu Nishi Vivek
70 100 70
80 90 80
90 70 60
30 40 50
80 40 50
Output:
Vijay's average grade was 80.0
Shivani's average grade was 83.0
Kanu's average grade was 73.0
Nishi's average grade was 40.0
Vivek's average grade was 56.0
In Test 1 the highest grade was 90% and the average was 70.0%
In Test 2 the highest grade was 100% and the average was
68.0%
In Test 3 the highest grade was 80% and the average was 62.0