Question

IN JAVA Create the “MathTest” class. It will have two class variables: 1) a question and...

IN JAVA

Create the “MathTest” class. It will have two class variables: 1) a question and 2) the answer to that question.

Please create an accessor and mutator method for both of those variables, without which we would not be able to see or change the question or the answer.

There should be a constructor method. We will also have a “ToString” method, which will print the question followed by its answer.

The constructor method has two parameters to allow the user to enter a question and answer, or if no input/parameters are given then a default question and answer should be supplied (see exercise 4 for the default question and answer).

The last method of the class, CheckAnswer, should take in a string as a parameter and compare it to the answer. If the input was incorrect, it should return false. Otherwise, it should return true.

Create an instance of the “MathTest” class in your “main” method. First ask the user if they want to enter a question and answer.

If they do, then allow them to enter the test question and its answer.

If they do not, then continue by using the default question and answer.

The default question should be “Find the value of x in 4 = x ^ 2” and the default answer should be “2”. Please use the appropriate constructor methods here.

Then, using the “ToString” method, have it print out those class variables as described above.

Lastly, call the “CheckAnswer” method. You may either terminate the program at this point (if the user types “quit”) or allow for an infinite series of trivia games.

Example:

Would you like to use a default question (yes or no)? “no”

Please enter a test question: “What is the value of x in 56 / x = 14?”

Please enter the answer to that question: “4”

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

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

Congratulations! You were correct.

Example using Default Question and Answer and with an Incorrect Answer:

Would you like to use a default question (yes or no)? “yes”

Please enter a test question: “”

Please enter the answer to that question: “”

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.

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

import java.io.*;
import java.util.*;

class MathTest {
String question;
String answer;
  
public MathTest(){
this.question = "Find the value of x in 4 = x ^ 2";
this.answer = "2";
}
  
public MathTest(String ques,String ans){
this.question=ques;
this.answer=ans;
}
  
public String getQuestion(){
return this.question;
}
  
public void setQuestion(String ques){
this.question = ques;
}
  
public String getAnswer(){
return this.answer;
}
  
public void setAnswer(String ans){
this.answer = ans;
}
  
public String ToString(){
return "Your question was: "+getQuestion()+"and your answer was: "+getAnswer();
}
  
public void CheckAnswer(String ans){
if(ans.equals(getAnswer()))
System.out.println("Congratulations! You were correct.");
else
System.out.println("I'm sorry, you were incorrect.");
}
  
   public static void main (String[] args) {
       Scanner sc = new Scanner(System.in);
       String option2;
       MathTest m;
       do{
       String option1,question,answer,finalanswer;
       System.out.println("Would you like to use a default question (yes or no)?");
       option1 = sc.nextLine();
       if(option1.equals("yes"))
       m = new MathTest();
       else
       {
       System.out.print("Please enter a test question: ");
       question = sc.nextLine();
       System.out.print("Please enter the answer to that question:");
       answer = sc.nextLine();
       m = new MathTest(question,answer);
       }
       m.ToString();
       System.out.print("Please enter your answer to the test question: ");
       finalanswer = sc.nextLine();
       m.CheckAnswer(finalanswer);
       System.out.print("Enter 'quit' to exit: ");
       option2=sc.nextLine();
       }while(!option2.equals("quit"));
   }
}

Output:

Add a comment
Know the answer?
Add Answer to:
IN JAVA Create the “MathTest” class. It will have two class variables: 1) a question and...
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
  • Exercise #3: Create the “MathTest” class. It will have two class variables: 1) a question and...

    Exercise #3: Create the “MathTest” class. It will have two class variables: 1) a question and 2) the answer to that question. Exercise #4: Please create an accessor and mutator method for both of those variables, without which we would not be able to see or change the question or the answer. Exercise #5: There should be a constructor method. We will also have a “ToString” method, which will print the question followed by its answer. The constructor method has...

  • Using C# Create an instance of the “TestQuestion” class in your “main” method. Allow the user...

    Using C# 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...

  • Urgent must be done in C# create an instance of the “ testquestion” class in your main method. al...

    Urgent must be done in C# create an instance of the “ testquestion” class in your main method. allow the uswr to enter the teat question and its answer, it they want to. if they do not, then continue by creating a default queatuon and answer. please use the constructor methods here. Then, using the “tostring or _str_method, have it print out those variables as decribed above. call the “check answer” method and allow the user to continue attempting to...

  • QUESTION 3 (20) Create a constructor class Sum that has public integer variables num1 and num2....

    QUESTION 3 (20) Create a constructor class Sum that has public integer variables num1 and num2. Include a default constructor and a method Add that accepts the two integer parameters, calculates and outputs the sum. In your main class Calculate include a main method that prompts the user to enter 2 integer values and create a reference called total for the constructor Sum. Output the values entered by the user and the sum of the values in the command line...

  • You will need to first create an object class encapsulating a Trivia Game which INHERITS from...

    You will need to first create an object class encapsulating a Trivia Game which INHERITS from Game. Game is the parent class with the following attributes: description - which is a string write the constructor, accessor, mutator and toString methods. Trivia is the subclass of Game with the additional attributes: 1. trivia game id - integer 2. ultimate prize money - double 3. number of questions that must be answered to win - integer. 4. write the accessor, mutator, constructor,...

  • Exercise #3: Create a class to represent a dog. Name the class “Dog”. It will contain...

    Exercise #3: Create a class to represent a dog. Name the class “Dog”. It will contain three (3) class attributes, its bark, size, and cuteness. The bark will be the sound of the dog’s bark, as in “woof!”. Its size will be how tall the dog is from the ground, and that number should always be between 6 and 44 inches. The cuteness is a string describing how cute the dog is, based on this scale: “ugliest dog ever!” “pretty...

  • C# programming 50 pts Question 2:2 1- Create the base class Book that has the following instance variables, constructor, and methods title (String) isbn (String) authors (String) publisher (Strin...

    C# programming 50 pts Question 2:2 1- Create the base class Book that has the following instance variables, constructor, and methods title (String) isbn (String) authors (String) publisher (String) edition ( int) published year (int) Constructor that takes all of the above variables as input parameters. set/get methods ToString method// that return sting representation of Book object. 2-Create the sub class New_Book that is derived from the base class Book and has the following instance variables, constructor, and methods: title...

  • In a project named 'DogApplication', create a class called 'Dog' 1. declare two instance variables in...

    In a project named 'DogApplication', create a class called 'Dog' 1. declare two instance variables in 'Dog' : name (String type) age (int type) 2. declare the constructor for the 'Dog' class that takes two input parameters and uses these input parameters to initialize the instance variables 3. create another class called 'Driver' and inside the main method, declare two variables of type 'Dog' and call them 'myDog' and 'yourDog', then assign two variables to two instance of 'Dog' with...

  • FOR C++ PLEASE Create a class called "box" that has the following attributes (variables): length, width,...

    FOR C++ PLEASE Create a class called "box" that has the following attributes (variables): length, width, height (in inches), weight (in pounds), address (1 line), city, state, zip code. These variables must be private. Create setter and getter functions for each of these variables. Also create two constructors for the box class, one default constructor that takes no parameters and sets everything to default values, and one which takes parameters for all of the above. Create a calcShippingPrice function that...

  • please write in Java and include the two classes Design a class named Fan (Fan.java) to...

    please write in Java and include the two classes Design a class named Fan (Fan.java) to represent a fan. The class contains: An int field named speed that specifies the speed of the fan. A boolean field named fanStatus that specifies whether the fan is on (default false). A double field named radius that specifies the radius of the fan (default 5). A string field named color that specifies the color of the fan (default blue). A no-arg (default) constructor...

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
ADVERTISEMENT