• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 241
  • Last Modified:

Initialiser list and arrays

Add 2 new methods which are called to display each result ie. Cut the display of the dialog box from the each of the methods addition(), subtraction() and multiplication() and replace then with a call to either of the following static void methods:

correctAnswer()
incorrectAnswer()

To make the program more interesting , each of these two methods should contain an array initialised with a number of responses (an initialiser list) which are selected at random. So if an answer is correct, the response may be

Very good!, Excellent!, You are hot! etc etc.

Similarly for incorrect reponses.

Now the program is multi-player. At the beginning of the main method prompt for number of players

Now create an array of this size to hold doubles. This will hold each players percentage correct.

Now put a for loop around the rest of the main method to loop as many times as there are players.

Adjust the menu to indicate the player number

Just before the end of the for loop  store that players correct percentage in the array.

After the for loop  at the end of the main method put another for loop that reads the array and displays the percentages for each player.


Having trouble with the above. I'm more than have way done, and would appreciate some help on getting rest incorporated into my code :).

Here's what I have so far




import javax.swing.JOptionPane; // Needed for JOptionPane
 
public class KidsCalc
  {
  //Initialise result & count to 0
  public static int result = 0;
  public static int count = 0;
 
 
  public static void main(String[] args)
    {
     
       int userChoice = 0;
       String userChoiceStr;
        do
          {
              // user input prompt
               userChoiceStr =  JOptionPane.showInputDialog(null,
                       
                        "1. Practice addition\n"
                        + "2. Practice subtraction\n"
                        + "3. Practice multiplication\n"
                        + "4. Quit the program\n"
                        + "\n"
                        + "Enter your choice",
                       
                        "Enter your choice",
                        JOptionPane.QUESTION_MESSAGE);
                        // keeps track of wrong entry
                        try
                        {
                         userChoice = Integer.parseInt(userChoiceStr);
                      }
                         catch (NumberFormatException e)
                       {
                        }
       // switch statement to count user score                    
       switch (userChoice)
       {
       case 1:    
          ++count;
          addition ();
          break;
       case 2:
           ++count;
           subtraction ();
           break;
       case 3:
           ++count;
           multiplication ();
            break;
       case 4:        
            break;
        default:
         // prompt if user doesn't enter a number between 1-4  
        JOptionPane.showMessageDialog(null,
                            "Please enter a number between 1 and 4",
                            "Wrong entry",
                            JOptionPane.ERROR_MESSAGE);      
          }
 }
       // returns your percentage if input not equal to 4
        while ( userChoice != 4 );

    JOptionPane.showMessageDialog ( null ,"You got " +
                                  (result / count) + "% right!!"  );
// Ends program
 System.exit(0);    
 }
 
 
  public static void addition()
    {        
        boolean isNum = true;
        int additionData = 0;
        int genNumOne, genNumTwo;
        String selectAddition;  
   
       // generates 2 random numbers between 0 and 9
        genNumOne = (int) (Math.random() * 9) + 0;
        genNumTwo = (int) (Math.random() * 9) + 0;
     
        selectAddition = JOptionPane.showInputDialog("What is" + " " + genNumOne +
                                            " " + "plus" + " " + genNumTwo + "?");    
        try
        {        
            additionData = Integer.parseInt(selectAddition);
       
        }
        catch (NumberFormatException e)
        {
            isNum = false; //keeps track of wrong entry
        }
   
       if ( additionData == (genNumOne + genNumTwo) )
       {
           // correct answer prompt
           JOptionPane.showMessageDialog(null,
                    "Very good!",
                    "Well done",
                    JOptionPane.INFORMATION_MESSAGE);                
         result  = result + 100;
        }
      else
       if (isNum == true)
       {
           // incorrect answer prompt
            JOptionPane.showMessageDialog(null,
                    "Sorry that was incorrect. Better luck next time",
                    "Bad luck",
                    JOptionPane.INFORMATION_MESSAGE);
       }
        else // if we have the wrong entry
        {
                 JOptionPane.showMessageDialog(null,
                            "Please enter a number between 1 and 4",
                            "Wrong entry",
                            JOptionPane.ERROR_MESSAGE);
                                isNum = true;      
        }
 
   }

 
    public static void subtraction()
    {      
        boolean isNum = true;
        int subtractionData = 0;
        int genNumOne, genNumTwo;
        String selectSubtraction;  
   
       
        genNumOne = (int) (Math.random() * 9) + 0;
        genNumTwo = (int) (Math.random() * genNumOne) + 0;
        // makes sure first random number is greater than second
   
        selectSubtraction = JOptionPane.showInputDialog("What is" + " " + genNumOne +
                            " " + "minus" + " " + genNumTwo + "?");    
        try
        {        
            subtractionData = Integer.parseInt(selectSubtraction);
       
        }
        catch (NumberFormatException e)
        {
            isNum = false; // keeps track of wrong entry
        }
   
       if ( subtractionData == (genNumOne - genNumTwo) )
       {
           JOptionPane.showMessageDialog(null,
                    "Very good!",
                    "Well done",
                    JOptionPane.INFORMATION_MESSAGE);                
         result  = result + 100;
        }
      else
       if (isNum == true)
       {
            JOptionPane.showMessageDialog(null,
                    "Sorry that was incorrect. Better luck next time",
                    "Bad luck",
                    JOptionPane.INFORMATION_MESSAGE);
       }
        else // if we have the wrong entry
        {
                 JOptionPane.showMessageDialog(null,
                            "Please enter a number between 1 and 4",
                            "Wrong entry",
                            JOptionPane.ERROR_MESSAGE);
                                isNum = true;        
        }
 
   }
       
 
    public static void multiplication()
    {
        boolean isNum = true;
        int multiplicationData = 0;
        int genNumOne, genNumTwo;
        String selectMultiplication;  
         
        genNumOne = (int) (Math.random() * 9) + 0;
        genNumTwo = (int) (Math.random() * 9) + 0;
   
        selectMultiplication = JOptionPane.showInputDialog("What is" + " " + genNumOne +
                                                 " " + "times" + " " + genNumTwo + "?");    
        try
        {        
            multiplicationData = Integer.parseInt(selectMultiplication);
       
        }
        catch (NumberFormatException e)
        {
            isNum = false; // keeps track of wrong entry
        }
   
       if ( multiplicationData == (genNumOne * genNumTwo) )
       {
           JOptionPane.showMessageDialog(null,
                    "Very good!",
                    "Well done",
                    JOptionPane.INFORMATION_MESSAGE);                
         result  = result + 100;
        }
      else
       if (isNum == true)
       {
            JOptionPane.showMessageDialog(null,
                    "Sorry that was incorrect. Better luck next time",
                    "Bad luck",
                    JOptionPane.INFORMATION_MESSAGE);
       }
        else // if we have the wrong entry
        {
                 JOptionPane.showMessageDialog(null,
                            "Please enter a number between 1 and 4",
                            "Wrong entry",
                            JOptionPane.ERROR_MESSAGE);
                                isNum = true;        
        }
 
   }
   
   
   
    public static void correctAnswer()
{





    }




    public static void incorrectAnswer()
{





     }
 }
0
DeafBox
Asked:
DeafBox
  • 2
1 Solution
 
gatorvipCommented:
add two string arrays to hold the different answers, i.e.

String[] arrGood = { "Very good!",  "Well done", "Great!" };
String[] arrBad = {  "Sorry that was incorrect.", "Better luck next time",  "Bad luck" };

and two random number generators
Random randGood = new Random();
Random randBad = new Random();

then in correctAnswer() you will pick out a random number between 0 and arrGood.length - 1 and print (in whatever form you need) the message at that array position
    int new_rand_index = randGood.nextInt( arrGood.length );
   String message = arrGood[ new_rand_index ];
to print the message, for example
    JOptionPane.showMessageDialog(null,
                   message ,
                    "Good Answer",
                    JOptionPane.INFORMATION_MESSAGE);                

And then when you need to call this function, you just do something like

if (result == correct)  
    correctAnswer() ;
else
    incorrectAnswer() ;
0
 
DeafBoxAuthor Commented:
Ok from your example I got it working. I think it's right?


I don't know why at my end results I'm getting a random arrBad message :S

Any ideas on how to do the rest of the code for the mutiplayers etc?

/**
 * Kids additon, subtraction, multiplication calculator
 * that generates 2 random numbers between 0 and 9.
 * 21 September 2007
 */

import javax.swing.JOptionPane; // Needed for JOptionPane
import java.util.Random;
 
public class KidsCalc
  {
  //Initialise result & count to 0
  public static int result = 0;
  public static int count = 0;
  public static int correct = 0;
 
 
  public static void main(String[] args)
    {
     
       int userChoice = 0;
       String userChoiceStr;
        do
          {
              // user input prompt
               userChoiceStr =  JOptionPane.showInputDialog(null,
                       
                        "1. Practice addition\n"
                        + "2. Practice subtraction\n"
                        + "3. Practice multiplication\n"
                        + "4. Quit the program\n"
                        + "\n"
                        + "Enter your choice",
                       
                        "Enter your choice",
                        JOptionPane.QUESTION_MESSAGE);
                        // keeps track of wrong entry
                        try
                        {
                         userChoice = Integer.parseInt(userChoiceStr);
                      }
                         catch (NumberFormatException e)
                       {
                        }
       // switch statement to count user score                    
       switch (userChoice)
       {
       case 1:    
          ++count;
          addition ();
          break;
       case 2:
           ++count;
           subtraction ();
           break;
       case 3:
           ++count;
           multiplication ();
            break;
       case 4:        
            break;
        default:
         // prompt if user doesn't enter a number between 1-4  
        JOptionPane.showMessageDialog(null,
                            "Please enter a number between 1 and 4",
                            "Wrong entry",
                            JOptionPane.ERROR_MESSAGE);      
          }
 }
       // returns your percentage if input not equal to 4
        while ( userChoice != 4 );

    JOptionPane.showMessageDialog ( null ,"You got " +
                                  (result / count) + "% right!!"  );
                                 
                                 
                                  if (result == correct)  
    correctAnswer() ;
else
    incorrectAnswer() ;
// Ends program
 System.exit(0);    
 }
 
 
  public static void addition()
    {        
        boolean isNum = true;
        int additionData = 0;
        int genNumOne, genNumTwo;
        String selectAddition;  
   
       // generates 2 random numbers between 0 and 9
        genNumOne = (int) (Math.random() * 9) + 0;
        genNumTwo = (int) (Math.random() * 9) + 0;
     
        selectAddition = JOptionPane.showInputDialog("What is" + " " + genNumOne +
                                            " " + "plus" + " " + genNumTwo + "?");    
        try
        {        
            additionData = Integer.parseInt(selectAddition);
       
        }
        catch (NumberFormatException e)
        {
            isNum = false; //keeps track of wrong entry
        }
   
       if ( additionData == (genNumOne + genNumTwo) )
        {
           
           result = result + 100;
           correctAnswer() ;  
        }
        else
            if (isNum == true)
            {
        incorrectAnswer() ;
       
    }
        else // if we have the wrong entry
        {
              JOptionPane.showMessageDialog(null,
              "Please enter a number between 1 and 4",
                                        "Wrong entry",
                JOptionPane.ERROR_MESSAGE);
               
            isNum = true;                                    
     }
}

 
    public static void subtraction()
    {      
        boolean isNum = true;
        int subtractionData = 0;
        int genNumOne, genNumTwo;
        String selectSubtraction;  
   
       
        genNumOne = (int) (Math.random() * 9) + 0;
        genNumTwo = (int) (Math.random() * genNumOne) + 0;
        // makes sure first random number is greater than second
   
        selectSubtraction = JOptionPane.showInputDialog("What is" + " " + genNumOne +
                            " " + "minus" + " " + genNumTwo + "?");    
        try
        {        
            subtractionData = Integer.parseInt(selectSubtraction);
       
        }
        catch (NumberFormatException e)
        {
            isNum = false; // keeps track of wrong entry
        }
   
       if ( subtractionData == (genNumOne - genNumTwo) )
       {
           
           result = result + 100;
           correctAnswer() ;  
        }
        else
            if (isNum == true)
            {
        incorrectAnswer() ;
       
    }
        else // if we have the wrong entry
        {
              JOptionPane.showMessageDialog(null,
              "Please enter a number between 1 and 4",
                                        "Wrong entry",
                JOptionPane.ERROR_MESSAGE);
               
            isNum = true;                                    
     }
}
       
 
    public static void multiplication()
    {
        boolean isNum = true;
        int multiplicationData = 0;
        int genNumOne, genNumTwo;
        String selectMultiplication;  
         
        genNumOne = (int) (Math.random() * 9) + 0;
        genNumTwo = (int) (Math.random() * 9) + 0;
   
        selectMultiplication = JOptionPane.showInputDialog("What is" + " " + genNumOne +
                                                 " " + "times" + " " + genNumTwo + "?");    
        try
        {        
            multiplicationData = Integer.parseInt(selectMultiplication);
       
        }
        catch (NumberFormatException e)
        {
            isNum = false; // keeps track of wrong entry
        }
   
       if ( multiplicationData == (genNumOne * genNumTwo) )
       {
           
           result = result + 100;
           correctAnswer() ;  
        }
        else
            if (isNum == true)
            {
        incorrectAnswer() ;
       
    }
        else // if we have the wrong entry
        {
              JOptionPane.showMessageDialog(null,
              "Please enter a number between 1 and 4",
                                        "Wrong entry",
                JOptionPane.ERROR_MESSAGE);
               
            isNum = true;                                    
     }
}
   

   
    public static void correctAnswer()
{

    String[] arrGood = { "Very good!",  "Well done", "Great!" };
    Random randGood = new Random();
   
    int new_rand_index = randGood.nextInt( arrGood.length );
   String message = arrGood[ new_rand_index ];
   JOptionPane.showMessageDialog(null, message,
                    "Good Answer",
                    JOptionPane.INFORMATION_MESSAGE);
                   
                   

    }



    public static void incorrectAnswer()
{

    String[] arrBad = {  "Sorry that was incorrect.", "Better luck next time",  "Bad luck" };
    Random randBad = new Random();

   
    int new_rand_index = randBad.nextInt( arrBad.length );
   String message = arrBad[ new_rand_index ];
   JOptionPane.showMessageDialog(null, message,
                    "Bad Answer",
                    JOptionPane.INFORMATION_MESSAGE);

     }
 }
0
 
gatorvipCommented:
to do the multiplayer, do exactly what the instructions say

-- At the beginning of the main method prompt for number of players
once you have that, do the following step:

-- Now create an array of this size to hold doubles. This will hold each players percentage correct.
(an array of length equal to the number of players). Actually in your case, create 3 arrays (result, count, correct)

--Now put a for loop around the rest of the main method to loop as many times as there are players.
for (player # 1 ... n)
 repeat what you have in your main method now for one player

-- Adjust the menu to indicate the player number
use the arrays that you created before,

--Just before the end of the for loop  store that players correct percentage in the array.
to save the information for that player

--After the for loop  at the end of the main method put another for loop that reads the array and displays the percentages for each player.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now