Question:
Java Assignment Help?
gamer3425
2013-04-14 02:20:54 UTC
The prompt is as follows:

Write a program that prompts the user to enter the number of students in a class, the student names and their scores on a test. The program should print the student names (along with their scores) in decreasing order of their scores (use the selection sort as covered in class). Your sort should be implemented as a method (please see the following method header):

Here is my code:

public class Assignment5 {

/**
* @param args
*/
public static void main(String[] args) {

Scanner input = new Scanner(System.in);

System.out.println("Enter the size of the class: ");
int size = input.nextInt();

String[] names = new String[size];
double[] scores = new double[size];
for (int i = 0; i < size; i++) {
System.out.print("Please enter a student name: ");
input.useDelimiter(System.getProperty("line.separator"));
names[i] = input.next();
System.out.print("Please enter " + names[i] + "'s score: ");
scores[i] = input.nextDouble();
}

System.out.println("The class size is " + size);
SelectionSort(scores, names);

}

public static void SelectionSort (double[] score, String[] name) {
Scanner myInput = new Scanner(System.in);
for (int i = 0; i < score.length; i++) {
// This finds the minimum in the list.
double currentMin = score[i]; // Minimum = position in the list.
int currentMinIndex = i; // Index of the current minimum.

for (int j = i + 1; j < score.length; j++) {
if (currentMin < score[j]) {
currentMin = score[j];
currentMinIndex = j;
}
}

if (currentMinIndex != i) {
score[currentMinIndex] = score [i];
score[i] = currentMin;
}
}
System.out.println("Name Score");
for (int i = 0; i < score.length; i++) {
System.out.print(name[i] + score[i]);
System.out.println();
}

}
}

Now the problem is that when it does the sort, it doesn't put the grade with its associated score. What do I do to correct this problem?
Three answers:
husoski
2013-04-14 04:19:26 UTC
When you sort parallel arrays (that's one name for what you're doing), you need to perform the swaps in all arrays when you exchange two elements. So add to your if statement:



if (currentMinIndex != i) {

score[currentMinIndex] = score [i];

score[i] = currentMin;

String temp = names[i]; // swap names[i] with names[currentMinIndex]:

names[i] = names[currentMinIndex];

names[currentMinIndex] = temp;

}
Tyler
2013-04-14 09:29:51 UTC
If you know about classes/objects you should create a new class to store the students. This way you can store both the name and score together as a student object and then create an array of the student objects and sort using their score variables. After it is sorted you can use a toString method in the student class to print the student with their score. In your student class make a toString method, a compare method (that uses the score variable to compare two students) and a getScore method that just returns the score variable. Hope that helps.
anonymous
2013-04-14 09:38:56 UTC
In your sorting you need to modify the "names" array as you do "scores".



But I would suggest object oriented approach to your data storage solution. Either that or use SQL database. I'm no expert at Java but there should be connectivity.



Define a class TestScores that holds variable for "studentName" and "mark", so that later you can expand your app to real-life situation to add a "testName" to this class. Then place TestScores objects into array and sort that one "data-base". That is a fundamental computer problem.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Continue reading on narkive:
Loading...