PART ONE: Class Gradebook1D Write an application that prints out the final grade
ID: 3553464 • Letter: P
Question
PART ONE:
Class Gradebook1D
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 the all 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 finalscore for each student.
b. Keep track of allscoresto later compute the average forthe class.
5. Calculate and Output the average for the class.
Format all floating?point numbers to 2 decimal places.
PART TWO:
The class Gradebook2D will produce the same output of the class Gradebook1D, but the implementation will
be slightly different.
You will need to use only two arrays:
? One array of type int to store all the ID's (just like the previous class).
? One two?dimensional array of type double to store all the scores for all quizzes.
Note: The two dimensional array can be represented also with the rows being the students and the columns
being the quizzes.
How to proceed:
1. Declare the number of quizzes as a constant, outside the main method. (Recall that identifiers for
constants are all in CAPITAL_LETTERS.)
2. Ask the user how many students are in the class, so you can set the length of the all arrays.
3. Allocate 2 arrays, one one?dimensional and the other two?dimensional, that will store the data.
4. Use two nested FOR loops to retrieve and store all the data.
5. Use another two nested FOR loops to
a. Output the final score for each student.
b. Keep track of all scores to later compute the average for the class.
6. Calculate and Output the average for the class.
Format all floating?point numbers to 2 decimal places.
Explanation / Answer
class Gradebook1D
import java.util.*;
public class Gradebook1D
{
public static void main(String args[])
{
Scanner scan = new Scanner(System.in);
System.out.println("Enter the number of students in the class");
int numberOfStudents=scan.nextInt();
//allocation of arrays that will store the data
int[] studentID=new int[numberOfStudents];
double[] marksQuiz1=new double[numberOfStudents];
double[] marksQuiz2=new double[numberOfStudents];
double[] marksQuiz3=new double[numberOfStudents];
for(int i=0;i<numberOfStudents;i++)
{
System.out.print("Enter student ID:");
studentID[i]=scan.nextInt();
System.out.print("Enter score for Quiz 1:");
marksQuiz1[i]=(double)(scan.nextInt());
System.out.print("Enter score for Quiz 2:");
marksQuiz2[i]=(double)(scan.nextInt());
System.out.print("Enter score for Quiz 3:");
marksQuiz3[i]=(double)(scan.nextInt());
}
double[] finalScores=new double[numberOfStudents];
for(int i=0;i<numberOfStudents;i++)
{
double finalGrade=(marksQuiz1[i]+marksQuiz3[i]+marksQuiz2[i])/3;
finalGrade=Math.round(finalGrade*100)/100.0;
System.out.println("ID "+studentID[i]+" - Final Grade: "+finalGrade);
finalScores[i]=finalGrade;
}
double classAverage=computeAverageOfClass(finalScores,numberOfStudents);
System.out.println("Class Average: "+classAverage);
}
public static double computeAverageOfClass(double[] finalScores,int numberOfStudents)
{
double sum=0;
for(int i=0;i<numberOfStudents;i++)
{
sum=sum+finalScores[i];
}
double classAverage=sum/numberOfStudents;
classAverage=Math.round(classAverage*100)/100.0;
return classAverage;
}
}
class Gradebook2D
import java.util.*;
public class Gradebook2D
{
private static final int NUMBER_OF_QUIZZES=3;
public static void main(String args[])
{
Scanner scan = new Scanner(System.in);
System.out.println("Enter the number of students in the class");
int numberOfStudents=scan.nextInt();
//allocation of arrays that will store the data
int[] studentID=new int[numberOfStudents]; //one dimensional array
double[][] marksQuiz=new double[numberOfStudents][NUMBER_OF_QUIZZES];//double dimensional array to store the marks of all the students
for(int i=0;i<numberOfStudents;i++)
{
System.out.print("Enter student ID:");
studentID[i]=scan.nextInt();
for(int j=0;j<NUMBER_OF_QUIZZES;j++){
System.out.print("Enter score for Quiz "+(j+1)+" :");
marksQuiz[i][j]=(double)(scan.nextInt());
}
}
double[] finalScores=new double[numberOfStudents];
for(int i=0;i<numberOfStudents;i++)
{
double finalGrade=0.0;
for(int j=0;j<NUMBER_OF_QUIZZES;j++)
{
finalGrade=finalGrade+marksQuiz[i][j];
}
finalGrade=finalGrade/NUMBER_OF_QUIZZES;
finalGrade=Math.round(finalGrade*100)/100.0;
System.out.println("ID "+studentID[i]+" - Final Grade: "+finalGrade);
finalScores[i]=finalGrade;
}
double classAverage=computeAverageOfClass(finalScores,numberOfStudents);
System.out.println("Class Average: "+classAverage);
}
public static double computeAverageOfClass(double[] finalScores,int numberOfStudents)
{
double sum=0;
for(int i=0;i<numberOfStudents;i++)
{
sum=sum+finalScores[i];
}
double classAverage=sum/numberOfStudents;
classAverage=Math.round(classAverage*100)/100.0;
return classAverage;
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.