In: Computer Science
java programming
write a program with arrays to ask the first name, last name, middle initial, IDnumber and 3 test scores of 10 students.
calculate the average of the 3 test scores. show the highest class average and the lowest class average. also show the average of the whole class.
please use basic codes and arrays with loops the out put should look like this:
sample output
first name middle initial last name ID test score1 test score2 test score3
dhdh d djddj. 3456 20 80. 67
the class average is 80
the highest class average is80
the lowest class average is 80
please use printf to format the output
import java.util.*;
class Arrays
{
public static void main (String[] args)
{
Scanner input = new
Scanner(System.in);
double[] testScore = new
double[3];
String[] firstName = new
String[10];
String[] lastName = new
String[10];
String[] middleInitial = new
String[10];
int[] idNum = new int[10];
double[] avg = new
double[10];
double
highest,lowest,average;
highest = 0;
lowest = 999;
average = 0;
System.out.println("Enter the
number of students : ");
int n = input.nextInt();
for(int i=0;i<n;i++)
{
System.out.println("Enter first
name : ");
firstName[i] = input.next();
System.out.println("Enter middle
initial : ");
middleInitial[i] =
input.next();
System.out.println("Enter last name
:");
lastName[i] = input.next();
System.out.println("Enter Id Number
: ");
idNum[i] = input.nextInt();
System.out.println("Enter the three
test scores of the student : ");
for(int j=0;j<3;j++)
{
testScore[j] =
input.nextDouble();
avg[i] = avg[i] +
testScore[j];
}
avg[i] = avg[i]/3;
if(highest < avg[i])
highest = avg[i];
if(lowest > avg[i])
lowest = avg[i];
average = average + avg[i];
}
average = average/n;
System.out.printf("\nThe class average is %.2f",
average);
System.out.printf("\nThe highest class average is
%.2f",highest);
System.out.printf("\nThe lowest class average is
%.2f",lowest);
}
}
Output:
Enter the number of students : 5 Enter first name : Garry Enter middle initial : G. Enter last name :Trump Enter Id Number : 1001 Enter the three test scores of the student : 80 94 95 Enter first name : Bran Enter middle initial : A. Enter last name :Aniston Enter Id Number : 1003 Enter the three test scores of the student : 60 90 95 Enter first name : Adam Enter middle initial : K. Enter last name :Lawrence Enter Id Number : 1002 Enter the three test scores of the student : 70 90 95 Enter first name : Anish Enter middle initial : L. Enter last name :Gupta Enter Id Number : 1004 Enter the three test scores of the student : 90 90 95 Enter first name : Smith Enter middle initial : K. Enter last name :Cooper Enter Id Number : 1005 Enter the three test scores of the student : 80 60 55 The class average is 82.60 The highest class average is 91.67 The lowest class average is 65.00
Do ask if any doubt. Please upvote.