Question: Create and test an updated Trivia class: The Trivia class has two String instance variables, question and answer . Define a constructor to initialize a

Create and test an updated Trivia class:

The Trivia class has two String instance variables, question and answer.

Define a constructor to initialize a Trivia objects question and answer.

The Trivia class has only getter methods for these instance variables, so once they have been set by the constructor they cant be changed.

The Trivia class should also have a toString() method to return its info (its question and answer) as a String.

Write a main method that creates two Trivia objects and then uses each of those objects question and answer instance variables (obtained by using their getter methods) to ask the user those two questions, reads their answer for each using nextLine(), and tells them whether they had the right answer in each case.

Use the String equalToIgnoreCase() method to check their answer. 

Also in main, print the two Trivia objects to show that this automatically runs their toString() methods.

There is a supplied part of main that will be used to test your program.

Your program should print one of the following two sets of outputs depending on whether their answer is correct:

That's right - congratulations! I'm sorry, that's not correct. The correct answer is:  

The String returned by toString() should look like this (put a newline character, , between the two parts of the returned String):

question:  answer: 

IN JAVA:

public class Trivia // Week 8 Tuesday Lab, part 2

{

private String question;

private String answer;

/* write a Trivia constructor here, using two *private*

setter methods for question and answer to set those

instance variables */

/* now write the two private setter methods for Trivia here */

/* write the two public getter methods for Trivia here */

@Override

public String toString()

{

/* write the toString method to return the String shown above */

}

public static void main(String[] args)

{

Scanner170 keyboard = new Scanner170(System.in);

// you can create some Trivia objects here and then use

// their getQuestion methods to print out their questions;

// use nextLine to read the user's answer and see if it

// matches what getAnswer returns, using equalsIgnoreCase

/* your code goes here

/* DO NOT MODIFY THIS PART OF MAIN (the next 4 statement) */

Trivia t = new Trivia("some question", "some answer");

System.out.println("Here's the question: " + t.getQuestion());

System.out.print("Enter your answer: ");

String answer = keyboard.nextLine();

/* END OF PART OF MAIN THAT YOU MUST NOT MODIFY */

/* now copy the code you wrote to check for the correct answer here */

System.out.println(t); // test toString

}

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!