DeafBox
asked on
Initialiser list and arrays
Add 2 new methods which are called to display each result ie. Cut the display of the dialog box from the each of the methods addition(), subtraction() and multiplication() and replace then with a call to either of the following static void methods:
correctAnswer()
incorrectAnswer()
To make the program more interesting , each of these two methods should contain an array initialised with a number of responses (an initialiser list) which are selected at random. So if an answer is correct, the response may be
Very good!, Excellent!, You are hot! etc etc.
Similarly for incorrect reponses.
Now the program is multi-player. At the beginning of the main method prompt for number of players
Now create an array of this size to hold doubles. This will hold each players percentage correct.
Now put a for loop around the rest of the main method to loop as many times as there are players.
Adjust the menu to indicate the player number
Just before the end of the for loop store that players correct percentage in the array.
After the for loop at the end of the main method put another for loop that reads the array and displays the percentages for each player.
Having trouble with the above. I'm more than have way done, and would appreciate some help on getting rest incorporated into my code :).
Here's what I have so far
import javax.swing.JOptionPane; // Needed for JOptionPane
public class KidsCalc
{
//Initialise result & count to 0
public static int result = 0;
public static int count = 0;
public static void main(String[] args)
{
int userChoice = 0;
String userChoiceStr;
do
{
// user input prompt
userChoiceStr = JOptionPane.showInputDialo g(null,
"1. Practice addition\n"
+ "2. Practice subtraction\n"
+ "3. Practice multiplication\n"
+ "4. Quit the program\n"
+ "\n"
+ "Enter your choice",
"Enter your choice",
JOptionPane.QUESTION_MESSA GE);
// keeps track of wrong entry
try
{
userChoice = Integer.parseInt(userChoic eStr);
}
catch (NumberFormatException e)
{
}
// switch statement to count user score
switch (userChoice)
{
case 1:
++count;
addition ();
break;
case 2:
++count;
subtraction ();
break;
case 3:
++count;
multiplication ();
break;
case 4:
break;
default:
// prompt if user doesn't enter a number between 1-4
JOptionPane.showMessageDia log(null,
"Please enter a number between 1 and 4",
"Wrong entry",
JOptionPane.ERROR_MESSAGE) ;
}
}
// returns your percentage if input not equal to 4
while ( userChoice != 4 );
JOptionPane.showMessageDia log ( null ,"You got " +
(result / count) + "% right!!" );
// Ends program
System.exit(0);
}
public static void addition()
{
boolean isNum = true;
int additionData = 0;
int genNumOne, genNumTwo;
String selectAddition;
// generates 2 random numbers between 0 and 9
genNumOne = (int) (Math.random() * 9) + 0;
genNumTwo = (int) (Math.random() * 9) + 0;
selectAddition = JOptionPane.showInputDialo g("What is" + " " + genNumOne +
" " + "plus" + " " + genNumTwo + "?");
try
{
additionData = Integer.parseInt(selectAdd ition);
}
catch (NumberFormatException e)
{
isNum = false; //keeps track of wrong entry
}
if ( additionData == (genNumOne + genNumTwo) )
{
// correct answer prompt
JOptionPane.showMessageDia log(null,
"Very good!",
"Well done",
JOptionPane.INFORMATION_ME SSAGE);
result = result + 100;
}
else
if (isNum == true)
{
// incorrect answer prompt
JOptionPane.showMessageDia log(null,
"Sorry that was incorrect. Better luck next time",
"Bad luck",
JOptionPane.INFORMATION_ME SSAGE);
}
else // if we have the wrong entry
{
JOptionPane.showMessageDia log(null,
"Please enter a number between 1 and 4",
"Wrong entry",
JOptionPane.ERROR_MESSAGE) ;
isNum = true;
}
}
public static void subtraction()
{
boolean isNum = true;
int subtractionData = 0;
int genNumOne, genNumTwo;
String selectSubtraction;
genNumOne = (int) (Math.random() * 9) + 0;
genNumTwo = (int) (Math.random() * genNumOne) + 0;
// makes sure first random number is greater than second
selectSubtraction = JOptionPane.showInputDialo g("What is" + " " + genNumOne +
" " + "minus" + " " + genNumTwo + "?");
try
{
subtractionData = Integer.parseInt(selectSub traction);
}
catch (NumberFormatException e)
{
isNum = false; // keeps track of wrong entry
}
if ( subtractionData == (genNumOne - genNumTwo) )
{
JOptionPane.showMessageDia log(null,
"Very good!",
"Well done",
JOptionPane.INFORMATION_ME SSAGE);
result = result + 100;
}
else
if (isNum == true)
{
JOptionPane.showMessageDia log(null,
"Sorry that was incorrect. Better luck next time",
"Bad luck",
JOptionPane.INFORMATION_ME SSAGE);
}
else // if we have the wrong entry
{
JOptionPane.showMessageDia log(null,
"Please enter a number between 1 and 4",
"Wrong entry",
JOptionPane.ERROR_MESSAGE) ;
isNum = true;
}
}
public static void multiplication()
{
boolean isNum = true;
int multiplicationData = 0;
int genNumOne, genNumTwo;
String selectMultiplication;
genNumOne = (int) (Math.random() * 9) + 0;
genNumTwo = (int) (Math.random() * 9) + 0;
selectMultiplication = JOptionPane.showInputDialo g("What is" + " " + genNumOne +
" " + "times" + " " + genNumTwo + "?");
try
{
multiplicationData = Integer.parseInt(selectMul tiplicatio n);
}
catch (NumberFormatException e)
{
isNum = false; // keeps track of wrong entry
}
if ( multiplicationData == (genNumOne * genNumTwo) )
{
JOptionPane.showMessageDia log(null,
"Very good!",
"Well done",
JOptionPane.INFORMATION_ME SSAGE);
result = result + 100;
}
else
if (isNum == true)
{
JOptionPane.showMessageDia log(null,
"Sorry that was incorrect. Better luck next time",
"Bad luck",
JOptionPane.INFORMATION_ME SSAGE);
}
else // if we have the wrong entry
{
JOptionPane.showMessageDia log(null,
"Please enter a number between 1 and 4",
"Wrong entry",
JOptionPane.ERROR_MESSAGE) ;
isNum = true;
}
}
public static void correctAnswer()
{
}
public static void incorrectAnswer()
{
}
}
correctAnswer()
incorrectAnswer()
To make the program more interesting , each of these two methods should contain an array initialised with a number of responses (an initialiser list) which are selected at random. So if an answer is correct, the response may be
Very good!, Excellent!, You are hot! etc etc.
Similarly for incorrect reponses.
Now the program is multi-player. At the beginning of the main method prompt for number of players
Now create an array of this size to hold doubles. This will hold each players percentage correct.
Now put a for loop around the rest of the main method to loop as many times as there are players.
Adjust the menu to indicate the player number
Just before the end of the for loop store that players correct percentage in the array.
After the for loop at the end of the main method put another for loop that reads the array and displays the percentages for each player.
Having trouble with the above. I'm more than have way done, and would appreciate some help on getting rest incorporated into my code :).
Here's what I have so far
import javax.swing.JOptionPane; // Needed for JOptionPane
public class KidsCalc
{
//Initialise result & count to 0
public static int result = 0;
public static int count = 0;
public static void main(String[] args)
{
int userChoice = 0;
String userChoiceStr;
do
{
// user input prompt
userChoiceStr = JOptionPane.showInputDialo
"1. Practice addition\n"
+ "2. Practice subtraction\n"
+ "3. Practice multiplication\n"
+ "4. Quit the program\n"
+ "\n"
+ "Enter your choice",
"Enter your choice",
JOptionPane.QUESTION_MESSA
// keeps track of wrong entry
try
{
userChoice = Integer.parseInt(userChoic
}
catch (NumberFormatException e)
{
}
// switch statement to count user score
switch (userChoice)
{
case 1:
++count;
addition ();
break;
case 2:
++count;
subtraction ();
break;
case 3:
++count;
multiplication ();
break;
case 4:
break;
default:
// prompt if user doesn't enter a number between 1-4
JOptionPane.showMessageDia
"Please enter a number between 1 and 4",
"Wrong entry",
JOptionPane.ERROR_MESSAGE)
}
}
// returns your percentage if input not equal to 4
while ( userChoice != 4 );
JOptionPane.showMessageDia
(result / count) + "% right!!" );
// Ends program
System.exit(0);
}
public static void addition()
{
boolean isNum = true;
int additionData = 0;
int genNumOne, genNumTwo;
String selectAddition;
// generates 2 random numbers between 0 and 9
genNumOne = (int) (Math.random() * 9) + 0;
genNumTwo = (int) (Math.random() * 9) + 0;
selectAddition = JOptionPane.showInputDialo
" " + "plus" + " " + genNumTwo + "?");
try
{
additionData = Integer.parseInt(selectAdd
}
catch (NumberFormatException e)
{
isNum = false; //keeps track of wrong entry
}
if ( additionData == (genNumOne + genNumTwo) )
{
// correct answer prompt
JOptionPane.showMessageDia
"Very good!",
"Well done",
JOptionPane.INFORMATION_ME
result = result + 100;
}
else
if (isNum == true)
{
// incorrect answer prompt
JOptionPane.showMessageDia
"Sorry that was incorrect. Better luck next time",
"Bad luck",
JOptionPane.INFORMATION_ME
}
else // if we have the wrong entry
{
JOptionPane.showMessageDia
"Please enter a number between 1 and 4",
"Wrong entry",
JOptionPane.ERROR_MESSAGE)
isNum = true;
}
}
public static void subtraction()
{
boolean isNum = true;
int subtractionData = 0;
int genNumOne, genNumTwo;
String selectSubtraction;
genNumOne = (int) (Math.random() * 9) + 0;
genNumTwo = (int) (Math.random() * genNumOne) + 0;
// makes sure first random number is greater than second
selectSubtraction = JOptionPane.showInputDialo
" " + "minus" + " " + genNumTwo + "?");
try
{
subtractionData = Integer.parseInt(selectSub
}
catch (NumberFormatException e)
{
isNum = false; // keeps track of wrong entry
}
if ( subtractionData == (genNumOne - genNumTwo) )
{
JOptionPane.showMessageDia
"Very good!",
"Well done",
JOptionPane.INFORMATION_ME
result = result + 100;
}
else
if (isNum == true)
{
JOptionPane.showMessageDia
"Sorry that was incorrect. Better luck next time",
"Bad luck",
JOptionPane.INFORMATION_ME
}
else // if we have the wrong entry
{
JOptionPane.showMessageDia
"Please enter a number between 1 and 4",
"Wrong entry",
JOptionPane.ERROR_MESSAGE)
isNum = true;
}
}
public static void multiplication()
{
boolean isNum = true;
int multiplicationData = 0;
int genNumOne, genNumTwo;
String selectMultiplication;
genNumOne = (int) (Math.random() * 9) + 0;
genNumTwo = (int) (Math.random() * 9) + 0;
selectMultiplication = JOptionPane.showInputDialo
" " + "times" + " " + genNumTwo + "?");
try
{
multiplicationData = Integer.parseInt(selectMul
}
catch (NumberFormatException e)
{
isNum = false; // keeps track of wrong entry
}
if ( multiplicationData == (genNumOne * genNumTwo) )
{
JOptionPane.showMessageDia
"Very good!",
"Well done",
JOptionPane.INFORMATION_ME
result = result + 100;
}
else
if (isNum == true)
{
JOptionPane.showMessageDia
"Sorry that was incorrect. Better luck next time",
"Bad luck",
JOptionPane.INFORMATION_ME
}
else // if we have the wrong entry
{
JOptionPane.showMessageDia
"Please enter a number between 1 and 4",
"Wrong entry",
JOptionPane.ERROR_MESSAGE)
isNum = true;
}
}
public static void correctAnswer()
{
}
public static void incorrectAnswer()
{
}
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
to do the multiplayer, do exactly what the instructions say
-- At the beginning of the main method prompt for number of players
once you have that, do the following step:
-- Now create an array of this size to hold doubles. This will hold each players percentage correct.
(an array of length equal to the number of players). Actually in your case, create 3 arrays (result, count, correct)
--Now put a for loop around the rest of the main method to loop as many times as there are players.
for (player # 1 ... n)
repeat what you have in your main method now for one player
-- Adjust the menu to indicate the player number
use the arrays that you created before,
--Just before the end of the for loop store that players correct percentage in the array.
to save the information for that player
--After the for loop at the end of the main method put another for loop that reads the array and displays the percentages for each player.
-- At the beginning of the main method prompt for number of players
once you have that, do the following step:
-- Now create an array of this size to hold doubles. This will hold each players percentage correct.
(an array of length equal to the number of players). Actually in your case, create 3 arrays (result, count, correct)
--Now put a for loop around the rest of the main method to loop as many times as there are players.
for (player # 1 ... n)
repeat what you have in your main method now for one player
-- Adjust the menu to indicate the player number
use the arrays that you created before,
--Just before the end of the for loop store that players correct percentage in the array.
to save the information for that player
--After the for loop at the end of the main method put another for loop that reads the array and displays the percentages for each player.
ASKER
I don't know why at my end results I'm getting a random arrBad message :S
Any ideas on how to do the rest of the code for the mutiplayers etc?
/**
* Kids additon, subtraction, multiplication calculator
* that generates 2 random numbers between 0 and 9.
* 21 September 2007
*/
import javax.swing.JOptionPane; // Needed for JOptionPane
import java.util.Random;
public class KidsCalc
{
//Initialise result & count to 0
public static int result = 0;
public static int count = 0;
public static int correct = 0;
public static void main(String[] args)
{
int userChoice = 0;
String userChoiceStr;
do
{
// user input prompt
userChoiceStr = JOptionPane.showInputDialo
"1. Practice addition\n"
+ "2. Practice subtraction\n"
+ "3. Practice multiplication\n"
+ "4. Quit the program\n"
+ "\n"
+ "Enter your choice",
"Enter your choice",
JOptionPane.QUESTION_MESSA
// keeps track of wrong entry
try
{
userChoice = Integer.parseInt(userChoic
}
catch (NumberFormatException e)
{
}
// switch statement to count user score
switch (userChoice)
{
case 1:
++count;
addition ();
break;
case 2:
++count;
subtraction ();
break;
case 3:
++count;
multiplication ();
break;
case 4:
break;
default:
// prompt if user doesn't enter a number between 1-4
JOptionPane.showMessageDia
"Please enter a number between 1 and 4",
"Wrong entry",
JOptionPane.ERROR_MESSAGE)
}
}
// returns your percentage if input not equal to 4
while ( userChoice != 4 );
JOptionPane.showMessageDia
(result / count) + "% right!!" );
if (result == correct)
correctAnswer() ;
else
incorrectAnswer() ;
// Ends program
System.exit(0);
}
public static void addition()
{
boolean isNum = true;
int additionData = 0;
int genNumOne, genNumTwo;
String selectAddition;
// generates 2 random numbers between 0 and 9
genNumOne = (int) (Math.random() * 9) + 0;
genNumTwo = (int) (Math.random() * 9) + 0;
selectAddition = JOptionPane.showInputDialo
" " + "plus" + " " + genNumTwo + "?");
try
{
additionData = Integer.parseInt(selectAdd
}
catch (NumberFormatException e)
{
isNum = false; //keeps track of wrong entry
}
if ( additionData == (genNumOne + genNumTwo) )
{
result = result + 100;
correctAnswer() ;
}
else
if (isNum == true)
{
incorrectAnswer() ;
}
else // if we have the wrong entry
{
JOptionPane.showMessageDia
"Please enter a number between 1 and 4",
"Wrong entry",
JOptionPane.ERROR_MESSAGE)
isNum = true;
}
}
public static void subtraction()
{
boolean isNum = true;
int subtractionData = 0;
int genNumOne, genNumTwo;
String selectSubtraction;
genNumOne = (int) (Math.random() * 9) + 0;
genNumTwo = (int) (Math.random() * genNumOne) + 0;
// makes sure first random number is greater than second
selectSubtraction = JOptionPane.showInputDialo
" " + "minus" + " " + genNumTwo + "?");
try
{
subtractionData = Integer.parseInt(selectSub
}
catch (NumberFormatException e)
{
isNum = false; // keeps track of wrong entry
}
if ( subtractionData == (genNumOne - genNumTwo) )
{
result = result + 100;
correctAnswer() ;
}
else
if (isNum == true)
{
incorrectAnswer() ;
}
else // if we have the wrong entry
{
JOptionPane.showMessageDia
"Please enter a number between 1 and 4",
"Wrong entry",
JOptionPane.ERROR_MESSAGE)
isNum = true;
}
}
public static void multiplication()
{
boolean isNum = true;
int multiplicationData = 0;
int genNumOne, genNumTwo;
String selectMultiplication;
genNumOne = (int) (Math.random() * 9) + 0;
genNumTwo = (int) (Math.random() * 9) + 0;
selectMultiplication = JOptionPane.showInputDialo
" " + "times" + " " + genNumTwo + "?");
try
{
multiplicationData = Integer.parseInt(selectMul
}
catch (NumberFormatException e)
{
isNum = false; // keeps track of wrong entry
}
if ( multiplicationData == (genNumOne * genNumTwo) )
{
result = result + 100;
correctAnswer() ;
}
else
if (isNum == true)
{
incorrectAnswer() ;
}
else // if we have the wrong entry
{
JOptionPane.showMessageDia
"Please enter a number between 1 and 4",
"Wrong entry",
JOptionPane.ERROR_MESSAGE)
isNum = true;
}
}
public static void correctAnswer()
{
String[] arrGood = { "Very good!", "Well done", "Great!" };
Random randGood = new Random();
int new_rand_index = randGood.nextInt( arrGood.length );
String message = arrGood[ new_rand_index ];
JOptionPane.showMessageDia
"Good Answer",
JOptionPane.INFORMATION_ME
}
public static void incorrectAnswer()
{
String[] arrBad = { "Sorry that was incorrect.", "Better luck next time", "Bad luck" };
Random randBad = new Random();
int new_rand_index = randBad.nextInt( arrBad.length );
String message = arrBad[ new_rand_index ];
JOptionPane.showMessageDia
"Bad Answer",
JOptionPane.INFORMATION_ME
}
}