Write a program that allows the user to enter students names followed by their t
ID: 3555516 • Letter: W
Question
Write a program that allows the user to enter students names followed by their test scores and outputs the following information(assume that the maximum nmber of students in the class is 50; if the number of students is less than 50, to indicate the end of input data, after entering the last students data, on a line by itself, hold the ctrl key/press z and then press the Enter key):
a) Class average
b) Names of all the students whose test scores are below the class average, with an appropriate message( youre below class average)
c) Highest test score and the names of all the students having the highest score.
Use methods.
Explanation / Answer
import java.util.Scanner;
// for double formatting
import java.text.DecimalFormat;
public class StudentScore
{
//---------------------------------------------------------
// Calculate student score and basic report
//---------------------------------------------------------
public static void main(String[] args)
{
// an array of double to store student's score
double [] studentscore = new double[50];
// an array of String object to store student name
String [] studentname = new String[50];
double studentavg = 0.0, sumscore = 0.0, averagescore = 0.0, highestscore =0.0;
// index
int i = 0, j = 0, k = 0, counter = 1;
Scanner readname = new Scanner(System.in);
Scanner readscore = new Scanner(System.in);
// prompt for inputs: Student name and score
System.out.println("Enter student name and score, 'Q' & 0 to stop: ");
// read and store student name and score for 1st input...
studentname[i] = readname.nextLine();
studentscore[i] = readscore.nextDouble();
// read and store next student name and score
do
{
i++;
counter++;
System.out.println("Enter student name and score, 'Q/q' & 0 to stop: ");
studentname[i] = readname.nextLine();
studentscore[i] = readscore.nextDouble();
// until the Q or q and 0 are entered...
} while(((studentname[i].compareTo("Q") != 0) || (studentname[i].compareTo("q") != 0)) && (studentscore[i] != 0));
// formatting the double output to two decimal places
DecimalFormat fmt = new DecimalFormat("0.##");
// some cosmetic formatting...
System.out.println("=================REPORT====================");
System.out.println("Student Name Score");
System.out.println("------------ -----");
// set initial value of the highest score to the 1st array element
// and then compare 1 by 1 in the for loop...
highestscore = studentscore[0];
for(k=0;k<=i-1;k++)
{
// print all the student names and their respective scores
System.out.println(studentname[k]+" "+fmt.format(studentscore[k]));
// summing up all the score for average calculation
sumscore = sumscore + studentscore[k];
// determining the highest score
if(highestscore < studentscore[k])
highestscore = studentscore[k];
}
// calculate class average score
System.out.println();
System.out.println("The number of student is " + (counter-1));
averagescore = sumscore / (counter-1);
System.out.print("The average score for this class is " + fmt.format(averagescore));
// some cosmetic formatting...
System.out.println();
System.out.println("================================================");
System.out.println("Below The Average Students! Work Harder! ");
System.out.println("================================================");
System.out.println("Student Name Score");
System.out.println("------------ -----");
// list down all the below average students
for(k=0;k<=i-1;k++)
{
if(studentscore[k] < averagescore)
System.out.println(studentname[k] + " " + fmt.format(studentscore[k]));
}
// some cosmetic formatting...
System.out.println("================================================");
System.out.println("Top Scorer Student! Congratulation!");
System.out.println("================================================");
System.out.println("Student Name Score");
System.out.println("------------ -----");
// list down all the highest mark students
for(k=0;k<=i-1;k++)
{
if(studentscore[k] == highestscore)
System.out.println(studentname[k] + " " + fmt.format(studentscore[k]));
}
}
}
An output samples:
Enter student name and score, 'Q' & 0 to stop:
Jodie Foster
79.50
Enter student name and score, 'Q/q' & 0 to stop:
Ahmad Nazri
99.9
Enter student name and score, 'Q/q' & 0 to stop:
Jaya Sakti
80.35
Enter student name and score, 'Q/q' & 0 to stop:
Laila Abu
58.9
Enter student name and score, 'Q/q' & 0 to stop:
Irene Khoh
65.5
Enter student name and score, 'Q/q' & 0 to stop:
Azmir Khan
45.30
Enter student name and score, 'Q/q' & 0 to stop:
John Jr.
99.9
Enter student name and score, 'Q/q' & 0 to stop:
Mikerisan
55.70
Enter student name and score, 'Q/q' & 0 to stop:
q
0
=================REPORT====================
Student Name Score
------------ -----
Jodie Foster 79.5
Ahmad Nazri 99.9
Jaya Sakti 80.35
Laila Abu 58.9
Irene Khoh 65.5
Azmir Khan 45.3
John Jr. 99.9
Mikerisan 55.7
The number of student is 8
The average score for this class is 73.13
================================================
Below The Average Students! Work Harder!
================================================
Student Name Score
------------ -----
Laila Abu 58.9
Irene Khoh 65.5
Azmir Khan 45.3
Mikerisan 55.7
================================================
Top Scorer Student! Congratulation!
================================================
Student Name Score
------------ -----
Ahmad Nazri 99.9
John Jr. 99.9
import java.util.Scanner;
// for double formatting
import java.text.DecimalFormat;
public class StudentScore
{
//---------------------------------------------------------
// Calculate student score and basic report
//---------------------------------------------------------
public static void main(String[] args)
{
// an array of double to store student's score
double [] studentscore = new double[50];
// an array of String object to store student name
String [] studentname = new String[50];
double studentavg = 0.0, sumscore = 0.0, averagescore = 0.0, highestscore =0.0;
// index
int i = 0, j = 0, k = 0, counter = 1;
Scanner readname = new Scanner(System.in);
Scanner readscore = new Scanner(System.in);
// prompt for inputs: Student name and score
System.out.println("Enter student name and score, 'Q' & 0 to stop: ");
// read and store student name and score for 1st input...
studentname[i] = readname.nextLine();
studentscore[i] = readscore.nextDouble();
// read and store next student name and score
do
{
i++;
counter++;
System.out.println("Enter student name and score, 'Q/q' & 0 to stop: ");
studentname[i] = readname.nextLine();
studentscore[i] = readscore.nextDouble();
// until the Q or q and 0 are entered...
} while(((studentname[i].compareTo("Q") != 0) || (studentname[i].compareTo("q") != 0)) && (studentscore[i] != 0));
// formatting the double output to two decimal places
DecimalFormat fmt = new DecimalFormat("0.##");
// some cosmetic formatting...
System.out.println("=================REPORT====================");
System.out.println("Student Name Score");
System.out.println("------------ -----");
// set initial value of the highest score to the 1st array element
// and then compare 1 by 1 in the for loop...
highestscore = studentscore[0];
for(k=0;k<=i-1;k++)
{
// print all the student names and their respective scores
System.out.println(studentname[k]+" "+fmt.format(studentscore[k]));
// summing up all the score for average calculation
sumscore = sumscore + studentscore[k];
// determining the highest score
if(highestscore < studentscore[k])
highestscore = studentscore[k];
}
// calculate class average score
System.out.println();
System.out.println("The number of student is " + (counter-1));
averagescore = sumscore / (counter-1);
System.out.print("The average score for this class is " + fmt.format(averagescore));
// some cosmetic formatting...
System.out.println();
System.out.println("================================================");
System.out.println("Below The Average Students! Work Harder! ");
System.out.println("================================================");
System.out.println("Student Name Score");
System.out.println("------------ -----");
// list down all the below average students
for(k=0;k<=i-1;k++)
{
if(studentscore[k] < averagescore)
System.out.println(studentname[k] + " " + fmt.format(studentscore[k]));
}
// some cosmetic formatting...
System.out.println("================================================");
System.out.println("Top Scorer Student! Congratulation!");
System.out.println("================================================");
System.out.println("Student Name Score");
System.out.println("------------ -----");
// list down all the highest mark students
for(k=0;k<=i-1;k++)
{
if(studentscore[k] == highestscore)
System.out.println(studentname[k] + " " + fmt.format(studentscore[k]));
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.