# how to convert from switches to methods

• 03-26-2009, 08:28 PM
peacehope
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:

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:

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
Code:

`public static int readProblemType(){}`
method to read the problem type? Or where should I start?
• 03-26-2009, 09:25 PM
peacehope
can anyone help?
• 03-26-2009, 10:39 PM
angryboy
just move some of the code to methods:
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...
• 03-27-2009, 12:00 AM
Fubarable
cross-posted in the sun new to java forum
• 03-27-2009, 01:33 AM
angryboy
i hate crossposters