Results 1 to 5 of 5
  1. #1
    peacehope is offline Member
    Join Date
    Mar 2009
    Posts
    2
    Rep Power
    0

    Default how to convert from switches to methods

    So I have this homework assignment where I need to get my application from switches to using methods to give me the same result. I usually am good in this class, but right now I am completely lost. I won't throw everything at you, but just something to hopefully get me started.

    Here is a piece of the code using switches:

    Java Code:
    		/* Get Type of Math problem from the user */
    input = JOptionPane.showInputDialog(null,"Enter the type of Math Problem " 
    		+ "you would like to solve: \n\n"
    				+ "Addition = 1\n"
    				+ "Subtraction = 2\n"
    				+ "Multiplication = 3\n"
    				+ "Division = 4\n"
    				+ "Enter the number of your choice: ");
    
            problemType = Integer.parseInt(input);
    
           	switch(problemType){
    			case ADDITION:
    				/* get random values */
    				mathVar1 = (int)(Math.random() * 100);
    				mathVar2 = (int)(Math.random() * 100);
    				/* solve problem */
    				mathAnswer = mathVar1 + mathVar2;
    				/* Get answer of Math problem from the user */
    				input = JOptionPane.showInputDialog(null,"Solve: \n" + mathVar1 +  " + " 
    + mathVar2);
    				userAnswer = Integer.parseInt(input);
    				break;
    			case SUBTRACTION:
    				/* get random values */
    				mathVar1 = (int)(Math.random() * 100);
    				mathVar2 = (int)(Math.random() * 100);
    				/* solve problem */
    				mathAnswer = mathVar1 - mathVar2;
    				/* Get answer of Math problem from the user */
    				input = JOptionPane.showInputDialog(null,"Solve: \n" + mathVar1 +  " - " 
    + mathVar2);
    				userAnswer = Integer.parseInt(input);
    				break;
    			case MULTIPLICATION:
    				/* get random values */
    				mathVar1 = (int)(Math.random() * 100);
    				mathVar2 = (int)(Math.random() * 100);
    				/* solve problem */
    				mathAnswer = mathVar1 * mathVar2;
    				/* Get answer of Math problem from the user */
    				input = JOptionPane.showInputDialog(null,"Solve: \n" + mathVar1 +  " * " 
    + mathVar2);
    				userAnswer = Integer.parseInt(input);
    				break;
    			case DIVISION:
    				/* get random values */
    				mathVar1 = (int)(Math.random() * 100);
    				mathVar2 = (int)(Math.random() * 100);
    				/* solve problem */
    				mathAnswer = mathVar1 / mathVar2;
    				/* Get answer of Math problem from the user */
    				input = JOptionPane.showInputDialog(null,"Solve: \n" + mathVar1 +  " / " 
    + mathVar2);
    				userAnswer = Math.round(Float.parseFloat(input));
    				break;
    
    		}
    I need to get this same result by using methods.

    I can use these method signatures:

    Java Code:
    public static int readProblemType(){}
    public static int getAddProblem(int randomValue1 int randomValue2){}
    public static int getSubProblem(int randomValue1 int randomValue2){}
    public static int getMultProblem(int randomValue1 int randomValue2){}
    public static int getDivProblem(int randomValue1 int randomValue2){}
    public static int readAnswer(){}
    Looking at what I need to do, how would I use
    Java Code:
    public static int readProblemType(){}
    method to read the problem type? Or where should I start?

  2. #2
    peacehope is offline Member
    Join Date
    Mar 2009
    Posts
    2
    Rep Power
    0

    Default

    can anyone help?

  3. #3
    angryboy's Avatar
    angryboy is offline Senior Member
    Join Date
    Jan 2009
    Posts
    742
    Rep Power
    6

    Default

    just move some of the code to methods:
    Java Code:
               switch(problemType){
                case ADDITION:
                    /* get random values */
                    mathVar1 = readProblemType();
                    mathVar2 = readProblemType();
                    /* solve problem */
                    mathAnswer = getAddProblem(mathVar1 ,mathVar2);
                    /* Get answer of Math problem from the user */
    ...
     
           break;
     
    ...
    public static int readProblemType()
    {
      return (int)(Math.random() * 100);
    }
     
    public static int getAddProblem(int randomValue1 int randomValue2)
    {
      return randomValue1 + randomValue2;
    }
    and so on...
    Last edited by angryboy; 03-26-2009 at 10:42 PM.
    USE CODE TAGS--> [CODE]...[/CODE]
    Get NotePad++ (free)

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    cross-posted in the sun new to java forum

  5. #5
    angryboy's Avatar
    angryboy is offline Senior Member
    Join Date
    Jan 2009
    Posts
    742
    Rep Power
    6

Similar Threads

  1. Replies: 8
    Last Post: 04-17-2012, 12:00 AM
  2. Convert to Int
    By 6kyAngel in forum New To Java
    Replies: 0
    Last Post: 03-06-2009, 10:18 AM
  3. how to convert xml to xsd
    By adi in forum XML
    Replies: 2
    Last Post: 02-12-2009, 03:43 PM
  4. need the help to convert to and from UTF-8 and UCS-2
    By sachin_n in forum Advanced Java
    Replies: 1
    Last Post: 01-05-2009, 10:35 PM
  5. Help with switches
    By Daniel in forum New To Java
    Replies: 2
    Last Post: 07-04-2007, 09:37 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •