Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: need help w/ arithmatic program

  1. #1
    Junior Member
    Join Date
    Oct 2010
    Posts
    14
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Angry need help w/ arithmatic program

    I have a major project that i need a good grade on... here's the project and following is my code.
    My problem is that I am unable to make it try the question 3 times if it is answered wrong, and that i don't understand how to track the percent answered correctly. If you want to help on extra credit that would be awesome, but i'm mainly worried about the main project.
    Thanks,
    Brandon

    You have been hired by Easy Street High School to write a basic computer-assisted instruction math drill program to help elementary students to learn basic math skills. Your program should meet the following specifications:
    Write a MathDrill Class to implement the following tasks. The program that will print the following output and wait for the user to select the drills.
    Easy CAI Math Drills

    1. Addition
    2. Subtraction
    3. Multiplication
    4. Division

    Please select the drill you want to take, or Q to quit:

    Ask 10 questions of the math drill questions based on the user preference. Use Random class to generate two integers between 0 to 9. For example, if the user chooses 3, you program should then display a question like the following: (Here boldface is what the user enter as input to the program.)
    Problem 1 of 10
    8 * 9 = 72

    If the user answers correctly, choose randomly from one of the following responses and output it.
    Very good!
    Excellent!
    Nice work!
    Keep up the good work!

    If the user answers incorrectly, choose randomly from one of the following responses and output it.
    No. Please try again.
    Wrong. Try once more.
    Don't give up!
    No. Keep trying.

    Allow the student three chances to get the problem correct. If the student did not answer correctly 3 times for the same question, print the message "Incorrect Solution", and prints the correct solution for the problem. Here is a sample interactive session with the program you are to write. (Here boldface is what the user enter as input to the program.)

    Easy CAI Math Drills

    1. Addition
    2. Subtraction
    3. Multiplication
    4. Division

    Please select the drill you want to take, or Q to quit: 3

    Problem 1 of 10
    8 * 9 = 61
    No. Keep trying.
    8 * 9 = 89
    Wrong. Try once more.
    8 * 9 = 90
    Incorrect Solution
    Solution is: 8 * 9 = 72

    Problem 2 of 10
    6 * 4 = 24
    Nice work!

    Problem 3 of 10
    8 * 6 = 48
    Excellent!
    .
    .
    .



    After the student answers 10 questions, your program should calculate the number of correct responses and the percentage of correct responses and output both of them. If the percentage is lower than 75 percent, print Please ask your instructor for extra help. Then, display the Math Drill menu again.
    You need to take care of the divide by 0 error. If the random generated denominator is 0, your program will regenerate a non zero denominator. You will need to set up a loop for it.
    Extra Credits 1(10 points):
    Modify the program to allow the user to pick number 5 from the menu which means a random mixture of problems of all four types math drill.

    Extra Credits 2(10 points):
    Modify the program to allow the user to enter a school grade-level capability. A grade level of 1 means that the program should use only single-digit numbers in the problems, a grade level of 2 means that the program should use numbers as large as two digits, and a grade level of 3 means that the program shuld use numbers as large as three digits.

    Extra Credits 3(10 points):
    Modify the program to use double/floating point instead of integer. The student's answer is considered to be correct by rounding up to the nearest hundredth.

    Programming Style requirement: You should use OOP design as much as possible. Comment your program thoroughly using javadoc style comment. Use javadoc to create the html files. Please keep proper indentation.

    import java.util.Scanner;
    import java.util.Random;
     
    public class MathDrill {
     
        public void doOperation (int choice)
        { 	
     
           Random rand1=new Random();   //to store first random number on it
           Random rand2=new Random();   //second random number       
          // Random ex = new Random();
           int input=0;   //store user answer for question
           Scanner scan= new Scanner(System.in); //scan user input
     
           String [] correct = {"Very Good","Excellent","Nice Work !","Keep up the good work"};
           String [] wrong = {"No. Please try again.","Wrong. Try once more.","Don't give up!","No. Keep trying."};
     
           for (int i=1;i<11;i++)
     
            {
        	   	int num1 = rand1.nextInt(11);   //here we create a new random number using Math.random function it creates a random number between 0-->1 so we have to multiply by 10 and convert to integer to remove the part after the floating point
                int num2 = rand2.nextInt(11); // here we create second random number
                int x = (int) (Math.random() * 10); //this random number is for correct or wrong random statement stores in the arrays of strings
                          while (x>3)  //to not to exceed array of strings depth.                    	  
                          {
                              x = (int) (Math.random() * 10);                        
                          }
                  int result=0;  //here we will store result4 of operation later
                  System.out.println("Question "+i +" of 10"); //print out question number
                  if (choice==1)
     
                  {
                      result=num1+num2;   //the real result
     
                      System.out.println(num1+" + "+num2 + " = "); //showing question to user
                      input = scan.nextInt(); //scan user answer
     
                      if (input!=result)   
                      {
                          System.out.println(wrong[x]);
                      }                               	  
     
                      else
                      {
                           System.out.println(correct[x]);
                      }
                  }
                  //here we repeat the same pattern for other operations.
                  else if (choice==2)
                  {
                      result=num1-num2;
                      System.out.println(num1+" - "+num2 + " = ");
                      input = scan.nextInt();
                      if (input!=result)
                      {
                          System.out.println(wrong[x]);
                      }
                      else
                      {
                           System.out.println(correct[x]);
                      }
                  }
                  else if (choice==3)
                  {
                      result=num1*num2;
                      System.out.println(num1+" * "+num2 + " = ");
                      input = scan.nextInt();
                      if (input!=result)
                      {
                          System.out.println(wrong[x]);
                      }
                      else
                      {
                           System.out.println(correct[x]);
                      }
                  }
                  else if (choice==4)
                  {
                    if(num2!=0)
                    {
    				result=num1/num2;     
                    }						
     
                      System.out.println(num1+" / "+num2 + " = ");                  
     
                      input = scan.nextInt();
                      if (input!=result)
                      {
                          System.out.println(wrong[x]);
                      }
                      else
                      {
     
                    	  System.out.println(correct[x]);                	  
                      }
     
     
                  }   
     
            }     
        }
       // public void getPercent{
        //	int count;
     
     
        }


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,357
    My Mood
    Hungover
    Thanks
    144
    Thanked 626 Times in 537 Posts

    Default Re: need help w/ arithmatic program

    Break your problem (and question) up into smaller pieces. Pick a specific part that you're having trouble with, and focus on that one piece. When you have each piece working individually, then worry about combining them.

  3. #3
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: need help w/ arithmatic program

    I have a major project that i need a good grade on... here's the project and following is my code.
    My problem is that I am unable to make it try the question 3 times if it is answered wrong, and that i don't understand how to track the percent answered correctly. If you want to help on extra credit that would be awesome, but i'm mainly worried about the main project.
    Thanks,
    Brandon

    You have been hired by Easy Street High School to write a basic computer-assisted instruction math drill program to help elementary students to learn basic math skills. Your program should meet the following specifications:
    Write a MathDrill Class to implement the following tasks. The program that will print the following output and wait for the user to select the drills.
    Easy CAI Math Drills

    1. Addition
    2. Subtraction
    3. Multiplication
    4. Division

    Please select the drill you want to take, or Q to quit:

    Ask 10 questions of the math drill questions based on the user preference. Use Random class to generate two integers between 0 to 9. For example, if the user chooses 3, you program should then display a question like the following: (Here boldface is what the user enter as input to the program.)
    Problem 1 of 10
    8 * 9 = 72

    If the user answers correctly, choose randomly from one of the following responses and output it.
    [index 0]Very good!
    [index 1] Excellent!
    [index 2]Nice work!
    [index 3]Keep up the good work!

    If the user answers incorrectly, choose randomly from one of the following responses and output it.
    [index 0] No. Please try again.
    [index 1]Wrong. Try once more.
    [index 2]Don't give up!
    [index 3] No. Keep trying.

    Allow the student three chances to get the problem correct. If the student did not answer correctly 3 times for the same question, print the message "Incorrect Solution", and prints the correct solution for the problem. Here is a sample interactive session with the program you are to write. (Here boldface is what the user enter as input to the program.)

    Easy CAI Math Drills

    1. Addition
    2. Subtraction
    3. Multiplication
    4. Division

    Please select the drill you want to take, or Q to quit: 3

    Problem 1 of 10
    8 * 9 = 61
    No. Keep trying.
    8 * 9 = 89
    Wrong. Try once more.
    8 * 9 = 90
    Incorrect Solution
    Solution is: 8 * 9 = 72

    Problem 2 of 10
    6 * 4 = 24
    Nice work!

    Problem 3 of 10
    8 * 6 = 48
    Excellent!
    .
    .
    .



    After the student answers 10 questions, your program should calculate the number of correct responses and the percentage of correct responses and output both of them. If the percentage is lower than 75 percent, print Please ask your instructor for extra help. Then, display the Math Drill menu again.
    You need to take care of the divide by 0 error. If the random generated denominator is 0, your program will regenerate a non zero denominator. You will need to set up a loop for it.
    Extra Credits 1(10 points):
    Modify the program to allow the user to pick number 5 from the menu which means a random mixture of problems of all four types math drill.

    Extra Credits 2(10 points):
    Modify the program to allow the user to enter a school grade-level capability. A grade level of 1 means that the program should use only single-digit numbers in the problems, a grade level of 2 means that the program should use numbers as large as two digits, and a grade level of 3 means that the program shuld use numbers as large as three digits.

    Extra Credits 3(10 points):
    Modify the program to use double/floating point instead of integer. The student's answer is considered to be correct by rounding up to the nearest hundredth.

    Programming Style requirement: You should use OOP design as much as possible. Comment your program thoroughly using javadoc style comment. Use javadoc to create the html files. Please keep proper indentation.

    import java.util.Scanner;
    import java.util.Random;
     
    public class MathDrill {
     
        public void doOperation (int choice)
        { 	
     
           Random rand1=new Random();   //to store first random number on it
           Random rand2=new Random();   //second random number       
          // Random ex = new Random();
           int input=0;   //store user answer for question
     
     
           Scanner scan= new Scanner(System.in); //scan user input
     
           String [] correct = {"Very Good","Excellent","Nice Work !","Keep up the good work"};
           String [] wrong = {"No. Please try again.","Wrong. Try once more.","Don't give up!","No. Keep trying."};
     
           for (int i=1;i<11;i++)
     
            {
        	   	int num1 = rand1.nextInt(11);   //here we create a new random number using Math.random function it creates a random number between 0-->1 so we have to multiply by 10 and convert to integer to remove the part after the floating point
                int num2 = rand2.nextInt(11); // here we create second random number
    // what if num2 =0 and you're operation is /?  You'll have a problem there.
    // also, if result is an int, if you have even 2/3, it'll expect them to put 0, not .6666666666666666
     
                int x = (int) (Math.random() * 10); //this random number is for correct or wrong random statement stores in the arrays of strings
                          while (x>3)  //to not to exceed array of strings depth.                    	  
                          {
                              x = (int) (Math.random() * 10);                        
                          }
                  int result=0;  //here we will store result4 of operation later
                  System.out.println("Question "+i +" of 10"); //print out question number
    // choice in not defined
    // will make error "cannot find symbol: choice"
     
     
                  if (choice==1)
     
                  {
                      result=num1+num2;   //the real result
     
                      System.out.println(num1+" + "+num2 + " = "); //showing question to user
                      input = scan.nextInt(); //scan user answer
     
                      if (input!=result)   
                      {
                          System.out.println(wrong[x]); // Here's a problem.  If x =9, and you only have up to index 3, // you're gonna get an IndexOutOfBoundsException. In fact, if x > 3 you're going to get an                         // IndexOutOfBoundsException.
     
                      }                               	  
     
                      else
                      {
                           System.out.println(correct[x]);
    // again, you will likely get an IndexOutOfBoundsException as you could get an x greater than 3.
                      }
                  }
                  //here we repeat the same pattern for other operations.
                  else if (choice==2)
                  {
                      result=num1-num2;
                      System.out.println(num1+" - "+num2 + " = ");
                      input = scan.nextInt();
                      if (input!=result)
                      {
                          System.out.println(wrong[x]);
                      }
                      else
                      {
                           System.out.println(correct[x]);
                      }
                  }
                  else if (choice==3)
                  {
                      result=num1*num2;
                      System.out.println(num1+" * "+num2 + " = ");
                      input = scan.nextInt();
                      if (input!=result)
                      {
                          System.out.println(wrong[x]);
                      }
                      else
                      {
                           System.out.println(correct[x]);
                      }
                  }
                  else if (choice==4)
                  {
                    if(num2!=0)
                    {
    				result=num1/num2;     
                    }						
     
                      System.out.println(num1+" / "+num2 + " = ");                  
     
                      input = scan.nextInt();
                      if (input!=result)
                      {
                          System.out.println(wrong[x]);
                      }
                      else
                      {
     
                    	  System.out.println(correct[x]);                	  
                      }
     
     
                  }   
     
            }     
        }
       // public void getPercent{
        //	int count;
     
     
        }

    1. Addition
    2. Subtraction
    3. Multiplication
    4. Division

    Please select the drill you want to take, or Q to quit:
    char drills = 'c';
     
    String str = "";
     
    str = console.nextLine();
     
    drills = str.charAt(0);
     
    if (drills == '1')
    {
     
    }
     
    else if (drills == '2')
    {
     
    }
     
    else if (drills == '3')
    {
     
    }
     
    else if (drills == '4')
    {
     
    }
     
    else if (drills == 'Q' || drills == 'q')
    {
     
    }
     
    else
    {
     
    }
     
    }

  4. #4
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: need help w/ arithmatic program

    Ahhhh....I found choice.

    But you have another problem. A big problem.

    You're going to get an error called "NoSuchMethodError "main" "

    You don't have a main method.

  5. #5
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: need help w/ arithmatic program

    My problem is that I am unable to make it try the question 3 times if it is answered wrong, and that i don't understand how to track the percent answered correctly
    Well, it's simple, if they get it right within one of the three tries, add 1 to the one correct
    else
    don't add 1 to the ones correct.

    Id recommend creating ten int values.

    int chances = 1;
    int chances2 = 1;
    int chances3 = 1;
    int chances4 = 1;
    int chances5 =1;
    int chances6 = 1;
    int chances7 = 1;
    int chances8 = 1;
    int chances9 = 1;
    int chances10 = 1;
    boolean isCorrect = false;
    boolean isCorrect2 = false;
    boolean isCorrect3 = false;
    boolean isCorrect4 = false;
    boolean isCorrect5 = false;
    boolean isCorrect6 = false;
    boolean isCorrect7 = false;
    boolean isCorrect8 = false;
    boolean isCorrect9 = false;
    boolean isCorrect10 = false;
    and for every question do this:

    Here's what you'd do for the first one

    while (chances <=3 && isCorrect == false)
    {
    // ask question
     
    if (correct)
    isCorrect = true;
     
    else
    isCorrect = false;
    chances = chances+ 1;
    }
     
    if (isCorrect == true)
    {
    answer is correct so add to total of correct
    }
     
    else
    answer is incorrect

  6. #6
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: need help w/ arithmatic program

    int num2 = rand2.nextInt(11);


    Ok, I think this should work, I hope.

    if (choice ==4)
    {
     
    while (num2 ==0)
    {
    num2 = rand2.nextInt(11);
    }
     
    }

    Also, to fix your IndexOutOfBoundsExceptions, I have another idea, though I'm not 100% sure if this will work.
    Random r3 = new Random(); // for correct
    Random r4 = new Random(); // for wrong
    int x = 0;
    int y = 0;
    while (chances <=3 && isCorrect == false)
    {
    // ask question
     
    if (correct)
    {
    x = r3.nextInt(4);
    System.out.println(correct[x]);
    isCorrect = true;
    }
     
    else
    {
    isCorrect = false;
    y = r4.nextInt(4);
    System.out.println(wrong[y]);
    chances = chances+ 1;
    }
    }
     
    if (isCorrect == true)
    {
    answer is correct so add to total of correct
    }
     
    else
    answer is incorrect

  7. #7
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,357
    My Mood
    Hungover
    Thanks
    144
    Thanked 626 Times in 537 Posts

    Default Re: need help w/ arithmatic program

    Quote Originally Posted by javapenguin View Post
    Id recommend creating ten int values.
    I'm sorry, but that's pretty bad advice. Spoonfeeding like you seem to be attempting is unhelpful in general, and spoonfeeding bad code is even worse.

    OP- I'd be very skeptical about these suggestions if I were you. It might seem like less work to try to hack together these code pieces, but I promise it will be more work than if you actually try to work through the errors with us one at a time.

  8. The Following User Says Thank You to KevinWorkman For This Useful Post:

    javapenguin (November 30th, 2010)

Tags for this Thread