Here the assignment: This assignment will have you use Random and methods to imp
ID: 3545135 • Letter: H
Question
Here the assignment:
This assignment will have you use Random and methods to implement a flash card program for teach your kid to add and subtract and multiply and divide.
Your program will have a loop with a sentinel value. If the user answers "Yes", "yes", or "yEs", it will generate a new flashcard. This will be a post-test loop (since no point in running a flash card program without at least one flash card.
In the loop, generate a random integer in the rang 0, 1, 2, 3.
If it is a 0, we are going to generate an "Add" flash card,
If it is a 1, we are going to generate an "Subtract" flash card,
If it is a 2, we are going to generate an "Multiply" flash card,
If it is a 3, we are going to generate an "Divide" flash card,
Generate two random integers in the range 1-99
Generate an appropriate prompt string, like "45 + 17", for example, if the type is add, and the two numbers are 45 and 17.
Display a flash card using JOptionPane.showInputDialog to get the student's response.
For add and subtract, round is no issue, right? Not for multiply either, as the product of two integers is an integer.
But divide, there's the rub.
The response is easiest to handle if it's always a double. Get the string, and use Double.parseDouble(...) to convert to a double.
Extra credit - use try...catch to avoid crashing on bogus input.
Create four methods, each returning a boolean, with three arguments, the two random test numbers, and the response double, to return a true or false, depending on whether the answer is correct or not.
Call the methods okPlus, okMinus, okMultiply, and okDivide.
For divide, it's tricky. Return true if the answer is correct to two decimal places.
Hint on okDivide - use Math.abs(correctAnswer - userAnswer) and see if it is less than 0.01.
okDivide(10,3,3.33) should return a true, and okDivide (10,3,3.34) should return a false.
Use the appropriate ok method (use select ... case) to choose the correct ok... method to determine whether the answer is correct or not. Use the result of the ok...method to tell the user whether the answer is correct or not. Ten prompt for another flash card. b
bonus extra credit, on wrong answers, give the user more chances.
You may want to use JOptionPane.showInputDialog("More?) to decide on that post-test loop whether to generate more flash cards.
10 pts extra credit, - have it in by noon on Sunday.
Classname: "Week05". First line of code is a comment with your name and "Week05" in it.
When the program runs, first display has your name and "Week05" in it - use JOptionPane.showMessageDialog()...
You should use Random, Select...case, and the four methods as named above.
Here is my program:
//*Eduardo Zamudio Week 05/*
import java.util.Random;
import javax.swing.JOptionPane;
?
public class Week05 {
public static void main(String[] args) {
System.out.println("Eduardo Zamudio Week 05");{
while (true){
Random numberGenerator = new Random();
int operator = numberGenerator.nextInt(4);
int x = numberGenerator.nextInt(99) + 1;
int y = numberGenerator.nextInt(99) + 1;
boolean userCorrect;
switch (operator){
case 0:{
int userInput = Integer.parseInt(JOptionPane.showInputDialog("add" + x + "+" + y));
userCorrect = okPluse(x,y,userInput);
}
case 1:{
int userInput1 = Integer.parseInt(JOptionPane.showInputDialog("Subtract" + x + "-" + y));
userCorrect = okMinus(x,y,userInput1);
}
case 2:{
int userInput2 = Integer.parseInt(JOptionPane.showInputDialog("Multiply" + x + "*" + y));
userCorrect = okMultiply(x,y,userInput2);
}
case 3:{
double userInput3 = Double.parseDouble(JOptionPane.showInputDialog("Divide" + x + "/" + y));
userCorrect = okDivide(x,y,userInput3);
break;
}
}
}
}
}
private static boolean okDivide(int x, int y, double userInput3) {
double correctAnswer = (double)x/(double)y;
if (Math.abs(correctAnswer-userInput3)<(0.01));
else
return false;
}
private static boolean okMultiply(int x, int y, int userInput2) {
int correctAnswer = x * y;
if (correctAnswer == userInput2)
return false;
}
private static boolean okMinus(int x, int y, int userInput1) {
int correctAnswer = x - y;
if (correctAnswer == userInput1)
else
return false;
}
private static boolean okPluse(int x, int y, int userInput) {
int correctAnswer = x + y;
if (correctAnswer == userInput){
return false;
}
}
}
Explanation / Answer
//*Eduardo Zamudio Week 05/*
import java.util.Random;
import javax.swing.JOptionPane;
public class Week05 {
public static void main(String[] args) {
System.out.println("Eduardo Zamudio Week 05");
String more = "";
do {
Random numberGenerator = new Random();
int operator = numberGenerator.nextInt(4);
int x = numberGenerator.nextInt(99) + 1;
int y = numberGenerator.nextInt(99) + 1;
boolean userCorrect = false;
switch (operator){
case 0:{
int userInput = Integer.parseInt(JOptionPane.showInputDialog("Add " + x + "+" + y));
userCorrect = okPluse(x,y,userInput);
break;
}
case 1:{
int userInput1 = Integer.parseInt(JOptionPane.showInputDialog("Subtract " + x + "-" + y));
userCorrect = okMinus(x,y,userInput1);
break;
}
case 2:{
int userInput2 = Integer.parseInt(JOptionPane.showInputDialog("Multiply " + x + "*" + y));
userCorrect = okMultiply(x,y,userInput2);
break;
}
case 3:{
double userInput3 = Double.parseDouble(JOptionPane.showInputDialog("Divide " + x + "/" + y));
userCorrect = okDivide(x,y,userInput3);
break;
}
}
if (userCorrect)
JOptionPane.showMessageDialog(null, "Correct.");
else
JOptionPane.showMessageDialog(null, "Wrong.");
more = JOptionPane.showInputDialog("More?");
} while (more.equalsIgnoreCase("yes"));
}
private static boolean okDivide(int x, int y, double userInput3) {
double correctAnswer = (double)x/(double)y;
if (Math.abs(correctAnswer-userInput3)<(0.01))
return true;
else
return false;
}
private static boolean okMultiply(int x, int y, double userInput2) {
int correctAnswer = x * y;
if (correctAnswer == userInput2)
return true;
else
return false;
}
private static boolean okMinus(int x, int y, double userInput1) {
int correctAnswer = x - y;
if (correctAnswer == userInput1)
return true;
else
return false;
}
private static boolean okPluse(int x, int y, double userInput) {
int correctAnswer = x + y;
if (correctAnswer == userInput)
return true;
else
return false;
}
}