Question

Urgent in Java Create an instance of the “TestQuestion” class in your “main” method. Allow the...

Urgent in Java

Create an instance of the “TestQuestion” class in your “main” method. Allow the user to enter the test question and its answer, if they want to. If they do not, then continue by creating a default question and answer. Please use the constructor methods here. Then, using the “ToString” or “__str__” method, have it print out those variables as described above.

Lastly, call the “CheckAnswer” method and allow the user to continue attempting to answer the question until they are correct. If they are correct, the method should work as above and allow them to enter a new question and answer. You may either terminate the program at this point (if the user types “quit”) or allow for an infinite series of trivia games.

Example:

Please enter a test question (or just hit enter to skip): “What is your favorite color?”

Please enter the answer to that question (or just hit enter to skip): “Blue”

Your question was: “What is your favorite color?” and your answer was: “Blue”

Please enter your answer to the test question: “Red”

I’m sorry, you were incorrect and have not dethroned the previous champion. Please enter another answer to the test question: “Blue”

You are the new champion. Claim your prize by setting a new more difficult question: “What is the airspeed velocity of an unladen Swallow?”

And the answer to that question: “24 miles per hour.”

Your question was: “What is the airspeed velocity of an unladen Swallow?” and your answer was: “24 miles per hour.”

…. (It should continue asking for the correct answer. Otherwise it should end here if the user types “quit”.)

Example using Default Question and Answer:

Please enter a test question (or just hit enter to skip): “”

Please enter the answer to that question (or just hit enter to skip): “”

Your question was: “Find the value of x in 4 = x ^ 2” and your answer was: “2”

Please enter your answer to the test question: “16”

I’m sorry, you were incorrect and have not dethroned the previous champion. Please enter another answer to the test question: “4”

You are the new champion. Claim your prize by setting a new more difficult question: “What is the airspeed velocity of an unladen Swallow?”

Please enter the answer to that question: “24 miles per hour.”

Your question was: “What is the airspeed velocity of an unladen Swallow?” and your answer was: “24 miles per hour.”

…. (It should continue on like that, if using a loop. Otherwise it should end here.)

0 0
Add a comment Improve this question Transcribed image text
Answer #1

import java.util.Scanner;

class Main

{

public static void main(String[] args)

{

String str,ans,uans;

int ch;

do

{

Scanner sc=new Scanner(System.in);

str="";

ans="";

uans="";

System.out.println("Please enter a test question (or just hit enter to skip):");

str=sc.nextLine();

//'What is your favorite color?”

System.out.println("Please enter the answer to that question (or just hit enter to skip): ");

ans=sc.nextLine();

//“Blue”

System.out.println("Your question was: "+str+" and your answer was: "+ans);

do

{

System.out.println("Please enter your answer to the test question: ");

uans=sc.next();

if(ans.equalsIgnoreCase(uans))

System.out.println("You are the new champion. Claim your prize by setting a new more difficult question:");

else

System.out.println("I’m sorry, you were incorrect and have not dethroned the previous champion.");

}while(!ans.equalsIgnoreCase(uans));

System.out.println("Do you want to continue(1.Yes/ 2.No)");

ch=sc.nextInt();

}while(ch!=2);

}

}

OUTPUT

Add a comment
Know the answer?
Add Answer to:
Urgent in Java Create an instance of the “TestQuestion” class in your “main” method. Allow the...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT