in python, inheritance. Add a class MultiChoiceQuestion to the question hierarch
ID: 3767336 • Letter: I
Question
in python, inheritance.
Add a class MultiChoiceQuestion to the question hierarchy of Section 10.1 that allows multiple correct choices. The respondent should provide all correct choices, separated by spaces. Provide instructions in the question text. Below is the program that runs the class MultiChoiceQuestion.py.
##
# This program shows a simple quiz with one question.
#
from questions import MultiChoiceQuestion
# Create the question and expected answer.
q = MultiChoiceQuestion()
q.setText("Of Apple, Tomato, Carrot, Cucumber and Celery, list all that are fruit.")
q.setAnswer("Apple Tomato")
# Display the question and obtain user's response.
q.display()
response = input("Your answer: ")
print(q.checkAnswer(response))
Explanation / Answer
import java.util.Scanner; public class QuestionDemo { public static void main(String[] args) { Question[] quiz = new Question[2]; MultiChoiceQuestion question0 = new MultiChoiceQuestion( "Which countries are in Africa?"); question0.addChoice("Ghana", true); question0.addChoice("Canada", false); question0.addChoice("Benin", true); question0.addChoice("Denmark", false); quiz[0] = question0; MultiChoiceQuestion question1 = new MultiChoiceQuestion( "Which of the following are vegetables?"); question1.addChoice("Zuchini", true); question1.addChoice("Pepper", true); question1.addChoice("Banana", false); question1.addChoice("Cabbage", true); quiz[1] = question1; Scanner in = new Scanner(System.in); for (Question q : quiz) { q.display(); System.out.println("Your answer: "); String response = in.nextLine(); System.out.println(q.checkAnswer(response)); } } }
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.