JAVA programming Write a program that reads the student name/grade input file an
ID: 3760777 • Letter: J
Question
JAVA programming
Write a program that reads the student name/grade input file and puts the name into alphabetical order as the names of a class are placed into a name array. While storing the names, their grades are put into corresponding cells in the grade array. You will need two, two dimensional arrays, one for the names and one for the grades. These will be parallel arrays. Use modules to simplify the top level logic. Your modules must be self-contained and may not use global variables to access data. You may assume that an argument variable that passes data into a module may also receive returned data from that module. After you have read in the names and grades, identify the high and low grades for each class. JAVA programmingExplanation / Answer
import java.util.*;
public class SortingStudents {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter the number of students: ");
int numofstudents = input.nextInt();
String[] names = new String[numofstudents];
int[] array = new int[numofstudents];
for(int i = 0; i < numofstudents; i++) {
System.out.print("Enter the student's name: ");
names[i] = input.next();
System.out.print("Enter the student's score: ");
array[i] = input.nextInt();
}
selectionSort(names, array);
System.out.println(Arrays.toString(names));
}
public static void selectionSort(String[] names, int[] array) {
for(int i = array.length - 1; i >= 1; i--) {
String temp;
int currentMax = array[0];
int currentMaxIndex = 0;
for(int j = 1; j <= i; j++) {
if (currentMax > array[j]) {
currentMax = array[j];
currentMaxIndex = j;
}
}
if (currentMaxIndex != i) {
temp = names[currentMaxIndex];
names[currentMaxIndex] = names[i];
names[i] = temp;
array[currentMaxIndex] = array[i];
array[i] = currentMax;
}
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.