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 2 of 2

Thread: Case statements to Method calls???

  1. #1
    Member Java Neil's Avatar
    Join Date
    Jan 2011
    Posts
    72
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Case statements to Method calls???

    Hi all...I'm in a pickle with an assignment and I hope someone can help me sort this out. I have a program called MathDrill that I have used case statements for the operators. Now my teacher wants me to switch these case statements to method calls. Can someone explain what he means (he speaks broken English) and maybe help me a bit with my code, at least to get me going in the right direction?

    Here's what I have...

       import javax.swing.JOptionPane;
       public class MathDrillMethod{
          public static void main(String[] args){
             final int NUMBER_OF_QUESTIONS = 2;
             int correctCount = 0;        
             long startTime = System.currentTimeMillis();
             String output ="";
             int option = 0;
     
             do{
     
                JOptionPane.showMessageDialog(null, "Welcome to Neil's Math Drill");                      			            
     
                //prompt user to enter a math operator for test
                String operatorString = JOptionPane.showInputDialog(null, "<html><u><i><b>Please enter operation</b></i></u></html>"
                   + "\nFor addition problems enter (1)" + "\nFor subtraction problems enter (2)" +
                   "\nFor multiplication problems enter (3)" + "\nFor division problems enter (4)" +
                   "\nFor modulo problems enter (5)");
     
    				int operator = 0;
                operator = Integer.parseInt(operatorString);
     
                if ((operator < 1)||(operator > 5)){
                   JOptionPane.showMessageDialog(null, "Error: Invalid Operator");
                   System.exit(0);
                }
     
     
     
             	//initialize count
                int count = 0;
     
                while(count < NUMBER_OF_QUESTIONS){
                   int timesleft = (NUMBER_OF_QUESTIONS - count);
                   JOptionPane.showMessageDialog(null, "Question number  "
                      + (NUMBER_OF_QUESTIONS - count));
     
                	//generate two integers that work with everything
                   int num1 = (int)(Math.random() * 10);
                   int num2 = (int)(Math.random() * 10);
     
                   if (num1 == 0)
                      num1++;
     
                   if (num2 == 0)
                      num2++; 
     
                   if (num1 < num2){
                      int temp = num1;
                      num1 = num2;
                      num2 = temp;
     
                      while ((num2 == 0) && ((operator != 4)||(operator != 5))){
     
                      }
                   }	
     
                   while ((operator < 1) || (operator > 5)){
     
                      char symbol;
                      switch (operator){
     
                         case 1: symbol = '+';
                            break;
                         case 2: symbol = '-';
                            break;
                         case 3: symbol = '*';
                            break;
                         case 4: symbol = '/';
                            break;
                         case 5: symbol = '%';
                            break;
                      }
     
                   }
     
                   if (operator == 1){
                      String answerString = JOptionPane.showInputDialog(null, " What is " + num1 +
                         " + " + num2 + "?");
                      int answer = Integer.parseInt(answerString);
     
                   //grade results
                      if (num1 + num2 == answer){
                         JOptionPane.showMessageDialog(null, "You are Correct");
                         correctCount++;
                      }
                      else
                         JOptionPane.showMessageDialog(null, "You are Incorrect\nYour answer should be "
                            + num1 + " + " + num2 + " = " + (num1 + num2));
     
                      output += "\n" + num1 + " + " + num2 + " = " + answer +
                         ((num1 + num2 == answer) ? " correct" : " wrong");
                   }      
                   if (operator == 2){
                      String answerString = JOptionPane.showInputDialog(null, " What is " + num1 +
                         " - " + num2 + " ? ");
                      int answer = Integer.parseInt(answerString);
     
                   //grade results
                      if (num1 - num2 == answer){
                         JOptionPane.showMessageDialog(null, "You are Correct");
                         correctCount++;
                      }
                      else
                         JOptionPane.showMessageDialog(null, "You are Incorrect\nYour answer should be "
                            + num1 + " - " + num2 + " = " + (num1 - num2));
     
                      output += "\n" + num1 + " - " + num2 + " = " + answer +
                         ((num1 - num2 == answer) ? " correct" : " wrong");
                   }      
                   if (operator == 3){
                      String answerString = JOptionPane.showInputDialog(null, " What is " + num1 +
                         " * " + num2 + " ? ");
                      int answer = Integer.parseInt(answerString);
     
                   //grade results
                      if (num1 * num2 == answer){
                         JOptionPane.showMessageDialog(null, " You are Correct");
                         correctCount++;
                      }
                      else
                         JOptionPane.showMessageDialog(null, "You are Incorrect\nYour answer should be "
                            + num1 + " * " + num2 + " = " + (num1 * num2));
     
                      output += "\n" + num1 + " * " + num2 + " = " + answer +
                         ((num1 * num2 == answer) ? " correct" : " wrong");
                   }      
                   if (operator == 4){
                      String answerString = JOptionPane.showInputDialog(null, " What is " + num1 +
                         " / " + num2 + " ? ");
                      int answer = Integer.parseInt(answerString);
     
                   //grade results
                      if (num1 / num2 == answer){
                         JOptionPane.showMessageDialog(null, " You are Correct");
                         correctCount++;
     
                      }
                      else
                         JOptionPane.showMessageDialog(null, "You are Incorrect\nYour answer should be "
                            + num1 + " / " + num2 + " = " + (num1 / num2));
     
                      output += "\n" + num1 + " / " + num2 + " = " + answer +
                         ((num1 / num2 == answer) ? " correct" : " wrong");
                   }      
                   if (operator == 5){
                      String answerString = JOptionPane.showInputDialog(null, " What is " + num1 +
                         " % " + num2 + " ? ");
                      int answer = Integer.parseInt(answerString);
     
                   //grade results
                      if (num1 % num2 == answer){
                         JOptionPane.showMessageDialog(null," You are Correct ");
                         correctCount++;
                      }
                      else
                         JOptionPane.showMessageDialog(null, "You are Incorrect\nYour answer should be "
                            + num1 + " % " + num2 + " = " + (num1 % num2));
     
                      output += "\n" + num1 + " % " + num2 + " = " + answer +
                         ((num1 % num2 == answer) ? " correct" : " wrong");
                   }
                   count++;
                }
     
                option = JOptionPane.showConfirmDialog(null, "Do you want to play again?");
     
                long endTime = System.currentTimeMillis();
                long testTime = (endTime - startTime);
     
                JOptionPane.showMessageDialog(null, " Correct answers = " + correctCount +
                   "\nYour test took " + testTime /1000 + " seconds\n " + output);
     
             }while(option == JOptionPane.YES_OPTION);
             System.exit(0);
     
          }
     
       }

    Thanks in advance for your help!


  2. #2
    Forum old-timer
    Join Date
    Nov 2008
    Location
    Faversham, Kent, UK
    Posts
    472
    My Mood
    Mellow
    Thanks
    4
    Thanked 58 Times in 54 Posts

    Default Re: Case statements to Method calls???

    Well, for a start, you don't use the 'symbol' that gets set in the 'case' statement...

    But I think what your tutor means is that you should put the code that performs each operation into a separate method, so you have a method for adding, a method for subtracting, etc., instead of a bunch of code inside 'if' blocks.

    The first thing to do is put the code for each 'if' block into a separate method, e.g.
    if (operator == 1) {
       doAdd(...);  // call add method, passing in the necessary parameters
    }
    ...
    Once you've done that, move the method calls to the 'case' statement and scrap the 'if's.

    In writing the calculation methods, you may find they have lots of common code. See if you can avoid having lots of duplicate code in each method by putting it in a separate method and calling it from each calculation method, or taking the common code out of the calculation methods and doing it later.
    Last edited by dlorde; February 24th, 2011 at 06:20 AM.

  3. The Following User Says Thank You to dlorde For This Useful Post:

    SPACE MONKEY (February 25th, 2011)

Similar Threads

  1. CompareToIngnore case problem
    By newTaz in forum What's Wrong With My Code?
    Replies: 9
    Last Post: November 7th, 2010, 05:28 PM
  2. [SOLVED] A Final method for DoublyLinkedList and a new class that calls DoublyLinkedList.
    By javapenguin in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 12th, 2010, 07:29 PM
  3. Need help understanding method calls and such
    By hackman2007 in forum Java Theory & Questions
    Replies: 8
    Last Post: July 14th, 2010, 08:18 AM
  4. [SOLVED] upper case
    By andaji in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 13th, 2010, 11:54 PM
  5. Any way to map method calls?
    By Swiftslide in forum Collections and Generics
    Replies: 1
    Last Post: September 21st, 2009, 04:37 AM