Score calculation
- Java course
- Intermediate
- Lesson 45/116
- 6 min
Compute correct count and percentage with score.
Score calculation
The fourth lesson adds a method that calculates the score. A check method compares the given answer with the question's answer. A score field holds the count of correct answers. This is the quiz's core operation.
Example: int score = 0; int check(String[] answers) { for (int i = 0; i < count; i++) { if (answers[i].equals(questions[i].answer)) score++; } return score; }. This method compares each question's answer with the provided answers. If there is a correct match, score increases. The equals method is used for string comparison; == compares references and can give wrong results. The method returns the total count of correct answers.
equals vs ==
In Java strings are compared with equals. The == operator compares references, not content. Two strings with the same content can still return false with ==.
Example: new String("yes").equals(new String("yes")) returns true; because the content is the same. But new String("yes") == new String("yes") returns false; because two separate objects have different references. That is why equals is always used for string comparison. In quiz score calculation, answers[i].equals(questions[i].answer) is the correct approach. If you use ==, the score can always stay zero.
Practice Add a check method to Quiz
Add an int score = 0 field to the Quiz class. Add an int check(String[] answers) method; in its body loop i from zero to count. At each step run if (answers[i].equals(questions[i].answer)) score++;. The method returns score. In main create a Quiz instance, add two questions, call check with String[] answers = {"Paris", "Berlin"} and print the result.
Starter code
class Question {
String text;
String answer;
Question(String t, String a) {
text = t;
answer = a;
}
public String toString() {
return text + " = " + answer;
}
}
class Quiz {
Question[] questions;
int count;
Quiz() {
questions = new Question[10];
count = 0;
}
void add(Question q) {
questions[count] = q;
count++;
}
void display() {
for (int i = 0; i < count; i++) {
System.out.println(questions[i]);
}
}
}
// Add score and check method to Quiz
// Compare answers with equals
// Instantiate and call check in mainSample solution
class Question {
String text;
String answer;
Question(String t, String a) {
text = t;
answer = a;
}
public String toString() {
return text + " = " + answer;
}
}
class Quiz {
Question[] questions;
int count;
int score;
Quiz() {
questions = new Question[10];
count = 0;
score = 0;
}
void add(Question q) {
questions[count] = q;
count++;
}
void display() {
for (int i = 0; i < count; i++) {
System.out.println(questions[i]);
}
}
int check(String[] answers) {
for (int i = 0; i < count; i++) {
if (answers[i].equals(questions[i].answer)) score++;
}
return score;
}
public static void main(String[] args) {
Quiz quiz = new Quiz();
quiz.add(new Question("Capital", "Paris"));
quiz.add(new Question("City", "London"));
String[] answers = {"Paris", "Berlin"};
System.out.println(quiz.check(answers));
}
}Expected output
1
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 Complete the equals comparison
if (answers[i].____(questions[i].answer)) {
score++;
}
if (answers[i].equals(questions[i].answer)) {
score++;
}
Explanation: String comparison is done with equals.
2 Match the score parts
| Term | Meaning |
|---|---|
int score |
Correct count |
answers[i].equals(...) |
Comparison |
score++ |
Increment |
return score |
Return |
3 Which method is used to compare two strings by content?
- ==
- same
- equals Correct answer
- compare
Explanation: Equals — string content is compared with equals.