# Two-Dimensional Array to Grade Students?

Show 40 post(s) from this thread on one page
Page 2 of 2 First 12
• March 21st, 2011, 10:28 PM
vanDarg
Re: Two-Dimensional Array to Grade Students?
For the if statements, look at the brackets in which the for loop is running.
As for the students grades, are you saying that each student has a unique grade? Ex. Student 1 has 6, and student 2 has 8. Could we not just say, student 1 has 8, and student 2 has 6 considering the students don't have names? If not, let me know. I'll work through it with you
• March 21st, 2011, 10:40 PM
Java Neil
Re: Two-Dimensional Array to Grade Students?
Quote:

Originally Posted by vanDarg
For the if statements, look at the brackets in which the for loop is running.
As for the students grades, are you saying that each student has a unique grade? Ex. Student 1 has 6, and student 2 has 8. Could we not just say, student 1 has 8, and student 2 has 6 considering the students don't have names? If not, let me know. I'll work through it with you

Yes, each student has a unique grade and needs to sort with their perspective student.

I now see what you mean about the brackets.

Here is the fixed code

Code java:

``` import javax.swing.JOptionPane; public class GradeExam { public static void main(String[] args) { char[][] answers = { {'A', 'B', 'A', 'C', 'C', 'D', 'E', 'E', 'A', 'D'}, {'D', 'B', 'A', 'B', 'C', 'A', 'E', 'E', 'A', 'D'}, {'E', 'D', 'D', 'A', 'C', 'B', 'E', 'E', 'A', 'D'}, {'C', 'B', 'A', 'E', 'D', 'C', 'E', 'E', 'A', 'D'}, {'A', 'B', 'D', 'C', 'C', 'D', 'E', 'E', 'A', 'D'}, {'B', 'B', 'E', 'C', 'C', 'D', 'E', 'E', 'A', 'D'}, {'B', 'B', 'A', 'C', 'C', 'D', 'E', 'E', 'A', 'D'}, {'E', 'B', 'E', 'C', 'C', 'D', 'E', 'E', 'A', 'D'}};   char[] keys = {'D', 'B', 'D', 'C', 'C', 'D', 'A', 'E', 'A', 'D'};   //Create new array to sort students int[] student = new int[8];   //Echo the answers String result = "The answer key to the test: \n"; for (int k = 0; k < 10; k++) { result += keys[k] + " ";   } for (int i = 0; i < answers.length; i++) { int correctCount = 0; //Convert grades to a String String result2 = "Student " + (i + 1) + "'s answers are: \n"; for (int j = 0; j < answers[i].length; j++) { result2 += answers[i][j] + " "; if (answers[i][j] == keys[j]) correctCount++; student[i] = correctCount; }   //Display the input JOptionPane.showMessageDialog(null, result + "\n" + result2 + "\nStudent " + (i + 1) + "'s correct count is " + correctCount); } //Find the maximum in the list for (int i = student.length - 1; i > 0; i--) { int currentMax = student[i]; int currentMaxIndex = i;   for (int j = i - 1; j > (-1); j--) { if (currentMax < student[j]) { currentMax = student[j]; currentMaxIndex = j; } }   //Swap student[i] with list[currentMinIndex] if necessary if (currentMaxIndex != i) { student[currentMaxIndex] = student[i]; student[i] = currentMax; } }   //Convert input to a String String result3 = "The students sorted scores: \n" + "\n"; for (int i = 0; i < student.length; i++) { result3 += "Student " + (i + 1) + "'s score = " + student[i] + " \n"; }   //Display the sorted grades JOptionPane.showMessageDialog(null, result3);   JOptionPane.showMessageDialog(null, "Thanks for playing!"); } }```

The only thing left is tying the students to their grades.

Once again, I can't thank you enough for your help.
• March 22nd, 2011, 06:51 AM
Java Neil
Re: Two-Dimensional Array to Grade Students?
I'm not sure if this helps, but here is some code that will sort two values.

Code java:

```public class SortTwoDimensions { public static void main(String[] args) { int m[][] = { { 4, 2 }, { 1, 7 }, { 4, 5 }, { 1, 2 }, { 1, 1 }, { 4, 1 } };   sort(m);   printArray(m); }   public static void sort(int m[][]) { for (int i = 0; i < m.length; i++) { double currentMin = m[i][0]; int currentMinIndex = i;   for (int j = i; j < m.length; j++) { if (currentMin > m[j][0] // primary sort || (currentMin == m[j][0] && m[currentMinIndex][1] > m[j][1])) // secondary // sort { currentMin = m[j][0]; currentMinIndex = j; } }   // Swap list[i] with list[currentMinIndex] if necessary; if (currentMinIndex != i) { int temp0 = m[currentMinIndex][0]; int temp1 = m[currentMinIndex][1]; m[currentMinIndex][0] = m[i][0]; m[currentMinIndex][1] = m[i][1]; m[i][0] = temp0; m[i][1] = temp1; } } }   public static void printArray(int m[][]) { for (int i = 0; i < m.length; i++) { System.out.println(m[i][0] + ", " + m[i][1]); } } }```

Now if we use this type of sort, I guess the students correctCount would be the primary sort and the student would be the secondary.

What do you think?
• March 22nd, 2011, 09:04 AM
Java Neil
Re: Two-Dimensional Array to Grade Students?
Wow, my brain hurts!

Well this is the combination of the codes.

Code java:

``` import javax.swing.JOptionPane; public class GradeExam { public static void main(String[] args) { char[][] answers = { {'A', 'B', 'A', 'C', 'C', 'D', 'E', 'E', 'A', 'D'}, {'D', 'B', 'A', 'B', 'C', 'A', 'E', 'E', 'A', 'D'}, {'E', 'D', 'D', 'A', 'C', 'B', 'E', 'E', 'A', 'D'}, {'C', 'B', 'A', 'E', 'D', 'C', 'E', 'E', 'A', 'D'}, {'A', 'B', 'D', 'C', 'C', 'D', 'E', 'E', 'A', 'D'}, {'B', 'B', 'E', 'C', 'C', 'D', 'E', 'E', 'A', 'D'}, {'B', 'B', 'A', 'C', 'C', 'D', 'E', 'E', 'A', 'D'}, {'E', 'B', 'E', 'C', 'C', 'D', 'E', 'E', 'A', 'D'}};   char[] keys = {'D', 'B', 'D', 'C', 'C', 'D', 'A', 'E', 'A', 'D'};   //Create new array to sort students int[][] student = new int[8][8];   //Echo the answers String result = "The answer key to the test: \n"; for (int k = 0; k < 10; k++) { result += keys[k] + " ";   } for (int i = 0; i < answers.length; i++) { int correctCount = 0; //Convert grades to a String String result2 = "Student " + (i + 1) + "'s answers are: \n"; for (int j = 0; j < answers[i].length; j++) { result2 += answers[i][j] + " "; if (answers[i][j] == keys[j]) correctCount++;     } student[i][0] = correctCount; student[i][1] = i; //Display the input JOptionPane.showMessageDialog(null, result + "\n" + result2 + "\nStudent " + (i + 1) + "'s correct count is " + correctCount); sort(student);   printArray(student); } } public static void sort(int m[][]) { for (int i = 0; i < m.length; i++) { double currentMin = m[i][0]; int currentMinIndex = i;   for (int j = i; j < m.length; j++) { if (currentMin > m[j][0] // primary sort || (currentMin == m[j][0] && m[currentMinIndex][1] > m[j][1])) // secondary // sort { currentMin = m[j][0]; currentMinIndex = j; } }   // Swap list[i] with list[currentMinIndex] if necessary; if (currentMinIndex != i) { int temp0 = m[currentMinIndex][0]; int temp1 = m[currentMinIndex][1]; m[currentMinIndex][0] = m[i][0]; m[currentMinIndex][1] = m[i][1]; m[i][0] = temp0; m[i][1] = temp1; } } }   public static void printArray(int m[][]) { String result3 = "The students sorted scores: \n" + "\n"; for (int i = 0; i < m.length; i++) { result3 += "Student " + (m[i][1]+1) + "'s score = " + m[i][0] + " \n";   } JOptionPane.showMessageDialog(null, result3); } }```

While this code kinda works the way I want it to, it does not hold all the values. It sorts the first four students with their grades, but after that it will replace previous students and their grades with new ones.

Another problem with this code is I only want it to print the sorted grades once, and this prints every cycle?

I'm soooooo close I can taste it. It's scary to say, but I'm starting to love Java!
• March 22nd, 2011, 09:42 AM
Java Neil
Re: Two-Dimensional Array to Grade Students?
Here is one without all the methods...

Code java:

``` import javax.swing.JOptionPane; public class GradeExam { public static void main(String[] args) { char[][] answers = { {'A', 'B', 'A', 'C', 'C', 'D', 'E', 'E', 'A', 'D'}, {'D', 'B', 'A', 'B', 'C', 'A', 'E', 'E', 'A', 'D'}, {'E', 'D', 'D', 'A', 'C', 'B', 'E', 'E', 'A', 'D'}, {'C', 'B', 'A', 'E', 'D', 'C', 'E', 'E', 'A', 'D'}, {'A', 'B', 'D', 'C', 'C', 'D', 'E', 'E', 'A', 'D'}, {'B', 'B', 'E', 'C', 'C', 'D', 'E', 'E', 'A', 'D'}, {'B', 'B', 'A', 'C', 'C', 'D', 'E', 'E', 'A', 'D'}, {'E', 'B', 'E', 'C', 'C', 'D', 'E', 'E', 'A', 'D'}};   char[] keys = {'D', 'B', 'D', 'C', 'C', 'D', 'A', 'E', 'A', 'D'};   //Create new array to sort students int[][] student = new int[8][8];   //Echo the answers String result = "The answer key to the test: \n"; for (int k = 0; k < 10; k++) { result += keys[k] + " ";   } for (int i = 0; i < answers.length; i++) { int correctCount = 0; //Convert grades to a String String result2 = "Student " + (i + 1) + "'s answers are: \n"; for (int j = 0; j < answers[i].length; j++) { result2 += answers[i][j] + " "; if (answers[i][j] == keys[j]) correctCount++; student[i][0] = correctCount; student[i][1] = i;   }   //Display the input JOptionPane.showMessageDialog(null, result + "\n" + result2 + "\nStudent " + (i + 1) + "'s correct count is " + correctCount); } for (int i = 0; i < student.length; i++) { double currentMin = student[i][0]; int currentMinIndex = i;   for (int j = i; j < student.length; j++) { if (currentMin > student[j][0] // primary sort || (currentMin == student[j][0] && student[currentMinIndex][1] > student[j][1])) // secondary // sort { currentMin = student[j][0]; currentMinIndex = j; } }   // Swap list[i] with list[currentMinIndex] if necessary; if (currentMinIndex != i) { int temp0 = student[currentMinIndex][0]; int temp1 = student[currentMinIndex][1]; student[currentMinIndex][0] = student[i][0]; student[currentMinIndex][1] = student[i][1]; student[i][0] = temp0; student[i][1] = temp1; }   String result3 = "The students sorted scores: \n" + "\n"; for (i = 0; i < student.length; i++) { result3 += "Student " + (student[i][1]+1) + "'s score = " + student[i][0] + " \n";   } JOptionPane.showMessageDialog(null, result3); } } }```

This code prints correctly and has all the values in it, but it's not sorting correctly. The students are printed with their grades but not in the right order. I can tell that there is some sorting being done, but I can't see the logic behind it.
• March 22nd, 2011, 10:39 AM
vanDarg
Re: Two-Dimensional Array to Grade Students?
Looking good, I did not realize that you had to sort the grades in such a way. So, using a multi-dimensional array here is a good idea. I think you need to look a little closer at the sorting algorithm. See this link on sorting a multi-dimensional array. I also recommend (and it isn't necessary) removing the sorting part of your code and place it into a separate method, this will add to the organization and readability.
• March 22nd, 2011, 10:49 AM
Java Neil
Re: Two-Dimensional Array to Grade Students?
Quote:

Originally Posted by vanDarg
Looking good, I did not realize that you had to sort the grades in such a way. So, using a multi-dimensional array here is a good idea. I think you need to look a little closer at the sorting algorithm. See this link on sorting a multi-dimensional array. I also recommend (and it isn't necessary) removing the sorting part of your code and place it into a separate method, this will add to the organization and readability.

Thanks for the tips. I've posted so many codes, I'm not sure which one you're referring to. About three posts ago I posted code that had separate methods.

Could you maybe show me what you had in mind. It's just that my class starts in about an hour and I might not have time to work through the logic. I would not normally ask, but I'll learn the process just the same just as I did when you showed me the other stuff.
• March 22nd, 2011, 10:54 AM
Java Neil
Re: Two-Dimensional Array to Grade Students?
Quote:

Originally Posted by Java Neil
Wow, my brain hurts!

Well this is the combination of the codes.

Code java:

``` import javax.swing.JOptionPane; public class GradeExam { public static void main(String[] args) { char[][] answers = { {'A', 'B', 'A', 'C', 'C', 'D', 'E', 'E', 'A', 'D'}, {'D', 'B', 'A', 'B', 'C', 'A', 'E', 'E', 'A', 'D'}, {'E', 'D', 'D', 'A', 'C', 'B', 'E', 'E', 'A', 'D'}, {'C', 'B', 'A', 'E', 'D', 'C', 'E', 'E', 'A', 'D'}, {'A', 'B', 'D', 'C', 'C', 'D', 'E', 'E', 'A', 'D'}, {'B', 'B', 'E', 'C', 'C', 'D', 'E', 'E', 'A', 'D'}, {'B', 'B', 'A', 'C', 'C', 'D', 'E', 'E', 'A', 'D'}, {'E', 'B', 'E', 'C', 'C', 'D', 'E', 'E', 'A', 'D'}};   char[] keys = {'D', 'B', 'D', 'C', 'C', 'D', 'A', 'E', 'A', 'D'};   //Create new array to sort students int[][] student = new int[8][8];   //Echo the answers String result = "The answer key to the test: \n"; for (int k = 0; k < 10; k++) { result += keys[k] + " ";   } for (int i = 0; i < answers.length; i++) { int correctCount = 0; //Convert grades to a String String result2 = "Student " + (i + 1) + "'s answers are: \n"; for (int j = 0; j < answers[i].length; j++) { result2 += answers[i][j] + " "; if (answers[i][j] == keys[j]) correctCount++;     } student[i][0] = correctCount; student[i][1] = i; //Display the input JOptionPane.showMessageDialog(null, result + "\n" + result2 + "\nStudent " + (i + 1) + "'s correct count is " + correctCount); sort(student);   printArray(student); } } public static void sort(int m[][]) { for (int i = 0; i < m.length; i++) { double currentMin = m[i][0]; int currentMinIndex = i;   for (int j = i; j < m.length; j++) { if (currentMin > m[j][0] // primary sort || (currentMin == m[j][0] && m[currentMinIndex][1] > m[j][1])) // secondary // sort { currentMin = m[j][0]; currentMinIndex = j; } }   // Swap list[i] with list[currentMinIndex] if necessary; if (currentMinIndex != i) { int temp0 = m[currentMinIndex][0]; int temp1 = m[currentMinIndex][1]; m[currentMinIndex][0] = m[i][0]; m[currentMinIndex][1] = m[i][1]; m[i][0] = temp0; m[i][1] = temp1; } } }   public static void printArray(int m[][]) { String result3 = "The students sorted scores: \n" + "\n"; for (int i = 0; i < m.length; i++) { result3 += "Student " + (m[i][1]+1) + "'s score = " + m[i][0] + " \n";   } JOptionPane.showMessageDialog(null, result3); } }```

While this code kinda works the way I want it to, it does not hold all the values. It sorts the first four students with their grades, but after that it will replace previous students and their grades with new ones.

Another problem with this code is I only want it to print the sorted grades once, and this prints every cycle?

I'm soooooo close I can taste it. It's scary to say, but I'm starting to love Java!

This is the code with the methods. As you can see I have a few problems with it!
• March 22nd, 2011, 11:34 AM
vanDarg
Re: Two-Dimensional Array to Grade Students?
I was referring to your most recent post. You don't have to move the sorting into a method, it was just a suggestion. Work on the sorting.
• March 22nd, 2011, 11:39 AM
Java Neil
Re: Two-Dimensional Array to Grade Students?
Quote:

Originally Posted by vanDarg
I was referring to your most recent post. You don't have to move the sorting into a method, it was just a suggestion. Work on the sorting.

Yeah, That's the code I'm messing with but I'm running out of time fast!

Code java:

``` import javax.swing.JOptionPane; public class GradeExam2 { public static void main(String[] args) { char[][] answers = { {'A', 'B', 'A', 'C', 'C', 'D', 'E', 'E', 'A', 'D'}, {'D', 'B', 'A', 'B', 'C', 'A', 'E', 'E', 'A', 'D'}, {'E', 'D', 'D', 'A', 'C', 'B', 'E', 'E', 'A', 'D'}, {'C', 'B', 'A', 'E', 'D', 'C', 'E', 'E', 'A', 'D'}, {'A', 'B', 'D', 'C', 'C', 'D', 'E', 'E', 'A', 'D'}, {'B', 'B', 'E', 'C', 'C', 'D', 'E', 'E', 'A', 'D'}, {'B', 'B', 'A', 'C', 'C', 'D', 'E', 'E', 'A', 'D'}, {'E', 'B', 'E', 'C', 'C', 'D', 'E', 'E', 'A', 'D'}};   char[] keys = {'D', 'B', 'D', 'C', 'C', 'D', 'A', 'E', 'A', 'D'};   //Create new array to sort students int[][] student = new int[8][8];   //Echo the answers String result = "The answer key to the test: \n"; for (int k = 0; k < 10; k++) { result += keys[k] + " ";   } for (int i = 0; i < answers.length; i++) { int correctCount = 0; //Convert grades to a String String result2 = "Student " + (i + 1) + "'s answers are: \n"; for (int j = 0; j < answers[i].length; j++) { result2 += answers[i][j] + " "; if (answers[i][j] == keys[j]) correctCount++; student[i][0] = correctCount; student[i][1] = i;   }   //Display the input JOptionPane.showMessageDialog(null, result + "\n" + result2 + "\nStudent " + (i + 1) + "'s correct count is " + correctCount); } for (int i = 0; i < student.length; i++) { double currentMin = student[i][0]; int currentMinIndex = i;   for (int j = i; j < student.length; j++) { if (currentMin > student[j][0] // primary sort || (currentMin == student[j][0] && student[currentMinIndex][1] > student[j][1])) // secondary // sort { currentMin = student[j][0]; currentMinIndex = j; } }   // Swap list[i] with list[currentMinIndex] if necessary; if (currentMinIndex != i) { int temp0 = student[currentMinIndex][0]; int temp1 = student[currentMinIndex][1]; student[currentMinIndex][0] = student[i][0]; student[currentMinIndex][1] = student[i][1]; student[i][0] = temp0; student[i][1] = temp1; }   String result3 = "The students sorted scores: \n" + "\n"; for (i = 0; i < student.length; i++) { result3 += "Student " + (student[i][1]+1) + "'s score = " + student[i][0] + " \n";   } JOptionPane.showMessageDialog(null, result3); } } }```
• March 22nd, 2011, 12:07 PM
Java Neil
Re: Two-Dimensional Array to Grade Students?
Well I've tried everything time will allow, but it's time for class. When I get to school I'll check back to see if you have come up with something. Even if you can't, I value everything you have done for me. I truly am learning a lot about the "proper way" to do things in java.

If you're not already planning it, you should become a moderator here.
• March 22nd, 2011, 01:37 PM
vanDarg
Re: Two-Dimensional Array to Grade Students?
Thanks for your confidence, I'd follow that link I provided on sorting multi-dimensional arrays, this should get your code to where you need it.
• March 22nd, 2011, 03:47 PM
Java Neil
Re: Two-Dimensional Array to Grade Students?
Quote:

Originally Posted by vanDarg
Thanks for your confidence, I'd follow that link I provided on sorting multi-dimensional arrays, this should get your code to where you need it.

The good news is my teacher checked off this assignment. He didn't even look at whether or not the students were being sorted with the grades. The bad news is I now need to figure this out to satisfy my own knowledge of this kind of thing.

So here is the program with the link's you provided me code mixed in. It in no way is working yet (I have not set it up to call anything yet). I thought you might be able to take it as it is and write some note for me as far as structure is concerned. Once again I think I need to learn the proper way, rather than just what works.

Code java:

``` import javax.swing.JOptionPane; public class GradeExam2 { public static void main(String[] args) { char[][] answers = { {'A', 'B', 'A', 'C', 'C', 'D', 'E', 'E', 'A', 'D'}, {'D', 'B', 'A', 'B', 'C', 'A', 'E', 'E', 'A', 'D'}, {'E', 'D', 'D', 'A', 'C', 'B', 'E', 'E', 'A', 'D'}, {'C', 'B', 'A', 'E', 'D', 'C', 'E', 'E', 'A', 'D'}, {'A', 'B', 'D', 'C', 'C', 'D', 'E', 'E', 'A', 'D'}, {'B', 'B', 'E', 'C', 'C', 'D', 'E', 'E', 'A', 'D'}, {'B', 'B', 'A', 'C', 'C', 'D', 'E', 'E', 'A', 'D'}, {'E', 'B', 'E', 'C', 'C', 'D', 'E', 'E', 'A', 'D'}};   char[] keys = {'D', 'B', 'D', 'C', 'C', 'D', 'A', 'E', 'A', 'D'};   //Create new array to sort students int[][] student = new int[8][8];   //Echo the answers String result = "The answer key to the test: \n"; for (int k = 0; k < 10; k++) { result += keys[k] + " ";   } for (int i = 0; i < answers.length; i++) { int correctCount = 0; //Convert grades to a String String result2 = "Student " + (i + 1) + "'s answers are: \n"; for (int j = 0; j < answers[i].length; j++) { result2 += answers[i][j] + " "; if (answers[i][j] == keys[j]) correctCount++; student[i][0] = correctCount; student[i][1] = i;   }   //Display the input JOptionPane.showMessageDialog(null, result + "\n" + result2 + "\nStudent " + (i + 1) + "'s correct count is " + correctCount); } }   private String[][] bubbleSortMulti(String[][] MultiIn, int compIdx) {   String[][] temp = new String[MultiIn.length][MultiIn[0].length];   boolean finished = false;   while (!finished) { finished = true;   for (int i = 0; i < MultiIn.length - 1; i++) {   if (MultiIn[i][compIdx].compareToIgnoreCase(MultiIn[i + 1][compIdx]) > 0) {   for (int j = 0; j < MultiIn[i].length; j++) { temp[i][j] = MultiIn[i][j]; MultiIn[i][j] = MultiIn[i + 1][j]; MultiIn[i + 1][j] = temp[i][j]; }   finished = false; } } } return MultiIn; } }```
Show 40 post(s) from this thread on one page
Page 2 of 2 First 12