100% found this document useful (1 vote)
36 views2 pages

True False Question

This Java code defines a TrueFalseQuestion class that extends the Question class. The TrueFalseQuestion class contains methods to ask a true/false question, accept the user's answer, normalize it to uppercase TRUE or FALSE, and check if it matches the predefined correct answer. It will continue prompting the user for an answer until they enter a valid TRUE or FALSE response.

Uploaded by

Wendy Pointe
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
36 views2 pages

True False Question

This Java code defines a TrueFalseQuestion class that extends the Question class. The TrueFalseQuestion class contains methods to ask a true/false question, accept the user's answer, normalize it to uppercase TRUE or FALSE, and check if it matches the predefined correct answer. It will continue prompting the user for an answer until they enter a valid TRUE or FALSE response.

Uploaded by

Wendy Pointe
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

import javax.swing.

JOptionPane;

public class TrueFalseQuestion extends Question {

String ask() {

while(true) {
String answer = JOptionPane.showInputDialog(question);
answer = answer.toUpperCase();

if (answer.equals("f") || answer.equals("false") ||
answer.equals("False") || answer.equals("n") ||
answer.equals("no") || answer.equals("No") ||
answer.equals("NO")) answer = "FALSE";

if (answer.equals("t") || answer.equals("true") ||
answer.equals("T") || answer.equals("True") ||
answer.equals("TRUE") || answer.equals("y") ||
answer.equals("yes")||
answer.equals("Y")|| answer.equals("Yes")||
answer.equals("YES")) answer="TRUE";

boolean valid = (answer.equals("FALSE")|| answer.equals("TRUE"));


if(valid) return answer;

JOptionPane.showMessageDialog(null,"Invalid answer. Please enter TRUE


or FALSE"); }

TrueFalseQuestion(String question, String answer){


this.question = "TRUE OR FALSE: " +
question ;

answer= answer.toUpperCase();

if(answer.equals("T") || answer.equals("TRUE") ||
answer.equals("Y") || answer.equals("YES")) correctAnswer = "TRUE";

if (answer.equals("F")||answer.equals("FALSE") ||
answer.equals("N")|| answer.equals("NO")) correctAnswer="FALSE";
}

You might also like