Lab3: Computer Assisted Instruction Part1

As computer costs decline, it becomes more feasible for every student, regardless of economic circumstance, to have a computer and use it in school. This creates exciting opportunities for improving the educational experience of children everywhere. The use of computers in education is referred to as Computer Assisted Instruction. 

Lab3: class Question and class QuestionTest

Create a Question class that has the following properties as instance variables

Constructors

The class should have 3 constructors: default constructor that sets difficulty and type to 0. A constructor that takes the question type and sets difficulty to 0. And lastly, a constructor that takes both question type and dififculty. If the type or difficulty is out of range, set them to 0. Use delegating constructors. Call the parameterized constructor using default values like this(0, 0).

public Question()     // default constructor should set both difficulty and type to 0.

public Question(int questionType)        // selects the difficulty randomly

public Question(int questionType, int difficulty)

Controlling Question Type and Difficulty Level

The valid question types are 1 = ADDITION, 2 = SUBTRACTION, 3 = MULTIPLICATION, 4 = DIVISION.  For a mixture, use type=0. These constants are declared to be public static final int.

A difficultly level of 1 will use single digit numbers in the questions. A difficulty level of 2 will use two digit numbers in the questions up to difificulty level 4. Use the Random number generator to generate the operands for the questions. SEED it with 13. If the user enters a difficulty level less than 0 or greater than 4, default it to 0.

When the difficulty is 0, select the difficulty at random in createQuestion method using the randomNumbers singleton to select a number from 1-4. When the question type is 0, select the question type using the randomNumbers singleton to select a number from 1-4.

Access to Question obect properties

Implement accessor functions for all of the instance variables:

public int getQuestionType()

public int getDifficultyLevel()

public int getOperand1()

public int getOperand2()

public double getAnswer()

public String getQuestion()

public double getResponse()

Implement mutator functions for the following instance variables:

public void setQuestionType(int questionType)

public void setDifficultyLevel(int difficultyLevel)

Other Question Instance Methods

Implement a public Question createQuestion method that uses the settings in the object to create the operands, the question and the answer. Remember that the operands must be numbers computed randomly based on difficultyLevel. Use the getRandomNumberInRange method to get the operands.

Implement a public boolean checkAndSetRespone method that returns true if the response is correct, false otherwise. Remember that computations that yield a double needs to consider precision. Use a range of values between +- .000001 to compare.

public Question createQuestion()                        // Creates a question

public boolean checkAndSetResponse(double response)     // Check and set the response 

public String toString()                                // Produce a printable string

// Combine question + " It's " + answer when the response was correct

// Combine question + " It's " + answer + " not " + response when the response was not.

Other Question Class Methods

Implement a public static method to create a question object using the given difficultyLevel and questionType. This will be a static factory method. Use the getRandomNumberInRange to get the operands given a difficultyLevel. [HINTS in the starter code.]

public static Question createQuestion(int difficulty, int type) 
// Constructs a new Question using the input parameters then calls the instance method createQuestion()

public static int getRandomNumberInRange(int start, int end) // Already implemented

class QuestionTest

Keep the communication with the user in the main function of the class QuestionTest. Implement the program in QuestionTest in public static void main(String[] args). A public static method called createResponse(boolean correct) has been implemented for you. A randomNumbers generator has been created and SEEDed with 13 so that it matches the output of the model code.

Declare and allocate an array of 5 Question objects. In the for-loop, use the input to construct a Question object. Print out the question for the user and read the answer. If the answer is correct, print one of the accolades, otherwise print a gentle encouragement. The code prompts from QuestionTest.java are given below. 

QuestionTest Driver

// TODO: Declare and allocate an array of Question objects.

for (int i = 0; i < 5; i++)
{
// prompts the user to enter a grade level
System.out.print("Enter the difficulty level (1=single digits, 2=two digits, etc.): ");

// TODO: Read in the difficulty Level. Let the Question class handle the correctness of the inputs

System.out.print("Enter the question type (0=Any, 1=Addition, 2=Subraction, 3=Multiplication, 4=Division): ");

// TODO: Read in the question type. Let the Question class handle the correctness of the inputs

// TODO: Construct the Question object given the difficultyLevel and questionType.

// TODO: Output the question.

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

// Input the response.
double guess = input.nextDouble();


// TODO: Use checkAndSetResponse to determine if correct or not and pass in the boolean to createResponse.
// TODO: print out the String that is returned from createResponse.


// TODO: assign the question into the array.

}

// TODO: Loop and print the questions in the array.

Sample Output

Enter the difficulty level (1=single digits, 2=two digits, etc.): 2
Enter the question type (0=Any, 1=Addition, 2=Subraction, 3=Multiplication, 4=Division): 4
How much is 46 divided by 23?
Enter your answer: 2
Nice work!
Enter the difficulty level (1=single digits, 2=two digits, etc.): 1
Enter the question type (0=Any, 1=Addition, 2=Subraction, 3=Multiplication, 4=Division): 3
How much is 1 times 1?
Enter your answer: 1
Excellent!
Enter the difficulty level (1=single digits, 2=two digits, etc.): 3
Enter the question type (0=Any, 1=Addition, 2=Subraction, 3=Multiplication, 4=Division): 2
How much is 704 minus 595?
Enter your answer: 109
Excellent!
Enter the difficulty level (1=single digits, 2=two digits, etc.): 4
Enter the question type (0=Any, 1=Addition, 2=Subraction, 3=Multiplication, 4=Division): 1
How much is 7832 plus 4972?
Enter your answer: 12804
Very good!
Enter the difficulty level (1=single digits, 2=two digits, etc.): 0
Enter the question type (0=Any, 1=Addition, 2=Subraction, 3=Multiplication, 4=Division): 4
How much is 4435 divided by 9831?
Enter your answer: .43
No. Please try another.

Here are the questions that you answered:
How much is 46 divided by 23?
 It's 2.00
How much is 1 times 1?
 It's 1.00
How much is 704 minus 595?
 It's 109.00
How much is 7832 plus 4972?
 It's 12804.00
How much is 4435 divided by 9831?
 It's 0.45 not .43