import java.util.Scanner;

public class MathTutor {
    Scanner input = new Scanner(System.in);
        int numQuestions;
        int choice;
        int numCorrect;
       
        public MathTutor (){
            numQuestions = choice = numCorrect = 0;
        }
       
       public int getNumQuestions(){  {
            System.out.print("How many questions? ");
            numQuestions = input.nextInt();
            if (numQuestions < 1)
            {
                System.out.println("Invalid - must ask at least 1 question");
            }
        }return numQuestions;}
        public int getQuestionType(){ while (choice < 1 || choice > 4)
        {
            System.out.println("Please select the type of questions you would like");
            System.out.println("1) Addition");
            System.out.println("2) Subtraction");
            System.out.println("3) Multiplication");
            System.out.println("4) Division");
            System.out.print("Type: ");
            choice = input.nextInt();
            if (choice < 1 || choice > 4)
            {
                System.out.println("Choice must be in the range 1-4");
            }
        }return choice;}

    public void askQuestions() {
        for (int i = 0; i < numQuestions; i++)
        {
            int num1 = genRandomNum(1);
            int num2 = genRandomNum(1);
            int sign = choice;
            if (sign == 5)
            {
                sign = (int)(Math.random() * 4 + 1);
            }
            switch(sign)
            {
                case 1: addition(num1, num2);
                    break;
                case 2: subtraction(num1, num2);
                    break;
                case 3: multiplication(num1, num2);
                    break;
                case 4: division(num1, num2);
                    break;
                default: System.out.println("Error");
                    System.exit(1);
            }
        }
    }

    public int genRandomNum(int difficulty) {
        if (difficulty == 1)
        {
            return (int)(Math.random() * 10);
        }
        else
        {
            return (int)(Math.random() * 100);
        }
    }
   
    public void addition(int num1, int num2) {
        System.out.print("What is " + num1 + " + " + num2 + "? ");
        int answer = input.nextInt();
        if (num1 + num2 == answer)
        {
            System.out.println("Correct");
            numCorrect++;
        }
        else
        {
            System.out.println("Incorrect -> " + num1 + " + " + num2 + " = " + num1+num2);
        }
    }
   
    public void subtraction(int num1, int num2) {
        System.out.print("What is " + num1 + " - " + num2 + "? ");
        int answer = input.nextInt();
        if (num1 - num2 == answer)
        {
            System.out.println("Correct");
            numCorrect++;
        }
        else
        {
            System.out.println("Incorrect -> " + num1 + " - " + num2 + " = " + num1+num2);
        }
    }
   
    public void multiplication(int num1, int num2) {
        System.out.print("What is " + num1 + " * " + num2 + "? ");
        int answer = input.nextInt();
        if (num1 * num2 == answer)
        {
            System.out.println("Correct");
            numCorrect++;
        }
        else
        {
            System.out.println("Incorrect -> " + num1 + " * " + num2 + " = " + num1+num2);
        }
    }
   
    public void division(int num1, int num2) {
        boolean quotientCorrect = false;
        boolean remainderCorrect = false;
        System.out.print("What is the quotient of " + num1 + " / " + num2 + "? ");
        int answer = input.nextInt();
        System.out.print("What is the remainder of " + num1 + " / " + num2 + "? ");
        int remainder = input.nextInt();
        if (num1 / num2 == answer)
        {
            quotientCorrect = true;
        }
        if (num1 % num2 == remainder)
        {
            remainderCorrect = true;
        }
        if (quotientCorrect && remainderCorrect)
        {
            System.out.println("Correct");
            numCorrect++;
        }
        else
        {
            System.out.println("Incorrect -> " + num1 + " / " + num2 + " = " + num1/num2 + "r" + num1%num2);
        }
    }

    public void printReport(long totalTime) {
        System.out.println("Number of correct responses: " + numCorrect + "/" + numQuestions);
        double percent = numCorrect/numQuestions;
        System.out.println("Your percentage: " + percent + "%");
        System.out.println("Total time taken: " + totalTime/1000 + "s");
        System.out.println("Average time per question: " + (totalTime/numQuestions)/1000 + "s");
    }
}

This is what I have so far.... I think. Here's my assignment:


Create a Java class that will help elementary school students learn basic arithmetic operations (addition, subtraction, multiplication, and division). The class will contain a menu that allows the user to choose the operation. It will then use a Random object to produce two positive one-digit integers. It will then prompt the user with a question, such as

How much is 6 plus 4?

The student will input the answer. Another method will verify if the answer is correct. Use another Random object to generate a number in the range of 1-4. If the student's answer is correct, the random number will be used to indicate which message to display from a String array of correct responses:

Very good!
Excellent!
Nice work!
Keep up the good work!

If the student's answer is incorrect use the random number to indicate which element of the incorrect responses String array to display:

No. Please try again.
Wrong. Try once more.
Don't give up!
No. Keep trying.

For each operation that the student selects, they are to answer 10 questions. The program should monitor the students's performance for that particular math operation and display the percentage that the user answered correctly. If the percentage is lower than 75%, display "Please ask your teacher for extra help with __________."

Allow the user to return to the menu after finishing an operation, so they can choose to try another math operation or quit the program.

Note: this assignment contains two classes: The arithmetic program and a client.


I'm in the middle of getting the grading program/set amount of questions/varied correct and incorrect responses added to the program. My main issue is... No matter what I do in my client, I can't seem to get this to run.... Horribly enough, at this stage of sleep depravation, I'm not even making sense of why this program would want to be separated into client/main file, or even how to make a client for something that doesn't even need anything set/inputted except for the number of questions you want the student to do. Can anyone give me a push in the right direction?

Thanks
~Will

Recommended Answers

All 4 Replies

Do you have a main method to run the class math tutor? or are you asking why you need a main method.

You need a main method to run the class, like in the class Math Tutor, have a method Run(), that runs through the whole program.

I wanted to test your code so I did a quick Driver(main method)

public static void main(String args[])
        {
        	MathTutor tutor = new MathTutor();
        	
        	int NumQues = tutor.getNumQuestions();
        	int choice = tutor.getQuestionType();
        	
        	tutor.askQuestions();
        	
        	tutor.printReport(1000);
        }

Note The totalTime is messed up, I just gave it one thousand, I dont know how you wanna calculate it.

tutor.printReport(1000);

This was the output of what I did

How many questions? 5
Please select the type of questions you would like
1) Addition
2) Subtraction
3) Multiplication
4) Division
Type: 1
What is 6 + 1? 7
Correct
What is 6 + 5? 11
Correct
What is 4 + 0? 4
Correct
What is 2 + 6? 8
Correct
What is 2 + 0? 0
Incorrect -> 2 + 0 = 20
Number of correct responses: 4/5
Your percentage: 0.0%
Total time taken: 1s
Average time per question: 0s

**Note number five, I wanted to see what a wrong answer looked like, you'll have to edit the code because this is what it shows.

What is 2 + 0? 0
Incorrect -> 2 + 0 = 20

Thank you SO MUCH. This seriously saved my ass for the project; here's my final code. It should be fine now; I tested it in every way I could. Netbeans still claims errors, though; do you have any suggestions?

import java.util.Scanner;
import java.util.Random;

public class MathTutor {
    Scanner input = new Scanner(System.in);
        int choice;
        int numCorrect;
        Random generator = new Random();

        public MathTutor (){
            choice = numCorrect = 0;
        }

        public int getQuestionType(){ while (choice < 1 || choice > 4)
        {
            System.out.println("Please select the type of questions you would like");
            System.out.println("1) Addition");
            System.out.println("2) Subtraction");
            System.out.println("3) Multiplication");
            System.out.println("4) Division");
            System.out.print("Type: ");
            choice = input.nextInt();
            if (choice < 1 || choice > 4)
            {
                System.out.println("Choice must be in the range 1-4");
            }
        }return choice;}

    public void askQuestions() {
        for (int i = 0; i < 10; i++)
        {
            int num1 = genRandomNum();
            int num2 = genRandomNum();
            int num3 = genRandomNum();
            int sign = choice;
            if (sign == 5)
            {
                sign = (int)(Math.random() * 4 + 1);
            }
            switch(sign)
            {
                case 1: addition(num1, num2);
                    break;
                case 2: subtraction(num1, num2);
                    break;
                case 3: multiplication(num1, num2);
                    break;
                case 4: division(num1, num2, num3);
                    break;
                default: System.out.println("Error");
                    System.exit(1);
            }
        }
    }

    public int genRandomNum() {
            return (int)generator.nextInt(9) +1;
    }
    public void genMotivationalPhrase(){
         int numMo = (int)generator.nextInt(4)+1;
                     switch(numMo)
            {
                case 1: System.out.println("Great job!");
                    break;
                case 2: System.out.println("I knew you could do it!");
                    break;
                case 3: System.out.println("Math is easy!");
                    break;
                case 4: System.out.println("Hello MIT!");
                    break;
                default: System.out.println("Error");

        }}
        public void genMotivationalWrPhrase(){
         int numMo = (int)generator.nextInt(4)+1;
                     switch(numMo)
            {
                case 1: System.out.println("Try again!");
                    break;
                case 2: System.out.println("So close!");
                    break;
                case 3: System.out.println("I know you can do it!");
                    break;
                case 4: System.out.println("You'll get it next time!");
                    break;
                default: System.out.println("Error");

        }}


    public void addition(int num1, int num2) {
        System.out.print("What is " + num1 + " + " + num2 + "? ");
        int answer = input.nextInt();
        if (num1 + num2 == answer)
        {
            genMotivationalPhrase();
            numCorrect++;
        }
        else
        {
            genMotivationalWrPhrase();
        }
    }

    public void subtraction(int num1, int num2) {
        if(num1>num2){
        System.out.print("What is " + num1 + " - " + num2 + "? ");
        int answer = input.nextInt();
        if (num1 - num2 == answer)
        {
            genMotivationalPhrase();
            numCorrect++;
        }
        else
        {
            genMotivationalWrPhrase();
        }
    }

        else{

            System.out.print("What is " + num2 + " - " + num1 + "? ");
        int answer = input.nextInt();
        if (num2 - num1 == answer)
        {
            genMotivationalPhrase();
            numCorrect++;
        }
        else
        {
           genMotivationalWrPhrase();
        }
    }
    }

    public void multiplication(int num1, int num2) {
        System.out.print("What is " + num1 + " * " + num2 + "? ");
        int answer = input.nextInt();
        if (num1 * num2 == answer)
        {
            genMotivationalPhrase();
            numCorrect++;
        }
        else
        {
           genMotivationalWrPhrase();
        }
    }

    public void division(int num1, int num2, int num3) {
        boolean quotientCorrect = false;
        num3 = num1*num2;
        System.out.print("What is the quotient of " + num3 + " / " + num1 + "? ");
        int answer = input.nextInt();
        if (answer == num3/num1);
        {
            quotientCorrect = true;
        }

       
        if (quotientCorrect)
        {
            genMotivationalPhrase();
            numCorrect++;
        }
        else
        {
            genMotivationalWrPhrase();
        }
    }

    public void printReport() {
        double win = 10.0;
        System.out.println("Number of correct responses: " + numCorrect + "/" + 10);
        double percent = numCorrect / win * 100 ;
        System.out.println("Your percentage: " + percent + "%");
        if(percent<75){
            System.out.println("Please see your teacher for help with ");
        if(choice==1)
            System.out.println("Addition");
        else if(choice==2)
            System.out.println("Subtraction");
        else if(choice==3)
            System.out.println("Multiplication");
        else if(choice==4)
            System.out.println("Division");}
    }
}








public class MathTutorClient {
public static void main(String args[])
        {
            MathTutor tutor = new MathTutor();

            int choice = tutor.getQuestionType();

            tutor.askQuestions();

            tutor.printReport();
        }
}

Thanks again for your help!

I'm not seeing any errors.

I'm not seeing any errors.

Thank you so much. I'm sending it in with a few small changes.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.