Question

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 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).

Exercise #6:

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.

Exercise #7:

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

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 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).

Exercise #6:

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.

Exercise #7:

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

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

I have implemented the question in java. You were not clear about what to be done in the main function. As per your requirement, I created the instance of the class and asked the user for input for the instance of to proceed the default values. If you need any changes please comment.

The code is:

import java.util.Scanner;
public class MathTest {
   //declaring the variables
   private String question;
   private String answer;
   //Mutator methods
   public void setQuestion(String q){
       question=q;
   }
   public void setAnswer(String a){
       answer=a;
   }
   //Accessor methods
   public String getQuestion(){
       return question;
   }
   public String getAnswer(){
       return answer;
   }
   //constructor with no arguments
   MathTest(){
       this.question="DefaultQuestion";
       this.answer="DefaultAnswer";
   }
   //constructor with arguments
   MathTest(String q, String a){
       this.question=q;
       this.answer=a;
   }
   // the ToString function
   public void ToString(){
       System.out.println(this.question + " " + this.answer);
   }
   // the checkanswer function
   public boolean CheckAnswer(String ans){
       if(ans.equals(this.answer))
           return true; //returns true if the answers match
       return false;
   }
   public static void main(String[] args) {
       MathTest mt; //instance creation
       Scanner s = new Scanner(System.in);
       int x; //To take input from user
       System.out.println("Enter 1 if you want to enter question and answer");
       System.out.println("Enter 2 if you want to continue with default values");
       x=s.nextInt();
       if(x==1 || x==2){//if enter one or two
           if(x==1){
               System.out.println("Enter the value for question");
               String ques = s.next();
               System.out.println("Enter the value for answer");
               String ans = s.next();
               mt = new MathTest(ques,ans);//taking input and calling parameterized constructor
           }
           else{
               mt = new MathTest();//calling non-parameterized constructor
           }
           System.out.println("Enter a string to check answer");
           String val=s.next();
           boolean comp = mt.CheckAnswer(val); //to check the checkanswer function
           System.out.println(comp);
           System.out.println("The ToString Function is : ");
           mt.ToString();// the ToString function
       }
       else // if no option is selected
           System.out.println("You did not select any");
      
   }

}
OUTPUT:

Add a comment
Know the answer?
Add Answer to:
Exercise #3: 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
  • 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...

  • Programming Assignment 1 Write a class called Clock. Your class should have 3 instance variables, one...

    Programming Assignment 1 Write a class called Clock. Your class should have 3 instance variables, one for the hour, one for the minute and one for the second. Your class should have the following methods: A default constructor that takes no parameters (make sure this constructor assigns values to the instance variables) A constructor that takes 3 parameters, one for each instance variable A mutator method called setHour which takes a single integer parameter. This method sets the value of...

  • I am stuck on this question. Write the class, GeoLocation.java. You can refer to Name.java used...

    I am stuck on this question. Write the class, GeoLocation.java. You can refer to Name.java used in the lecture to complete the below exercises. Do the following: 1. Create two instance variables, lat and lng, both of which should be doubles. 2. Write the default constructor. 3. Write the non-default constructor. 4. Write 2 accessor methods, one for each instance variable. 5. Write 2 mutator methods, one for each instance variable. 6. Write a method that will return the location...

  • Exercise 1: Create a class Resource. The class should have: a) Two private variables status and w...

    C++C++ Exercise 1: Create a class Resource. The class should have: a) Two private variables status and writeTo representing integer value either 0 or 1. b) One default constructor that initializes the status and writeTo to zero. c) One single parameterized constructor to initialize the writeTo variable. d) Two constant accessor functions per class that return the values of status e) Two mutator functions per class that set the values of status and writeTo One output (member) function that outputs...

  • 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...

  • Create a UML diagram to help design the class described in exercise 3 below. Do this...

    Create a UML diagram to help design the class described in exercise 3 below. Do this exercise before you attempt to code the solution. Think about what instance variables will be required to describe a Baby class object; should they be private or public? Determine what class methods are required; should they be private or public? Write Java code for a Baby class. A Baby has a name of type String and an age of type integer. Supply two constructors:...

  • Java Project Requirements: Account class Superclass Instance variables clearPassword String Must be at least 8 characters...

    Java Project Requirements: Account class Superclass Instance variables clearPassword String Must be at least 8 characters long encryptedPassword : String key int Must be between 1 and 10(inclusive) accountId - A unique integer that identifies each account nextIDNum – a static int that starts at 1000 and is used to generate the accountID no other instance variables needed. Default constructor – set all instance variables to a default value. Parameterized constructor Takes in clearPassword, key. Calls encrypt method to create...

  • 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...

  • Create an abstract class “Appointment” that represents an event on a calendar. The “Appointment” class will...

    Create an abstract class “Appointment” that represents an event on a calendar. The “Appointment” class will have four instance variables: • An instance variable called “year” which will be of type int • An instance variable called “month” which will be of type int • An instance variable called “day” which will be of type int • An instance variable called “description” which will be of type String The “Appointment” class must also implement the following methods: • A getter...

  • 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...

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