Results 1 to 5 of 5
 03262009, 08:28 PM #1Member
 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){ 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 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(){}
Java Code:public static int readProblemType(){}
 03262009, 09:25 PM #2Member
 Join Date
 Mar 2009
 Posts
 2
 Rep Power
 0
can anyone help?
 03262009, 10:39 PM #3
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; }
Last edited by angryboy; 03262009 at 10:42 PM.
USE CODE TAGS> [CODE]...[/CODE]
Get NotePad++ (free)

crossposted in the sun new to java forum
 03272009, 01:33 AM #5
Similar Threads

Trouble with static methods and boolean equals() methods with classes
By dreamingofgreen in forum New To JavaReplies: 8Last Post: 04172012, 12:00 AM 
Convert to Int
By 6kyAngel in forum New To JavaReplies: 0Last Post: 03062009, 10:18 AM 
how to convert xml to xsd
By adi in forum XMLReplies: 2Last Post: 02122009, 03:43 PM 
need the help to convert to and from UTF8 and UCS2
By sachin_n in forum Advanced JavaReplies: 1Last Post: 01052009, 10:35 PM 
Help with switches
By Daniel in forum New To JavaReplies: 2Last Post: 07042007, 09:37 AM
Bookmarks