What is the Java code for this problem? You will write a program that utilizes 2
ID: 3824183 • Letter: W
Question
What is the Java code for this problem?
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. Once you have all the student names and grades, you are to find the classAverage() test grade and the highestGrade() and lowestGrade(). 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)
You will write a method classAverage() which should return the average test grade based on all the values in the array of test grades. You are also expected to 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.
Bill 70
Dawn 100
John 90
Samir 100
David 98
Ayesha 100
Tony 99
Lisa 88
Ravin 97
Bob 60
Output the stored values in the arrays, class average and highest test score
Explanation / Answer
This Program will prompt for Entering next student, if y is entered it would display to enter another student name, if n is entered program would display required out put and exits:
StudentGrades.java
import java.util.Scanner;
public class StudentGrades {
static String names[] = new String[100];
static int grades[] = new int[100];
static Scanner in = new Scanner(System.in);
static int num = 0;
public static void main(String[] args) {
System.out.println("Enter Student Name and Grade ");
while (in.hasNextLine()) {
String name = in.next();
int grade = in.nextInt();
if (grade < 0 || grade > 100) {
System.err.println("Entered Grade is not valid please enter value between 1 and 99");
grade = in.nextInt();
}
names[num] = name;
grades[num] = grade;
num++;
System.out.println("Enter Y to add another student Detail or N to exit");
String check = in.next();
if (check.equalsIgnoreCase("y")) {
System.out.println("Enter Student Name and Grade ");
} else if (check.equalsIgnoreCase("n")) {
System.out.println("Store Values....");
for (int i = 0; i < num; i++) {
System.out.println(names[i] + " " + grades[i]);
}
System.out.println(" Class Avegare: " + classAverage());
highestScore();
sort();
System.exit(1);
}
}
in.close();
}
public static int classAverage() {
int sum = 0;
int avg = 0;
for (int i = 0; i < grades.length; i++) {
sum += grades[i];
}
avg = sum / num;
return avg;
}
public static int highestScore() {
int highest = grades[0];
for (int i = 1; i < grades.length; i++) {
if (grades[i] > highest) {
highest = grades[i];
}
}
System.out.println("Highest Score: " + highest);
System.out.println(" Students with Highest Grade: ");
for (int i = 1; i < num; i++) {
if (grades[i] == highest) {
System.out.println(names[i]+" "+grades[i]);
}
}
return highest;
}
public static void sort(){
int temp=0;
String tempName;
for (int i = 0; i <= num; i++)
{
for (int j = i + 1; j <= num; j++)
{
if (grades[i] < grades[j])
{
temp = grades[i];
grades[i] = grades[j];
grades[j] = temp;
tempName=names[i];
names[i]=names[j];
names[j]=tempName;
}
}
}
System.out.println(" Sorted Names: ");
for (int i = 0; i < num; i++) {
System.out.println(names[i]+" "+grades[i]);
}
}
}
Sample Output:
Enter Student Name and Grade
Bill 70
Enter Y to add another student Detail or N to exit
y
Enter Student Name and Grade
Dawn 100
Enter Y to add another student Detail or N to exit
y
Enter Student Name and Grade
John 90
Enter Y to add another student Detail or N to exit
y
Enter Student Name and Grade
Samir 100
Enter Y to add another student Detail or N to exit
y
Enter Student Name and Grade
David 98
Enter Y to add another student Detail or N to exit
y
Enter Student Name and Grade
Ayesha 100
Enter Y to add another student Detail or N to exit
y
Enter Student Name and Grade
Tony 99
Enter Y to add another student Detail or N to exit
y
Enter Student Name and Grade
Lisa 88
Enter Y to add another student Detail or N to exit
y
Enter Student Name and Grade
Ravin 97
Enter Y to add another student Detail or N to exit
y
Enter Student Name and Grade
Bob 60
Enter Y to add another student Detail or N to exit
n
Store Values....
Bill 70
Dawn 100
John 90
Samir 100
David 98
Ayesha 100
Tony 99
Lisa 88
Ravin 97
Bob 60
Class Avegare: 90
Highest Score: 100
Students with Highest Grade:
Dawn 100
Samir 100
Ayesha 100
Sorted Names:
Dawn 100
Samir 100
Ayesha 100
Tony 99
David 98
Ravin 97
John 90
Lisa 88
Bill 70
Bob 60
Program with out prompting for Y/N after entering each student and having fixed number of students as 10.
StudentGrades.java
import java.util.Scanner;
public class StudentGrades {
static String names[] = new String[100];
static int grades[] = new int[100];
static Scanner in = new Scanner(System.in);
static int num = 0;
static int max = 10;
public static void main(String[] args) {
System.out.println("Enter Student Name and Grade ");
while (in.hasNextLine()) {
String name = in.next();
int grade = in.nextInt();
if (grade < 0 || grade > 100) {
System.err.println("Entered Grade is not valid please enter value between 1 and 99");
grade = in.nextInt();
}
names[num] = name;
grades[num] = grade;
num++;
if (num<max) {
System.out.println("Enter Student Name and Grade ");
} else{
System.out.println("Store Values....");
for (int i = 0; i < num; i++) {
System.out.println(names[i] + " " + grades[i]);
}
System.out.println(" Class Avegare: " + classAverage());
highestScore();
sort();
System.exit(1);
}
}
in.close();
}
public static int classAverage() {
int sum = 0;
int avg = 0;
for (int i = 0; i < grades.length; i++) {
sum += grades[i];
}
avg = sum / num;
return avg;
}
public static int highestScore() {
int highest = grades[0];
for (int i = 1; i < grades.length; i++) {
if (grades[i] > highest) {
highest = grades[i];
}
}
System.out.println("Highest Score: " + highest);
System.out.println(" Students with Highest Grade: ");
for (int i = 1; i < num; i++) {
if (grades[i] == highest) {
System.out.println(names[i]+" "+grades[i]);
}
}
return highest;
}
public static void sort(){
int temp=0;
String tempName;
for (int i = 0; i <= num; i++)
{
for (int j = i + 1; j <= num; j++)
{
if (grades[i] < grades[j])
{
temp = grades[i];
grades[i] = grades[j];
grades[j] = temp;
tempName=names[i];
names[i]=names[j];
names[j]=tempName;
}
}
}
System.out.println(" Sorted Names: ");
for (int i = 0; i < num; i++) {
System.out.println(names[i]+" "+grades[i]);
}
}
}
Sample Output:
Enter Student Name and Grade
Bill 70
Enter Student Name and Grade
Dawn 100
Enter Student Name and Grade
John 90
Enter Student Name and Grade
Samir 100
Enter Student Name and Grade
David 98
Enter Student Name and Grade
Ayesha 100
Enter Student Name and Grade
Tony 99
Enter Student Name and Grade
Lisa 88
Enter Student Name and Grade
Ravin 97
Enter Student Name and Grade
Bob 60
Store Values....
Bill 70
Dawn 100
John 90
Samir 100
David 98
Ayesha 100
Tony 99
Lisa 88
Ravin 97
Bob 60
Class Avegare: 90
Highest Score: 100
Students with Highest Grade:
Dawn 100
Samir 100
Ayesha 100
Sorted Names:
Dawn 100
Samir 100
Ayesha 100
Tony 99
David 98
Ravin 97
John 90
Lisa 88
Bill 70
Bob 60
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.