4

I have a project that I need to create 2 Arrays, one to hold Student Names and one to hold Student Scores. The user inputs the size of the array, and the array needs to be sorted using BubbleSort (putting the high scores at the top). I have started the project, created the first array for scores, I have successfully done bubble sort and sorted the grades. Now I can't figure out how to make an array for Names, and once I do how do I make the names array correspond to the Grades array BubbleSort?

Here is the code I have so far.

import java.util.Scanner;

public class Grades {

public static void main(String[]args){

{
Scanner GradeIn = new Scanner(System.in);
Scanner NameIn = new Scanner(System.in);
System.out.print( "How many students are there? " );
int[]GradeArray = new int[GradeIn.nextInt()];
String[]nameArray = new String[GradeIn.nextInt()];

for( int i=0 ; i<GradeArray.length ; i++ ) 
{
System.out.print( "Enter Grade for Student " + (i+1) + ": " );
GradeArray[i] = GradeIn.nextInt();
System.out.print( "Enter Name of Student " + (i+1) + ": " );
nameArray[i] = NameIn.nextLine();
}

bubbleSort(GradeArray, nameArray);


for( int i : GradeArray ) System.out.println( i );
System.out.println();

}
}

private static void bubbleSort(int[]GradeArray, String[] nameArray){

int n = GradeArray.length;
int temp = 0;
String temp2;

for(int i=0; i<n; i++){
 for(int j=1; j<(n-i);j++){

  if(GradeArray[j-1]<GradeArray[j]){
   //swap
   temp=GradeArray[j-1];
   GradeArray[j-1]=GradeArray[j];
   GradeArray[j]=temp;

    temp2=nameArray[j-1];
    nameArray[j=1]=nameArray[j];
    nameArray[j]=temp2;


   }
  }
 }
}
}

Also how do I change the grades to Double? I started with Int and when I try to change everything to double I get an error saying "Found Double, expected Int".

What the Professor is asking for: Write a program that prompts the user to enter the number of students, the students' names, and their scores, and prints the names in decreasing order according to their scores.

ADDITIONAL INFO:

You will need two arays. One to hold strings. Another to hold the students' scores. (doubles)

The size of the arrays will be entered by the user.

You will have to sort the arrays in the main() method. I recommend using the BubbleSort (http://www.java-examples.com/java-bubble-sort-example) but not as a separate method. HINT: While sorting the grades array, you will need to sort the names array according to the grades.

And finally, you should include a method (void printAnswer(String [] names)) to print out the names array after it has been sorted.

1 Answer 1

2

Make a custom object. This is the entire idea of object oriented programming. You don't have a name and a grade, you have a Student who has a name and a grade. Then you manipulate the student in whatever way you see fit.

public class Student {
    private String name;
    private int grade;

    public Student(String name, int grade) {
        this.name = name;
        this.grade = grade;
    }

    public String getName() {
        return name;
    }
    public int getGrade() {
        return grade;
    }
}

Then where you have

int[]GradeArray = new int[UserIn.nextInt()];

Do this instead:

Student[] studentArray = new Student[UserIn.nextInt()];

I leave the rest of the changes as an exercise for you to get used to how it should work. Though, remember that a Student[] is full of null, as you read in the data, you have to create a new Student(name, grade) each time.


Alternate solution

The above is the correct way to do this. Do not learn anything from the below answer, becuase it defeats the entire purpose of learning Java. I would email the professor if the above solution is acceptable.

That being said, as you do each grade swap, you could do exactly the same swap in the String name array, i.e.

// change method signature
private static void bubbleSort(int[] gradeArray, String[] nameArray){

  int n = gradeArray.length;
  int temp = 0;
  String temp2;

  for(int i=0; i<n; i++){
    for(int j=1; j<(n-i);j++){

      if(gradeArray[j-1]<gradeArray[j]){
        //swap
        temp=gradeArray[j-1];
        gradeArray[j-1]=gradeArray[j];
        gradeArray[j]=temp;

        // New code begin
        temp2=nameArray[j-1];
        nameArray[j-1]=nameArray[j];
        nameArray[j]=temp2;
        // New code end
      }
    }
  }
}

Again, DO NOT DO THIS IN THE REAL WORLD ONCE YOU ARE NO LONGER IN SCHOOL. It is MUCH SLOWER and much more confusing.

Sign up to request clarification or add additional context in comments.

4 Comments

This does seem easier, but the professor is looking for something very specific. I'll edit my original post with what the professor asked for.
@user1861544: Wow, that sounds like an awful professor... specifically telling you NOT to use good object oriented practices. Can you edit the guidelines into the original question?
Already did. This is level 1 programming so I guess he's asking for specifics so we can learn how things work.
@user1861544: I edited my post. I see you are new to StackOverflow, don't forget to checkmark acceptable answers :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.