In: Computer Science
For this exercise, you will implement two programs that allow the user to enter grades in a gradebook. The first program will use one‐dimensional arrays, and the second program will use two‐dimensional arrays. The two programs function the same
Write an application that prints out the final grade of the students in a class and the average for the whole class. There are a total of 3 quizzes. You will need 4 arrays:
1. An array of type int to store all the ID's
2. An array of type double to store all scores for quiz 1
3. An array of type double to store all scores for quiz 2
4. An array of type double to store all scores for quiz 3
How to proceed:
1. Ask the user how many students are in the class, so you can set the length of all the arrays.
2. Allocate 4 arrays that will store the data.
3. Use a FOR loop to retrieve and store all the data.
4. Use another FOR loop to
a. Output the final score for each student.
b. Keep track of all scores to later compute the average for the class.
5. Calculate and Output the average for the class.
Format all floating‐point numbers to 2 decimal places
Your program MUST receive user input as shown in the sample output.
Please leave comments on the program so i can give you a thumbs up :)!
Also, this is for java language.
CODE:
USING ONE DIMENSIONAL ARRAY:
import java.util.Scanner;
public class Average1D {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("How many Students in the class ?");
//Getting total number of students
int numberOfStudents = input.nextInt();
//Declaring Arrays for studentIds,quiz1,qui2,quiz3 of
size n
int[] studentIds = new int[numberOfStudents];
double[] quiz1 = new double[numberOfStudents];
double[] quiz2 = new double[numberOfStudents];
double[] quiz3 = new double[numberOfStudents];
//Getting n students id,quiz1,quiz2,quiz3 data
for (int i = 0; i < numberOfStudents; i++) {
System.out.println("Enter The Student Id: ");
studentIds[i] = input.nextInt();
System.out.println("Enter score for Quiz 1: ");
quiz1[i] = input.nextDouble();
System.out.println("Enter score for Quiz 2: ");
quiz2[i] = input.nextDouble();
System.out.println("Enter score for Quiz 3: ");
quiz3[i] = input.nextDouble();
}
//For calculating the average of n students;
double[] average = new double[numberOfStudents];
double classTotal = 0.0;
for (int i = 0; i < numberOfStudents; i++) {
//average = (Sum of items)/(No of
items)
average[i] = (quiz1[i] + quiz2[i] + quiz3[i]) / 3;
classTotal = classTotal + average[i];
}
//Displaying the average of each student
for (int i = 0; i < numberOfStudents; i++) {
System.out.println("ID " + studentIds[i] + " - Final Grade: " + average[i]);
}
//Displaying the totalClassAverage
System.out.println("Class Avergae: " +
classTotal/numberOfStudents);
}
}
Sampel Output:
How many Students in the class ?
4
Enter The Student Id:
111
Enter score for Quiz 1:
100
Enter score for Quiz 2:
95
Enter score for Quiz 3:
90
Enter The Student Id:
222
Enter score for Quiz 1:
90
Enter score for Quiz 2:
85
Enter score for Quiz 3:
80
Enter The Student Id:
333
Enter score for Quiz 1:
100
Enter score for Quiz 2:
100
Enter score for Quiz 3:
100
Enter The Student Id:
444
Enter score for Quiz 1:
90
Enter score for Quiz 2:
90
Enter score for Quiz 3:
90
ID 111 - Final Grade: 95.0
ID 222 - Final Grade: 85.0
ID 333 - Final Grade: 100.0
ID 444 - Final Grade: 90.0
Class Avergae: 92.5
USING TWO DIMENSIONAL ARRAY:
class Average2D {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("How many Students in the class ?");
//Getting total number of students
int numberOfStudents = input.nextInt();
//Declaring Array to store studentId of size n
int[] studentIds = new int[numberOfStudents];
//Declaring 2D Array, Since we have 3 quiz test marks of
size(nxNo.of students).
double[][] quiz = new double[3][numberOfStudents];
for (int i = 0; i < numberOfStudents; i++) {
System.out.println("Enter The Student Id: ");
//Getting student ID
studentIds[i] = input.nextInt();
for (int j = 0; j < 3; j++) {
System.out.println("Enter score for Quiz " + j + 1 + ":");
//Respective student 3 quiz test marks
quiz[j][i] = input.nextDouble();
}
}
double[] average = new double[numberOfStudents];
double classTotal = 0.0;
for (int i = 0; i < numberOfStudents; i++) {
//Calculating average of three quiz test marks
average[i] = (quiz[0][i] + quiz[1][i] + quiz[2][i]) /
3;
classTotal = classTotal + average[i];
}
//Displaying the average of each student
for (int i = 0; i < numberOfStudents; i++) {
System.out.println("ID " + studentIds[i] + " - Final Grade: " + average[i]);
}
//Displaying the totalClassAverage
System.out.println("Class Avergae: " + classTotal /
numberOfStudents);
}
}
Sample Output:
How many Students in the class ?
4
Enter The Student Id:
111
Enter score for Quiz 1:
100
Enter score for Quiz 2:
90
Enter score for Quiz 3:
95
Enter The Student Id:
222
Enter score for Quiz 1:
90
Enter score for Quiz 2:
80
Enter score for Quiz 3:
85
Enter The Student Id:
333
Enter score for Quiz 1:
100
Enter score for Quiz 2:
100
Enter score for Quiz 3:
100
Enter The Student Id:
444
Enter score for Quiz 1:
90
Enter score for Quiz 2:
90
Enter score for Quiz 3:
90
ID 111 - Final Grade: 95.0
ID 222 - Final Grade: 85.0
ID 333 - Final Grade: 100.0
ID 444 - Final Grade: 90.0
Class Avergae: 92.5