Write a java program utilizes 2 arrays, one dimensional, for student names and s
ID: 3941275 • Letter: W
Question
Write a java program utilizes 2 arrays, one dimensional, for student names and student test grades. Prompt 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.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. 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) .
Explanation / Answer
StudentGradeDetails.java
import java.util.Scanner;
public class StudentGradeDetails {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
String names[] = new String[10];
int grades[] = new int[10];
for(int i=0; i<names.length; i++){
System.out.print("Enter first name followed by a space and test grade: ");
names[i] = scan.next();
grades[i] = scan.nextInt();
if(grades[i] <0 || grades[i] > 100){
System.out.print("Invalid grade. ");
i--;
}
}
System.out.println("Before sorting, Student details are: ");
for(int i=0; i<grades.length; i++){
System.out.println(names[i]+" "+grades[i]);
}
sort(names, grades);
System.out.println();
System.out.println("After sorting, Student details are: ");
System.out.println();
for(int i=0; i<grades.length; i++){
System.out.println(names[i]+" "+grades[i]);
}
}
public static void sort(String names[], int grades[]){
int n = grades.length;
int temp = 0;
String tempStr = "";
for(int i=0; i < n; i++){
for(int j=1; j < (n-i); j++){
if(grades[j-1] < grades[j]){
//swap the elements!
temp = grades[j-1];
grades[j-1] = grades[j];
grades[j] = temp;
tempStr = names[j-1];
names[j-1] = names[j];
names[j] = tempStr;
}
}
}
}
}
Output:
Enter first name followed by a space and test grade: Suresh 88
Enter first name followed by a space and test grade: Sekhar 99
Enter first name followed by a space and test grade: Anshu 101
Invalid grade. Enter first name followed by a space and test grade: Anshu 98
Enter first name followed by a space and test grade: Revathi 44
Enter first name followed by a space and test grade: aaa 4
Enter first name followed by a space and test grade: rrr 66 yyy 77
Enter first name followed by a space and test grade: Enter first name followed by a space and test grade: hhhh 8
Enter first name followed by a space and test grade: vvvv 11
Enter first name followed by a space and test grade: gggg 67
Before sorting, Student details are:
Suresh 88
Sekhar 99
Anshu 98
Revathi 44
aaa 4
rrr 66
yyy 77
hhhh 8
vvvv 11
gggg 67
After sorting, Student details are:
Sekhar 99
Anshu 98
Suresh 88
yyy 77
gggg 67
rrr 66
Revathi 44
vvvv 11
hhhh 8
aaa 4
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.