Results 1 to 6 of 6
  1. #1
    son012189 is offline Member
    Join Date
    Oct 2010
    Posts
    38
    Rep Power
    0

    Cool Sort Student Names and Grades

    Write a program that prompts the user to enter the number of students, the students' names, and their scores, and prints student names in decreasing order of their scores.

    Here is my code:
    Java Code:
    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();
    		int[] array = new int[numofstudents];
    		for(int i = 0; i < numofstudents; i++) {
    			System.out.print("Enter the student's name: ");
    			String name = input.next();
    			System.out.print("Enter the student's score: ");
    			double score = input.nextDouble();
    		}
    		selectionSort(array);
    	}
    	public static void selectionSort(int[] array) {
    		for(int i = array.length - 1; i >= 1; i--) {
    			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) {
    				array[currentMaxIndex] = array[i];
    				array[i] = currentMax;
    				}
    			}
    		}
    		System.out.println(array);		
    	}
    }
    My code compiles however I have no idea how to get the student names to show up in decreasing order by score. How do I do this?

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default Re: Sort Student Names and Grades

    In the for loop of main() you say

    Java Code:
    System.out.print("Enter the student's name: ");
    String name = input.next();
    System.out.print("Enter the student's score: ");
    double score = input.nextDouble();
    But you don't actually do anything with name or score. Effectively they are forgotten about as the next iteration of the for loop begins.

    Clearly you have to do something with these values that the user has entered. Like put them into an array (or two). Once you have this done (and can print the student names in the order they were entered) you can move on to actually sorting them.

    -----

    Have you learnt how to define your own classes? I ask because it makes more sense to create a Student class that represents the student (both name and score). If you haven't that's no problem, but you will probably have to create two arrays: one for the students' names and one for their scores.

  3. #3
    son012189 is offline Member
    Join Date
    Oct 2010
    Posts
    38
    Rep Power
    0

    Default Re: Sort Student Names and Grades

    Ok thanks. I changed those four lines of code and put name and score into arrays.

    Here is my code now:
    Java Code:
    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(array);
    	}
    	public static void selectionSort(int[] array) {
    		for(int i = array.length - 1; i >= 1; i--) {
    			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) {
    					array[currentMaxIndex] = array[i];
    					array[i] = currentMax;
    				}
    				System.out.println(array[i]);
    			}
    		}		
    	}
    }
    To print out the student's name in order of which they were entered I would use this:
    Java Code:
    System.out.println(Arrays.toString(names));
    How would I actually sort them? I'm having difficulty with this. No, I have not learned how to define my own classes. I'm taking an intro level java class and we haven't gotten to that yet.

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default Re: Sort Student Names and Grades

    How would I actually sort them? I'm having difficulty with this.
    What makes sorting the names difficult is the fact that you are sorting them based on the scores.

    First make sure that selectionSort() is working correctly. Ie if you say in main():

    Java Code:
    selectionSort(array);
    System.out.println(Arrays.toString(array));
    you should see the scores in numeric order. If not there's some problem in the sort method.

    Then you have to change the sort method to take account of the fact that you are sorting one thing in terms of another. The obvious change is that you need to pass both arrays to the sort method:

    Java Code:
        /** Sorts both array arguments so that the second is in order. */
    public static void selectionSort(String[] keyArr, int[] valueArr) {
        // changed code here
    }
    I've called them keyArr and valueArr but you would call this method like

    Java Code:
    selectionSort(names, array);
    // now print pnames
    The changes to the sort code are fairly straightforward:

    * Whenever you have a comparison (ie the conditions of the if statements) you use the value array (the one with the scores).
    * But every time to alter array values (ie the assignment statements inside the second if statement) you alter both arrays in the same way so that names stay in the same position in their array as their associated scores in the other array.

  5. #5
    son012189 is offline Member
    Join Date
    Oct 2010
    Posts
    38
    Rep Power
    0

    Default Re: Sort Student Names and Grades

    Thank you. I was finally able to solve the problem thanks to your help. Here is the final code:

    Java Code:
    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;
    				}
    		}		
    	}
    }

  6. #6
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default Re: Sort Student Names and Grades

    Well done! I'm glad you've got it figured out.

Similar Threads

  1. Passing Grades Help!
    By JojoDiaz in forum New To Java
    Replies: 16
    Last Post: 10-14-2011, 04:48 PM
  2. Replies: 26
    Last Post: 05-13-2011, 04:54 PM
  3. Replies: 18
    Last Post: 05-02-2011, 06:37 PM
  4. array to sort names in alphabetical order
    By leoshiner in forum New To Java
    Replies: 6
    Last Post: 05-01-2011, 01:28 PM
  5. Array help counting # of grades
    By speaknspell in forum New To Java
    Replies: 4
    Last Post: 04-16-2009, 11:09 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •