Please write in JAVA Only! You will write a program that utilizes 2 arrays, each
ID: 3775514 • Letter: P
Question
Please write in JAVA Only!
You will write a program that utilizes 2 arrays, each array will be one-dimensional. One array will contain student names, the other student test grades. These will be parallel arrays. With this in mind, complete the following:
Write a program that prompts the user to input a student first name followed by a space and an integer test grade, then the user will press enter and you will prompt them again for a name and a test grade; for a total of 10 students and 10 grades. (Ex: Dawn 100 ) ;
You must check that the user enters a grade that is >= 0 and <= 100. If not, you must prompt the user with an error message and the user must input the grade again.
You must use an array to store the student names and another array to store the student grades. Recall that average is the sum of the values divided by the number of values.
Use a sort() method to sort both arrays (remember that each array holds a name related to a grade. If you move a grade in one array, you should move the name to the corresponding index in the name array)
Write a method classAverage() which should return the average test grade based on all the values in the array of test grades.
Also create a method highestGrade(), which will find the highest grade in the test grades array and print the highest grade and ALL the students’ names that have the highest grade on the test. Test your program using the test data given below and call the classAverage() and highestGrade() methods defined above passing in the arrays. You must use the test data that I give you below.
These are the values to prompt the user with:
Bill 70 Dawn 100 John 90 Samir 100 David 98 Ayesha 100 Tony 99 Lisa 88 Ravi 97 Bob 60
Example of what your output should look like:
The Student Grades are:
Bill 70
Dawn 100
John 90
Samir 100
David 98
Ayesha 100
Tony 99
Lisa 88
Ravi 97
Bob 60
The Average Test Grade is: 90.2
The highest test grade is 100
The lowest test grade is 60
Students scoring the highest grade are Dawn, Samir, Ayesha
The sorted values are : Bob 60
Bill 70
Lisa 88
John 90
Ravi 97
David 98
Tony 99
Ayesha 100
Samir 100
Dawn 100
Explanation / Answer
Ans)
package cheggprojects;
import java.util.Arrays;
import java.util.Scanner;
public class StudentsGragesArrays {
public float classAverage(int[] grd){
int sum = 0 ;
for(int i=0; i<grd.length;i++){
sum = sum + grd[i];
}
float avg = sum/grd.length;
return avg;
}
public int highestGrade(int[] grds){
int max = grds[0];
for(int j=1;j<grds.length;j++){
if(grds[j] > max){
max = grds[j];
}
}
return max;
}
public int classLowGrade(int[] gr){
int low = gr[0];
for(int k=1;k<gr.length;k++){
if(gr[k] < low){
low = gr[k];
}
}
return low;
}
public String[] classHighGradeNames(String[] fnames, int[] grs){
int max = grs[0];
int tot = 0;
for(int l=1;l<grs.length;l++){
if(grs[l] >= max){
max = grs[l];
}
}
for(int p=0;p<grs.length;p++){
if(max == grs[p]){
tot++;
}
}
String[] s = new String[tot];
int count =0;
for(int m=0; m<grs.length;m++){
if(max == grs[m]){
if(count < s.length){
s[count] = fnames[m];
count++;
}
}
}
return s;
}
public void sortArray(String[] names,int[] grades1,int[] gradesOrg){
Arrays.sort(grades1);
int count=0;
System.out.println();
while(count < grades1.length){
for(int x=0; x<gradesOrg.length;x++){
if(grades1[count] == gradesOrg[x]){
System.out.println(names[x]+" "+grades1[count]);
}
}
count++;
}
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String[] firstnames = new String[10];
int[] grades = new int[10];
try{
for(int i=0; i<firstnames.length;i++){
System.out.println("Enter first names and grades of a student should be between 0 and 100: ");
firstnames[i] = sc.next();
grades[i] = sc.nextInt();
}
System.out.println("The student grades are: ");
for(int i=0; i<firstnames.length;i++){
System.out.println(firstnames[i]+" "+grades[i]);
}
StudentsGragesArrays sg = new StudentsGragesArrays();
float avgGrade = sg.classAverage(grades);
System.out.println("The Average Test Grade is: "+avgGrade);
int highGrade = sg.highestGrade(grades);
System.out.println("The highest test grade is: "+highGrade);
int lowGrade = sg.classLowGrade(grades);
System.out.println("The lowest test grade is "+lowGrade);
String[] s = sg.classHighGradeNames(firstnames,grades);
System.out.println("Students scoring the highest grade are: ");
for(int n=0; n<s.length;n++){
System.out.print(s[n]+" ,");
}
sg.sortArray(firstnames,grades,grades);
}catch(Exception e){
System.out.println("**************** "+e.getMessage());
}
}
}
output:
Enter first names and grades of a student should be between 0 and 100:
asd 98
Enter first names and grades of a student should be between 0 and 100:
sdfv 99
Enter first names and grades of a student should be between 0 and 100:
sdfgv 89
Enter first names and grades of a student should be between 0 and 100:
dfgh 99
Enter first names and grades of a student should be between 0 and 100:
sdfg 91
Enter first names and grades of a student should be between 0 and 100:
sdfgb 96
Enter first names and grades of a student should be between 0 and 100:
sdfg 92
Enter first names and grades of a student should be between 0 and 100:
sdrfg 95
Enter first names and grades of a student should be between 0 and 100:
sdfg 99
Enter first names and grades of a student should be between 0 and 100:
asdfg 93
The student grades are:
asd 98
sdfv 99
sdfgv 89
dfgh 99
sdfg 91
sdfgb 96
sdfg 92
sdrfg 95
sdfg 99
asdfg 93
The Average Test Grade is: 95.0
The highest test grade is: 99
The lowest test grade is 89
Students scoring the highest grade are:
sdfv ,dfgh ,sdfg ,
asd 89
sdfv 91
sdfgv 92
dfgh 93
sdfg 95
sdfgb 96
sdfg 98
sdrfg 99
sdfg 99
asdfg 99