Write the Java source code necessary to build a solution for the problem below:Y
ID: 670175 • Letter: W
Question
Write the Java source code necessary to build a solution for the problem below:You have just taken a new "Teach for America" job in a very rural, impoverished neighborhood. The school system does not provide you with any tools. You decide to write a new gradebook program. You need your new gradebook to allow you to sort the students by NAME or by highest grade on a test so that you can fill out all of the many district reports on your students' progress. Remembering that sometimes Java lets you write one method and use it many times, you decide to write a generic method that will return the maximum and minimum element in a two-dimensional array (your gradebook, NAME, SCORE). Test the program using an array of random integers and again using an array of random names.
Print the array sorted by SCORE, then use it again and print the array sorted by NAME.
Explanation / Answer
Here is the code for the question. Sample output also is attached.
import java.time.chrono.MinguoDate;
public class GradeBook {
//A generic method to sort 2 parallel arrays based on the value in arr1. After sorting the elements are sorted
// in ascending order in arr1.
private static <T1 extends Comparable<T1>,T2 extends Comparable <T2>> void sort(T1[] arr1, T2[] arr2)
{
int minIdx;
T1 tempt1;
T2 tempt2;
for(int i=0; i<arr1.length; i++)
{
minIdx = i;
for(int j=i+1; j< arr1.length ; j++)
{
if(arr1[j].compareTo(arr1[minIdx])<0)
minIdx = j;
}
if(minIdx != i)
{
tempt1 = arr1[i];
tempt2 = arr2[i];
arr1[i] = arr1[minIdx];
arr2[i] = arr2[minIdx];
arr1[minIdx] = tempt1;
arr2[minIdx] = tempt2;
}
}
}
public static void print(String names[],Integer scores[])
{
for(int i=0; i<names.length; i++)
{
System.out.println(names[i]+" "+scores[i]);
}
System.out.println(" ____________ ");
}
public static void main(String[] args) {
String names[]={"John","Alice","Paul","Peter","Douglas","Allen","Chris"};
Integer scores[]={56,89,78,74,92,90,65};
System.out.println("The original list of names and scores is ");
print(names,scores);
sort(names,scores); //sorting on the first argument i.e. names
System.out.println("The list sorted on names in ascending order is ");
print(names,scores);
System.out.println("Minimum is "+names[0]);
System.out.println("Maximum is "+names[names.length-1]+" ");
sort(scores,names); //sorting on the first argument i.e. scores
System.out.println("The list sorted on scores in ascending order is ");
print(names,scores);
System.out.println("Minimum is "+scores[0]);
System.out.println("Maximum is "+scores[scores.length-1]);
}
}
output
The original list of names and scores is
John 56
Alice 89
Paul 78
Peter 74
Douglas 92
Allen 90
Chris 65
____________
The list sorted on names in ascending order is
Alice 89
Allen 90
Chris 65
Douglas 92
John 56
Paul 78
Peter 74
____________
Minimum is Alice
Maximum is Peter
The list sorted on scores in ascending order is
John 56
Chris 65
Peter 74
Paul 78
Alice 89
Allen 90
Douglas 92
____________
Minimum is 56
Maximum is 92
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.