5 Replies Latest reply: Sep 20, 2007 3:57 AM by 807600 RSS

    Static Methods & Switch Statement

    807600
      Need help with a switch statement and static methods. My program offers the user a menu to choose what they want to practice.

      I want to use a switch statement to process the selection. Selecting 1, 2 or 3 causes the program to go to one of the following static methods:

      addition()
      subtraction
      multiplication()


      import javax.swing.JOptionPane;  // Needed for JOptionPane
      import java.text.DecimalFormat;  // Needed for DecimalFormat
      
      public class MathsFinal {
          
          static boolean exit = false;
          static int totalAnswersAsked = 0;
         
          public static void main(String[] args) 
          {
             
              int userChoice;  
              int correctAnswers = 0;
             
             
              do{
                  //custom button text
                  do{
                      boolean exitAsking = true;
                      String userChoiceString = 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);
                     
                      int userAnswer, genNumOne, genNumTwo;
                     
                      if(userChoiceString == null || userChoiceString.equals("4") )
                          { //closed it
                          exit = true;
                         
                      }else if(userChoiceString.equals("1"))
                          { //addition
                         
                          genNumOne = getRandomNumber(9,0);
                          genNumTwo = getRandomNumber(9,0);
                         
                          userAnswer = genNumOne + genNumTwo;
                          correctAnswers += display(genNumOne, genNumTwo, userAnswer, "plus");
                         
                      }else if(userChoiceString.equals("2"))
                          { //subtraction
                         
                          genNumOne = getRandomNumber(9,0);
                          genNumTwo = getRandomNumber(genNumOne,0);
                         
                          userAnswer = genNumOne - genNumTwo;
                          correctAnswers += display(genNumOne, genNumTwo, userAnswer, "minus");
                         
                      }else if(userChoiceString.equals("3"))
                          { //multiplication
                         
                          genNumOne = getRandomNumber(9,0);
                          genNumTwo = getRandomNumber(9,0);
                         
                          userAnswer = genNumOne * genNumTwo;
                          correctAnswers += display(genNumOne, genNumTwo, userAnswer, "times");
                         
                      }else
                          { //user didn't enter a number
                          JOptionPane.showMessageDialog(null,
                                  "Please enter a number between 1 and 4",
                                  "Wrong entry",
                                  JOptionPane.ERROR_MESSAGE);
                          exitAsking = false;
                      }
                  }
                  while(!exit);
                 
              }
              while(!exit);
             
              //create a DecimalFormat object for percentages.
              DecimalFormat userPercent = new DecimalFormat("#0%");
             
              //show results using information icon
              JOptionPane.showMessageDialog(null,
                     
                      "You got " + (userPercent.format(((double)correctAnswers / (double)totalAnswersAsked)))  + " right!!",
                      "Thank you for playing",
                      JOptionPane.INFORMATION_MESSAGE);
             
              //ends the program
              System.exit(0);
          }
         
          public static int getRandomNumber(int large, int small) 
              {
              return  (int)((large - small) * Math.random()) + small;
          }
         
          public static int display(int genNumOne, int genNumTwo, int userAnswer, String operation) 
              {           
              String selectAddition = JOptionPane.showInputDialog("What is" + " " + genNumOne + " " + operation + " " + genNumTwo + "?");
             
              if(selectAddition == null) { //pressed close
                  exit = true;
                  return 0;
              }
               else if(userAnswer == Integer.parseInt(selectAddition))
                { //answer correct
                 
                  totalAnswersAsked++;
                 
                  //show results using information icon
                  JOptionPane.showMessageDialog(null,
                          "Very good!",
                          "Well done",
                          JOptionPane.INFORMATION_MESSAGE);
                  return 1;
              } 
               else if(userAnswer != Integer.parseInt(selectAddition)) 
                { // incorrect answer
                    
                  totalAnswersAsked++;
                  JOptionPane.showMessageDialog(null,
                          "Sorry that was incorrect. Better luck next time",
                          "Bad luck",
                          JOptionPane.INFORMATION_MESSAGE);
                  return 0;
              }
              return 0;
          }
      }
        • 1. Re: Static Methods & Switch Statement
          3004
          A couple of points...


          1) +"Static Methods & Switch Statement", "help with a switch statement and static methods", etc.+
          There's no association in the Java language between switch and static. A switch statement has a certain behavior. That behavior is the same regardless of whether the switch occurs in a static method or not. Likewise, static methods have certain rules and behavior, and that's not altered by the presence or absence of a switch statement.

          2) What is your question?
          • 2. Re: Static Methods & Switch Statement
            807600
            That's not what I meant. I want to use static methods for sections of my code. The sections are addition()
            subtraction() multiplication(). Selecting 1, 2 or 3 causes the program to go to one of the following static methods. I then want to use a switch statement to process the selection.

            Does that make any sense?
            • 3. Re: Static Methods & Switch Statement
              807600
              hi,
              switch statement to process the selection. Selecting 1, 2 or 3
              import javax.swing.JOptionPane;  // Needed for JOptionPane
              import java.text.DecimalFormat;  // Needed for DecimalFormat
               
              public class MathsFinal {
                  
                  static boolean exit = false;
                  static int totalAnswersAsked = 0;
                 
                  public static void main(String[] args) 
                  {
                     
                      int userChoice;  
                      int correctAnswers = 0;
                     int userChoiceString=4;
                     
                      do{
                          //custom button text
                              boolean exitAsking = true;
                              userChoiceString = Integer.parseInt(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));
                             
                              int userAnswer, genNumOne, genNumTwo;
                             
                             switch (userChoiceString)
                             {
                             case 1:
                                     
                                  genNumOne = getRandomNumber(9,0);
                                  genNumTwo = getRandomNumber(9,0);
                                 
                                  userAnswer = genNumOne + genNumTwo;
                                  correctAnswers += display(genNumOne, genNumTwo, userAnswer, "plus");
                                       break;
              
                                  case 2:            
                                  genNumOne = getRandomNumber(9,0);
                                  genNumTwo = getRandomNumber(genNumOne,0);
                                 
                                  userAnswer = genNumOne - genNumTwo;
                                  correctAnswers += display(genNumOne, genNumTwo, userAnswer, "minus");
                                        break;
                                  case 3:
                                 
                                  genNumOne = getRandomNumber(9,0);
                                  genNumTwo = getRandomNumber(9,0);
                                 
                                  userAnswer = genNumOne * genNumTwo;
                                  correctAnswers += display(genNumOne, genNumTwo, userAnswer, "times");
                                       break;
                                  
                                  case 4: break;
                               default : 
                                  JOptionPane.showMessageDialog(null,
                                          "Please enter a number between 1 and 4",
                                          "Wrong entry",
                                          JOptionPane.ERROR_MESSAGE);
                                  exitAsking = false;
                              }
                          } while(userChoiceString != 4);
                         
                      //create a DecimalFormat object for percentages.
                      DecimalFormat userPercent = new DecimalFormat("#0%");
                     
                      //show results using information icon
                      JOptionPane.showMessageDialog(null,
                             
                              "You got " + (userPercent.format(((double)correctAnswers / (double)totalAnswersAsked)))  + " right!!",
                              "Thank you for playing",
                              JOptionPane.INFORMATION_MESSAGE);
                     
                      //ends the program
                      System.exit(0);
                  }
                 
                  public static int getRandomNumber(int large, int small) 
                      {
                      return  (int)((large - small) * Math.random()) + small;
                  }
                 
                  public static int display(int genNumOne, int genNumTwo, int userAnswer, String operation) 
                      {           
                      String selectAddition = JOptionPane.showInputDialog("What is" + " " + genNumOne + " " + operation + " " + genNumTwo + "?");
                     
                      if(selectAddition == null) { //pressed close
                          exit = true;
                          return 0;
                      }
                       else if(userAnswer == Integer.parseInt(selectAddition))
                        { //answer correct
                         
                          totalAnswersAsked++;
                         
                          //show results using information icon
                          JOptionPane.showMessageDialog(null,
                                  "Very good!",
                                  "Well done",
                                  JOptionPane.INFORMATION_MESSAGE);
                          return 1;
                      } 
                       else if(userAnswer != Integer.parseInt(selectAddition)) 
                        { // incorrect answer
                            
                          totalAnswersAsked++;
                          JOptionPane.showMessageDialog(null,
                                  "Sorry that was incorrect. Better luck next time",
                                  "Bad luck",
                                  JOptionPane.INFORMATION_MESSAGE);
                          return 0;
                      }
                      return 0;
                  }
              }
               
              • 4. Re: Static Methods & Switch Statement
                807600
                I guess it is similar to what you want
                int choice =  userChoiceString == null? 4 : Integer.parseInt(userChoiceString);
                                switch(choice) {
                                case 1:
                                     genNumOne = getRandomNumber(9,0);
                                    genNumTwo = getRandomNumber(9,0);
                                   
                                    userAnswer = genNumOne + genNumTwo;
                                    correctAnswers += display(genNumOne, genNumTwo, userAnswer, "plus");
                                     break;
                                case 2:
                                     genNumOne = getRandomNumber(9,0);
                                    genNumTwo = getRandomNumber(genNumOne,0);
                                   
                                    userAnswer = genNumOne - genNumTwo;
                                    correctAnswers += display(genNumOne, genNumTwo, userAnswer, "minus");
                                     break;
                                case 3:
                                     genNumOne = getRandomNumber(9,0);
                                    genNumTwo = getRandomNumber(9,0);
                                   
                                    userAnswer = genNumOne * genNumTwo;
                                    correctAnswers += display(genNumOne, genNumTwo, userAnswer, "times");
                                     break;
                                default:
                                     exit = true;
                Edited by: AlekB on Sep 20, 2007 1:56 AM
                • 5. Re: Static Methods & Switch Statement
                  807600
                  DeafBox wrote:
                  That's not what I meant. I want to use static methods for sections of my code. The sections are addition()
                  subtraction() multiplication(). Selecting 1, 2 or 3 causes the program to go to one of the following static methods. I then want to use a switch statement to process the selection.

                  Does that make any sense?
                  use with static
                  import javax.swing.JOptionPane;
                  
                  public class MathFinal
                    {
                  public static float result = 0.0f;
                  public static int count = 0;
                  
                  
                    public static void main(String[] args)
                      {
                       
                          int userChoice;
                     
                        
                            do
                              {
                           userChoice = Integer.parseInt (JOptionPane.showInputDialog("Enter your choice [1-4] \n 1. Add \n 2. Sub \n 3. Mul \n 4. Exit"));
                  
                  
                          switch (userChoice)
                          {
                          case 1:
                          
                             ++count;
                              addition (); 
                              break;
                          case 2:
                               ++count;
                               subtraction ();
                               break;
                          case 3:
                               ++count;
                               multiplication ();
                                 break;
                  
                            case 4:
                                 break;
                            default:
                                  JOptionPane.showMessageDialog ( null ,"Choice [1-4] only");
                                 break;
                          }
                  
                         }while ( userChoice != 4 );
                       
                   MathFinal.result();
                   System.exit(0);     
                   }
                  
                  
                  
                     public static void result ()
                         {
                          JOptionPane.showMessageDialog ( null ,"You Got " + (result / count) + "% Right"  );
                         }
                  
                   
                    
                    public static void addition()
                      {
                          
                          int additionData, genNumOne, genNumTwo;
                          String selectAddition;  
                       
                         
                          genNumOne = (int) (Math.random() * 9) + 0;
                          genNumTwo = (int) (Math.random() * 9) + 0;
                         
                          selectAddition = JOptionPane.showInputDialog("What is" + " " + genNumOne + " " + "plus" + " " + genNumTwo);
                          additionData = Integer.parseInt(selectAddition);
                          if ( additionData == (genNumOne + genNumTwo) )
                          {
                                JOptionPane.showMessageDialog ( null ,"Very Good" );
                            result  = result + 100.0f;
                          }
                          else
                            {
                                JOptionPane.showMessageDialog ( null ,"Wrong Answer" );
                            }
                  
                  }
                          
                  
                    
                     public static void subtraction()
                      {
                          
                          int subtractionData, genNumOne, genNumTwo;
                          String selectSubtraction;  
                       
                         
                          genNumOne = (int) (Math.random() * 9) + 0;
                          genNumTwo = (int) (Math.random() * 9) + 0;
                         
                          selectSubtraction = JOptionPane.showInputDialog ("What is" + " " + genNumOne + " " + "minus" + " " + genNumTwo);
                          subtractionData = Integer.parseInt(selectSubtraction);
                         
                          if ( subtractionData == (genNumOne - genNumTwo) )
                          {
                                JOptionPane.showMessageDialog ( null ,"Very Good" );
                                 result  = result + 100.0f;
                          }
                          else
                            {
                                JOptionPane.showMessageDialog ( null ,"Wrong Answer" );
                            }
                  
                  } 
                         
                  
                     public static void multiplication()
                      {
                          
                          int multiplicationData, genNumOne, genNumTwo;
                          String selectMultiplication;  
                       
                         
                          genNumOne = (int) ( Math.random() * 9) + 0;
                          genNumTwo = (int) (Math.random() * 9) + 0;
                         
                          selectMultiplication = JOptionPane.showInputDialog("What is" + " " + genNumOne + " " + "times" + " " + genNumTwo);
                          multiplicationData = Integer.parseInt(selectMultiplication);
                            if ( multiplicationData == (genNumOne * genNumTwo) )
                          {
                                JOptionPane.showMessageDialog ( null ,"Very Good" );
                            result  = result + 100.0f;
                          }
                          else
                            {
                                JOptionPane.showMessageDialog ( null ,"Wrong Answer" );
                            }
                         
                         
                      }
                  }