# Thread: how to convert from switches to methods

1. Member Join Date
Mar 2009
Posts
2
Rep Power
0

## 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){
/* 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);
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);
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);
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);
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){}
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?  Reply With Quote

2. Member Join Date
Mar 2009
Posts
2
Rep Power
0

## can anyone help?  Reply With Quote

3. ## just move some of the code to methods:
Java Code:
```           switch(problemType){
/* get random values */
/* solve problem */
/* 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 09:42 PM.  Reply With Quote

4. ## cross-posted in the sun new to java forum  Reply With Quote

5. ## i hate crossposters  Reply With Quote

#### Posting Permissions

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