In: Computer Science
Write a JAVA application that asks elementary students a set of 10 math problems
//---------- MathOps.java -------------
import java.util.Random;
import java.util.Scanner;
class MathOps
{
Scanner in ;
Random rand;
String[] positive = {"Good, You got it","WoW, You are
a genious","You are rocking. Keep it up"};
String[] negative = {"Oops, Wrong answer","Sorry,
Wrong answer. keep practice","You missed again, keep focus"};
MathOps()
{
in = new Scanner(System.in);
rand = new Random();
}
public int getRand(int range)
{
return rand.nextInt(range);
}
public void printLevels()
{
System.out.println("\n------------
Select Math Level -----------\n");
System.out.println("1. Level-1
values of Operands in the range of 0-9");
System.out.println("2. Level-2
values of Operands in the range of 0-99");
System.out.println("3. Level-3
values of Operands in the range of 0-999");
System.out.print("Enter your
choice: ");
}
public int getLevels()
{
boolean isValid = false;
int level = 0;
while(!isValid)
{
try
{
printLevels();
level = in.nextInt();
if(level <1 || level > 3)
{
System.out.println("\nInvalid
Level entered. Try Again");
}
else
{
isValid = true;
}
}
catch(Exception
e)
{
System.out.println("\nInvalid Level entered. Try
Again");
in.nextLine();
}
}
return level;
}
public int getAnswer(String question)
{
boolean isValid =false;
int ans=-1;
while(!isValid)
{
try
{
System.out.print("\n"+question);
ans = in.nextInt();
isValid = true;
}
catch(Exception
e)
{
in.nextLine();
System.out.println("\nInvalid Answer, Answer
must be a integer");
}
}
return ans;
}
public int getSum(int opOne,int opTwo , int
opThree)
{
return opOne + opTwo +
opThree;
}
public int getIntAverage(int opOne,int opTwo , int
opThree)
{
return
getSum(opOne,opTwo,opThree)/3;
}
public int getMax(int opOne,int opTwo , int
opThree)
{
return
Math.max(opOne,Math.max(opTwo,opThree));
}
public int getMin(int opOne,int opTwo , int
opThree)
{
return
Math.min(opOne,Math.min(opTwo,opThree));
}
public void simulate(int level)
{
int opRange;
if(level == 1)
{
opRange =
10;
}
else if(level == 2)
{
opRange =
100;
}
else
{
opRange =
1000;
}
int prblmType;
int opOne;
int opTwo;
int opThree;
int ans;
int correctAns;
int score = 0;
for(int i =1 ; i <= 10;
i++)
{
prblmType =
getRand(4)+1;
//System.out.println("\nProblme Type: "+prblmType);
opOne =
getRand(opRange);
opTwo =
getRand(opRange);
opThree =
getRand(opRange);
System.out.println("\n-------------------------------------------------------");
if(prblmType ==
1)
{
ans = getAnswer(String.format("Sum of %d , %d ,
%d = ",opOne,opTwo,opThree));
correctAns = getSum(opOne,opTwo,opThree);
}
else
if(prblmType == 2)
{
ans = getAnswer(String.format("Integer average
of %d , %d , %d = ",opOne,opTwo,opThree));
correctAns =
getIntAverage(opOne,opTwo,opThree);
}
else
if(prblmType == 3)
{
ans = getAnswer(String.format("Maximum of %d ,
%d , %d = ",opOne,opTwo,opThree));
correctAns = getMax(opOne,opTwo,opThree);
}
else
{
ans = getAnswer(String.format("Minimum of %d ,
%d , %d = ",opOne,opTwo,opThree));
correctAns = getMin(opOne,opTwo,opThree);
}
if(ans ==
correctAns)
{
score++;
System.out.println("\n"+positive[getRand(3)]);
}
else
{
System.out.println("\n"+negative[getRand(3)]);
System.out.println("\nThe Correct Answer is:
"+correctAns);
}
}
System.out.println("\n\nYour Total
Score: "+score);
}
public static void main(String[] args)
{
MathOps tester = new
MathOps();
Scanner in= new
Scanner(System.in);
int level;
String choice;
do
{
level =
tester.getLevels();
tester.simulate(level);
System.out.println("Do you want to play again?n.no or any key to
continue: ");
choice =
in.nextLine();
}while(!choice.equalsIgnoreCase("n"));
}
}
/*
* -------------- OUTPUT ----------
*
------------ Select Math Level -----------
1. Level-1 values of Operands in the range of 0-9
2. Level-2 values of Operands in the range of 0-99
3. Level-3 values of Operands in the range of 0-999
Enter your choice: 1
-------------------------------------------------------
Maximum of 8 , 0 , 4 = 0
Oops, Wrong answer
The Correct Answer is: 8
-------------------------------------------------------
Integer average of 9 , 6 , 8 = 7
WoW, You are a genious
-------------------------------------------------------
Sum of 0 , 1 , 2 = 3
You are rocking. Keep it up
-------------------------------------------------------
Minimum of 8 , 6 , 9 = 3
Oops, Wrong answer
The Correct Answer is: 6
-------------------------------------------------------
Minimum of 9 , 9 , 1 = 9
You missed again, keep focus
The Correct Answer is: 1
-------------------------------------------------------
Sum of 8 , 7 , 0 = 9
Sorry, Wrong answer. keep practice
The Correct Answer is: 15
-------------------------------------------------------
Integer average of 5 , 7 , 0 = 4
Good, You got it
-------------------------------------------------------
Integer average of 6 , 0 , 0 = 2
WoW, You are a genious
-------------------------------------------------------
Integer average of 8 , 2 , 0 = 9
Sorry, Wrong answer. keep practice
The Correct Answer is: 3
-------------------------------------------------------
Maximum of 8 , 3 , 7 = 4
Oops, Wrong answer
The Correct Answer is: 8
Your Total Score: 4
Do you want to play again?n.no or any key to continue:
y
------------ Select Math Level -----------
1. Level-1 values of Operands in the range of 0-9
2. Level-2 values of Operands in the range of 0-99
3. Level-3 values of Operands in the range of 0-999
Enter your choice: 3
-------------------------------------------------------
Maximum of 22 , 691 , 310 = 1
You missed again, keep focus
The Correct Answer is: 691
-------------------------------------------------------
Integer average of 432 , 760 , 604 = 2
You missed again, keep focus
The Correct Answer is: 598
-------------------------------------------------------
Maximum of 563 , 94 , 215 = 3
Sorry, Wrong answer. keep practice
The Correct Answer is: 563
-------------------------------------------------------
Integer average of 120 , 245 , 573 = 4
Sorry, Wrong answer. keep practice
The Correct Answer is: 312
-------------------------------------------------------
Integer average of 506 , 47 , 761 = 5
Sorry, Wrong answer. keep practice
The Correct Answer is: 438
-------------------------------------------------------
Integer average of 312 , 8 , 934 = 6
You missed again, keep focus
The Correct Answer is: 418
-------------------------------------------------------
Maximum of 905 , 380 , 995 = 7
Sorry, Wrong answer. keep practice
The Correct Answer is: 995
-------------------------------------------------------
Integer average of 966 , 886 , 775 = 8
You missed again, keep focus
The Correct Answer is: 875
-------------------------------------------------------
Integer average of 513 , 340 , 56 = 9
You missed again, keep focus
The Correct Answer is: 303
-------------------------------------------------------
Integer average of 302 , 422 , 984 = 10
Sorry, Wrong answer. keep practice
The Correct Answer is: 569
Your Total Score: 0
Do you want to play again?n.no or any key to continue:
n
------------------
(program exited with code: 0)
Press any key to continue . . .
*/