Finding the median in an int array
Hi everyone,
I have this program that displays scores from a test. I am trying to write an algorhythm that finds the median score, but mine is not working. Can anyone point me in the right direction? Thanks ahead of time.
import javax.swing.JOptionPane;
import java.util.Arrays;
publicclass Grades{
//Answer Key
public String[] correctAnswer ={"D","B","A","B","C","A","E","E","A","D"};
//Students' Submitted Answers
public String[][] submittedAnswer ={{"D","B","A","B","C","A","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","A","D","C","E","E","A","D"},
{"A","B","D","E","C","D","E","E","A","D"},
{"B","B","E","B","C","D","E","E","A","D"},
{"B","B","A","B","C","D","E","E","A","D"},
{"E","B","E","E","C","D","E","E","A","D"}};
//Contains the number of correct answers for each student
publicint[] correct =newint[8];
//The user inputs the answer key
publicvoid getAnswer(){
String ans;
String ans2;
for (int i =0; i<correctAnswer.length; i++){
ans = JOptionPane.showInputDialog("\nPlease enter the correct answer for question " + i +":");
ans2 = ans.toUpperCase();
correctAnswer[i] =ans2;
}
}
//The user inputs student's submitted answers
publicvoid getSubmittedAnswer(){
String ans;
String ans2;
for (int i =0; i><submittedAnswer[0].length; i++){
ans = JOptionPane.showInputDialog("\nPlease enter student answer for question " + i +":");
ans2 = ans.toUpperCase();
submittedAnswer[0][i] =ans2;
}
}
//Compare the student answers to the answer key
publicvoid gradeTest(){
int count = 0;
for(int i = 0; i >< submittedAnswer.length; i++){
for(int j = 0; j < submittedAnswer[i].length; j++){
if(submittedAnswer[i][j].equals(correctAnswer[j]))
count ++;
}
correct[i] = count;
count=0;
//System.out.println("The no. correct answers for student " + i + " are " + correct[i]);
}
}
//Find the highest test score
publicint findMax(){
if (correct.length > 0){
int max = correct[0];
for (int i = 1; i < correct.length; i++){
if (correct[i] > max)
max = correct[i];
}
System.out.println("The highest test score is: " + max);
return max;
}
else
return 0;
}
//Find the lowest test score
publicint findMin(){
if (correct.length > 0){
int min = correct[0];
for (int i = 1; i < correct.length; i++){
if (correct[i] < min)
min = correct[i];
}
System.out.println("The lowest test score is: " + min);
return min;
}
else
return 0;
}
//Calculates the average test score
publicvoid calcAvg(){
double sum = 0;
for (int i = 0; i < correct.length; i++){
sum = sum + correct[i];
}
doubleavg = sum / correct.length;
System.out.println("The average score is " + avg);
}
//Calculates the median test score
publicvoid calcMedian(){
Arrays.sort(correct);
double median = correct.length / 2;
System.out.println("The median score is " + median);
}
public Grades(){}
}

