CodeBlanks Logo Icon CodeBlanks

Putting it together

Combine the Quiz class with a menu loop.

Integration

The final lesson brings all parts together. You'll define a Runner class, create a quiz, add questions, check answers and print the result.

Example: In Runner's main method a Quiz instance is created. Two questions are added. An answer array is provided and check is called. The result and question count are printed on separate lines. This shows how all parts of the project come together. You now have a working quiz application; it can distinguish correct and wrong answers.

Program output

The integration lesson prints the questions and the score. First the questions via display, then the score via check. The output shows all parts of the project working together.

Expected output: first two question lines, then the score line. For example Capital = Paris, City = London, 1. These lines come from the display and check calls respectively. The question count is two, the correct answer is one; the score is printed as one. When the program runs, the quiz's status is visible at a glance.

Practice Define Runner and run the quiz

Add a Runner class to the file. In its main method create a Quiz instance. Add two questions: new Question("Capital", "Paris") and new Question("City", "London"). Call display(). Define String[] answers = {"Paris", "Berlin"}, call check(answers) and print the result.

Starter code

Main.java
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;
    }
}

// Add the Runner class
// Create quiz, add questions, call display and check in main

Sample solution

Main.java
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;
    }
}
class Runner {
    public static void main(String[] args) {
        Quiz quiz = new Quiz();
        quiz.add(new Question("Capital", "Paris"));
        quiz.add(new Question("City", "London"));
        quiz.display();
        String[] answers = {"Paris", "Berlin"};
        System.out.println(quiz.check(answers));
    }
}

Expected output

Capital = Paris
City = London
1

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 Match classes to their roles

Term Meaning
Question Question
Quiz Question pool
check Score calculation
Runner Entry point

2 Which class holds the main method in the integration lesson?

  • Question
  • Quiz
  • Answer
  • Runner Correct answer

Explanation: Runner — a new Runner class is defined in the integration lesson.

3 Complete the Runner main body

Question
class Runner {
    public static void main(String[] args) {
        Quiz quiz = new Quiz();
        quiz.add(new Question("Başkent", "Ankara"));
        String[] answers = {"Ankara"};
        System.out.println(quiz.____(answers));
    }
}
Answer
class Runner {
    public static void main(String[] args) {
        Quiz quiz = new Quiz();
        quiz.add(new Question("Başkent", "Ankara"));
        String[] answers = {"Ankara"};
        System.out.println(quiz.check(answers));
    }
}

Explanation: The check method calculates and returns the score.

All lessons in Intermediate