The goal of this code is to compare two different string[] arrays, and check to see how many of the elements match. This will allow the methods to then see whether a student has passed or failed a test, along with determining their letter grade. However, every test done results in true being returned for them passing and an "A" being returned for their letter grade. Now that the elements are being compared correctly, I still have the issue of every element passing the .equals().
i.e.
answerKey[1] = "B";
studentAnswers[1] = "C";
if these two elements were to be compared, it would pass the .equals().
public class ListOfAnswers
{
static final String[] answerKey = {"A", "B", "B", "C", "D", "B", "C", "C", "D", "E",
"C", "D", "D", "E", "A", "A", "A", "D", "D", "E"};
String[] studentAnswers;
public ListOfAnswers(String[] ans)
{
studentAnswers = ans;
}
public boolean checkPassOrFail()
{
int answersRight = 0;
for (int cnt = 0; cnt < studentAnswers.length; cnt++)
{
if(studentAnswers[cnt].equals(answerKey[cnt]))
{
answersRight++;
}
}
if (answersRight >= 14)
{
return true;
}
else
{
return false;
}
}
public String computeGrade()
{
int score = 0;
String grade = "";
for (int cnt = 0; cnt < studentAnswers.length; cnt++)
{
if (studentAnswers[cnt].(answerKey[cnt]))
{
score++;
}
}
if (score < 10)
{
grade = "E";
}
else if (score == 10)
{
grade = "D";
}
else if ((score==11)||(score==12))
{
grade = "C-";
}
else if (score==13)
{
grade = "C";
}
else if (score == 14)
{
grade = "C+";
}
else if (score == 15)
{
grade = "B-";
}
else if (score == 16)
{
grade = "B";
}
else if (score == 17)
{
grade = "B+";
}
else if (score == 18)
{
grade = "A-";
}
else if ((score == 19)||(score==20))
{
grade = "A";
}
return grade;
}
}
The test data looks as follows
public class ListOfAnswersTester
{
public static void main(String[] args)
{
String[] danAnswers = {"A", "B", "B", "D", "D", "B", "C", "C", "D", "E", "C",
"D", "D", "E", "A", "A", "A", "D", "D", "E"};
ListOfAnswers danAnswerList = new ListOfAnswers(danAnswers);
String[] bobAnswers = {"A", "C", "B", "C", "D", "B", "C", "C", "D", "E", "C",
"D", "A", "E", "A", "A", "A", "D", "E", "E"};
ListOfAnswers bobAnswerList = new ListOfAnswers(danAnswers);
System.out.println("Student Dan's Pass/Fail: " + danAnswerList.checkPassOrFail());
System.out.println("Student Dan's Grade: " + danAnswerList.computeGrade());
System.out.println("Student Bob's Pass/Fail: " + bobAnswerList.checkPassOrFail());
System.out.println("Student Bob's Grade: " + bobAnswerList.computeGrade());
}
}