Method getIntBetween that gets user input for an integer within certain range. It receives a string, message, to be printed out as the prompt message, and two integer min and max that indicate the desired range of the input. It error-checks the user input, keeps asking for user input until a number within the range is entered. It then returns the number.
Method printMenu prints out the main menu and gets user input of the choice for a menu item. It calls on getIntBetween to get an input in between 1 and 4. It then returns the number to the calling method (in this case, the main method).
Method getRandom that generates a random number within certain range. It receives two integers, min and max, and returns a random number in the range [min, max].
Three methods additionTutor, substractionTutor and multiplicationTutor that implements the tutoring tools for the operators. For each operator, generate 10 random problems. Call on getRandom to generate random numbers within the range 0 to 20 to use in the problems. For each problem, ask user input for an answer, then check if the answer is correct. Give the correct answer if the user’s answer is wrong.
Sample Output (from one run of the program):
Welcome to Math Tutor!
1 - Addition
2 - Subtraction
3 - Multiplication
4 - Quit
Enter your choice of 1-4: 5
Wrong input! Must enter a number between 1 and 4!
Enter your choice of 1-4: 1
Addition Tutor...
What is 19 + 19? 38
You are correct!
What is 9 + 13? 21
Sorry, the correct answer is 22.
What is 14 + 8? 22
You are correct!
What is 19 + 4? 23
You are correct!
What is 12 + 2? 1
Sorry, the correct answer is 14.
What is 5 + 14? 19
You are correct!
What is 11 + 11? 22
You are correct!
What is 0 + 0? 0
You are correct!
What is 8 + 13? 21
You are correct!
What is 13 + 5? 18
You are correct!
Here's the code I have:
Expand|Select|Wrap|Line Numbers
- import java.util.Scanner;
- public class MathTutor
- {
- public static void main( String[] args )
- {
- int choice;
- choice = printMenu();
- while (choice != 4)
- {
- if ( choice == 1 )
- additionTutor();
- else if ( choice == 2 )
- substractionTutor();
- else if ( choice == 3 )
- multiplicationTutor();
- choice = printMenu();
- }
- }
- // method prints out the main menu and gets user choice for menu item
- public static int printMenu()
- {
- System.out.println( "Welcome to Math Tutor!" );
- System.out.println( "1 - Addtion" );
- System.out.println( "2 - Substraction" );
- System.out.println( "3 - Multiplication" );
- System.out.println( "4 - Quit" );
- // Write your code here
- }
- // method gets user input for an integer in between min and max
- public static int getIntBetween( String message, int min, int max )
- {
- // Write your code here
- }
- // method generates a random number in between min and max
- public static int getRandom( int min, int max )
- {
- // Write your code here
- }
- // method implements addition tutor
- public static void additionTutor()
- {
- System.out.println( "Addition Tutor...\n" );
- // Write your code here
- }
- // method implements substraction tutor
- public static void substractionTutor()
- {
- System.out.println( "Substraction Tutor...\n" );
- // Write your code here
- }
- // method implements multiplication tutor
- public static void multiplicationTutor()
- {
- System.out.println( "Multiplication Tutor...\n" );
- // Write your code here
- }
- }