In: Computer Science
Write a JAVA application that asks elementary students a set of 10 math problems
Note: Hi, Just because of time limitation for answering this question, I am not able to comment the whole code as the total code is of more than 340 lines.
Java Code :
import java.util.*; // for using Scanner class and
Random class
class Problems
{
public static void main(String args[])
{
Scanner obj = new
Scanner(System.in);
while(true)
{
int level ,
problem_type;
while(true)
{
System.out.print("\nEnter level No. (1-3) :
");
level = obj.nextInt();
if(level <1 || level>3)
System.out.println("\nInvalid
Level No. Pleaase try again ! ");
else
break;
}
while(true)
{
System.out.print("\nEnter problem type (1-4) :
");
problem_type = obj.nextInt();
if(problem_type <1 ||
problem_type>4)
System.out.println("\nInvalid
Problem Type. Pleaase try again ! ");
else
break;
}
int correct = 0
, incorrect = 0;
for(int
i=1;i<=10;i++)
{
Random rand = new Random();
if(level == 1)
{
int first =
rand.nextInt(10);
int second =
rand.nextInt(10);
int third =
rand.nextInt(10);
if(problem_type == 1)
{
System.out.print("\n first Number = "+first);
System.out.print("\n second Number = "+second);
System.out.print("\n third Number = "+third);
System.out.print("\nEnter the sum of these three numbers :
");
int sum =
obj.nextInt();
if(sum ==
(first + second + third) )
correct += 3;
else
incorrect += 3;
}
else if(problem_type ==
2)
{
System.out.print("\n first Number = "+first);
System.out.print("\n second Number = "+second);
System.out.print("\n third Number = "+third);
System.out.print("\nEnter the integer average of these three
numbers : ");
int avg =
obj.nextInt();
if(avg ==
(first + second + third)/3 )
{
correct += 3;
System.out.println("Correct Answer !");
}
else
{
incorrect += 3;
System.out.println("Incorrect Answer !");
}
}
else if(problem_type ==
3)
{
System.out.print("\n first Number = "+first);
System.out.print("\n second Number = "+second);
System.out.print("\n third Number = "+third);
int
largest = first;
if(second
>= first && second >= third)
largest = second;
if(third
>= second && third >= first)
largest = third;
System.out.println("\nEnter the largest of these three numbers :
");
int input
= obj.nextInt();
if(largest
== input )
{
correct += 3;
System.out.println("Correct Answer !");
}
else
{
incorrect += 3;
System.out.println("Incorrect Answer !");
}
}
else if(problem_type ==
4)
{
System.out.print("\n first Number = "+first);
System.out.print("\n second Number = "+second);
System.out.print("\n third Number = "+third);
int
smallest = first;
if(second
<= first && second <= third)
smallest = second;
if(third
<= second && third <= first)
smallest = third;
System.out.println("\nEnter the largest of these three numbers :
");
int input
= obj.nextInt();
if(smallest == input )
{
correct += 3;
System.out.println("Correct Answer !");
}
else
{
incorrect += 3;
System.out.println("Incorrect Answer !");
}
}
}
else if(level == 2)
{
int first =
rand.nextInt(100);
int second =
rand.nextInt(100);
int third =
rand.nextInt(100);
if(problem_type == 1)
{
System.out.print("\n first Number = "+first);
System.out.print("\n second Number = "+second);
System.out.print("\n third Number = "+third);
System.out.println("\nEnter the sum of these three numbers :
");
int sum =
obj.nextInt();
if(sum ==
(first + second + third) )
{
correct += 3;
System.out.println("Correct Answer !");
}
else
{
incorrect += 3;
System.out.println("Incorrect Answer !");
}
}
else if(problem_type ==
2)
{
System.out.print("\n first Number = "+first);
System.out.print("\n second Number = "+second);
System.out.print("\n third Number = "+third);
System.out.println("\nEnter the integer average of these three
numbers : ");
int avg =
obj.nextInt();
if(avg ==
(first + second + third)/3 )
{
correct += 3;
System.out.println("Correct Answer !");
}
else
{
incorrect += 3;
System.out.println("Incorrect Answer !");
}
}
else if(problem_type ==
3)
{
System.out.print("\n first Number = "+first);
System.out.print("\n second Number = "+second);
System.out.print("\n third Number = "+third);
int
largest = first;
if(second
>= first && second >= third)
largest = second;
if(third
>= second && third >= first)
largest = third;
System.out.print("\nEnter the largest of these three numbers :
");
int input
= obj.nextInt();
if(largest
== input )
{
correct += 3;
System.out.println("Correct Answer !");
}
else
{
incorrect += 3;
System.out.println("Incorrect Answer !");
}
}
else if(problem_type ==
4)
{
System.out.print("\n first Number = "+first);
System.out.print("\n second Number = "+second);
System.out.print("\n third Number = "+third);
int
smallest = first;
if(second
<= first && second <= third)
smallest = second;
if(third
<= second && third <= first)
smallest = third;
System.out.print("\nEnter the largest of these three numbers :
");
int input
= obj.nextInt();
if(smallest == input )
{
correct += 3;
System.out.println("Correct Answer !");
}
else
{
incorrect += 3;
System.out.println("Incorrect Answer !");
}
}
}
else if(level == 3)
{
int first =
rand.nextInt(1000);
int second =
rand.nextInt(1000);
int third =
rand.nextInt(1000);
if(problem_type == 1)
{
System.out.print("\n first Number = "+first);
System.out.print("\n second Number = "+second);
System.out.print("\n third Number = "+third);
System.out.print("\nEnter the sum of these three numbers :
");
int sum =
obj.nextInt();
if(sum ==
(first + second + third) )
{
correct += 3;
System.out.println("Correct Answer !");
}
else
{
incorrect += 3;
System.out.println("Incorrect Answer !");
}
}
else if(problem_type ==
2)
{
System.out.print("\n first Number = "+first);
System.out.print("\n second Number = "+second);
System.out.print("\n third Number = "+third);
System.out.println("\nEnter the integer average of these three
numbers : ");
int avg =
obj.nextInt();
if(avg ==
(first + second + third)/3 )
{
correct += 3;
System.out.println("Correct Answer !");
}
else
{
incorrect += 3;
System.out.println("Incorrect Answer !");
}
}
else if(problem_type ==
3)
{
System.out.print("\n first Number = "+first);
System.out.print("\n second Number = "+second);
System.out.print("\n third Number = "+third);
int
largest = first;
if(second
>= first && second >= third)
largest = second;
if(third
>= second && third >= first)
largest = third;
System.out.print("\nEnter the largest of these three numbers :
");
int input
= obj.nextInt();
if(largest
== input )
{
correct += 3;
System.out.println("Correct Answer !");
}
else
{
incorrect += 3;
System.out.println("Incorrect Answer !");
}
}
else if(problem_type ==
4)
{
System.out.print("\n first Number = "+first);
System.out.print("\n second Number = "+second);
System.out.print("\n third Number = "+third);
int
smallest = first;
if(second
<= first && second <= third)
smallest = second;
if(third
<= second && third <= first)
smallest = third;
System.out.print("\nEnter the largest of these three numbers :
");
int input
= obj.nextInt();
if(smallest == input )
{
correct += 3;
System.out.println("Correct Answer !");
}
else
{
incorrect += 3;
System.out.println("Incorrect Answer !");
}
}
}
}
System.out.println("\nYou got "+correct+" marks out of 30 ");
System.out.print("\nDo you want to play again (yes / no)? ");
String choice =
obj.next();
if(choice.equals("no"))
{
System.out.println("Thanks for playing !
");
break;
}
}
}
}
Output :