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?