In: Computer Science
----- Please solve the questions with the code below. Thank you. -----
Exercise Overview
Refactor your code to enhance the user experience and to use objects and classes.
All functional requirements in Project 1 remain, except where enhancing the system replaces specific functions.
Functional Requirements
Analysis. Describe any analysis that is required for solving the problem, including a discussion of key elements and complex statements or logic of the system.
Design. Describe the major steps for solving the problem (developing the code). A high-level outline of the classes and the variables and methods in the classes is recommended.
Testing. Describe how you tested the system.
----- Please solve the questions with the code below. Thank you. -----
import java.util.Scanner;
import java.util.concurrent.TimeUnit;
import java.time.format.DateTimeFormatter;
import java.time.LocalDateTime;
public class Question {
public static void main(String arg[]) {
String name;
Scanner sc = new
Scanner(System.in);
int num,max,min,count=0;
double num1,num2;
double ans,result;
String str ="";
System.out.print("Enter your name :
");
name = sc.nextLine();
System.out.println("Enter \"A\" for
Addition, \"S\" for Subtraction, \"M\" for Multiplication, \"D\"
for Division: M");
System.out.print("Enter : ");
char ch = sc.next().charAt(0); //
input choice for operation
System.out.print("Enter the number
of problems you wish to work : ");
num = sc.nextInt();
System.out.println("What are the
low and high numbers you want in your problems?");
System.out.print("Enter the low
value for your problems : ");
min = sc.nextInt();
System.out.print("Enter the high
value for your problems : ");
max = sc.nextInt();
long startTime = System.nanoTime();
// start session for questions
if(ch == 'A')
{
str =
"Addition";
for(int
i=0;i<num;i++)
{
num1 = (int)(Math.random() * (max - min + 1) +
min);
num2 = (int)(Math.random() * (max - min + 1) +
min);
System.out.print((int)num1 +" + "+ (int)num2 +"
= ");
ans = sc.nextDouble();
if(ans == (num1+num2))
{
count++;
System.out.println("Correct");
}
else
{
System.out.println("Incorrect");
}
}
}
else if(ch == 'S')
{
str =
"Subtraction";
for(int
i=0;i<num;i++)
{
num1 = (int)(Math.random() * (max - min + 1) +
min);
num2 = (int)(Math.random() * (max - min + 1) +
min);
System.out.print((int)num1 +" - "+ (int)num2 +"
= ");
ans = sc.nextDouble();
if(ans == (num1-num2))
{
count++;
System.out.println("Correct");
}
else
{
System.out.println("Incorrect");
}
}
}
else if(ch == 'M')
{
str =
"Multipication";
for(int
i=0;i<num;i++)
{
num1 = (int)(Math.random() * (max - min + 1) +
min);
num2 = (int)(Math.random() * (max - min + 1) +
min);
System.out.print((int)num1 +" * "+ (int)num2 +"
= ");
ans = sc.nextDouble();
if(ans == (num1*num2))
{
count++;
System.out.println("Correct");
}
else
{
System.out.println("Incorrect");
}
}
}
else if(ch == 'D')
{
str =
"Division";
for(int
i=0;i<num;i++)
{
num1 = (int)(Math.random() * (max - min + 1) +
min);
num2 = (int)(Math.random() * (max - min + 1) +
min);
System.out.print((int)num1 +" / "+ (int)num2 +"
= ");
ans = sc.nextDouble();
result = (num1/num2);
double result1 =
Math.round(result*1000.0)/1000.0;
if(ans == result1 )
{
count++;
System.out.println("Correct
"+result1);
}
else
{
System.out.println("Incorrect
"+result1);
}
}
}
else
{
System.out.println("Wrong Input !");
}
long endTime = System.nanoTime();
// sessio end for question
System.out.println("");
System.out.println("Session Summary
: ");
System.out.println(num+"
problems,"+count+" correct");
double avg =
(count*1.0/num*1.0)*100;
long totalTime = endTime -
startTime; // calculate time in nanoseconds
long seconds =
TimeUnit.NANOSECONDS.toSeconds(totalTime); // convert time
nanoseconds to seconds
System.out.println("Score is
"+(Math.round(avg)) +", Time is : "+seconds+" seconds");
DateTimeFormatter dtf =
DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss"); // date
LocalDateTime now =
LocalDateTime.now(); // local time
System.out.println("Session for
"+name+" was "+str+" on "+dtf.format(now));
}
}
Code Analysis :- we need 4 operation in this problem Addition, subtraction, Multiplication and Division so we analyse that we need four methods for those operations and every method requires two variables and and one operand so we can pass those variables and operand to a method and calculate the result.
Design:- First make a class with name operatin having two private instance variables for storing the value of min and max range and four method for each operation, Each method take a scanner object which is used to take the operand from the user. Then we implement those method carefully so no compilation like syntax errror and runtime error like divide by zero is there, After this we need to work on the Driver class that is used for testing those methods.
Testing:- After implemetation of Operation class we need a Driver class that is Question class having main method to run the class. First we take the name , number of operation user want and high and low range for generating random value for operations. Then make a menu driven program using if else statement to choose the operation. When user enters his choice and high, low value, System will generate Questions for the user if user give correct answer then it count variable will increment .. so we can use this count variable as number of correct answer given by the user. At last we note the time taken by user and print All necessary information of the user like how many correct and incorrect answer he/she given, how much time he taken, how much he scored.
/********************************Operation.java****************************/
import java.util.Scanner;
public class Operation {
/*
* we need those instance variable
*/
private int min;
private int max;
//constructor that instantiate a Operation
public Operation(int min, int max) {
this.min = min;
this.max = max;
}
//return min
public int getMin() {
return min;
}
//set min
public void setMin(int min) {
this.min = min;
}
//return max
public int getMax() {
return max;
}
//set max
public void setMax(int max) {
this.max = max;
}
//addition method
public String addition(Scanner sc) {
int num1 = (int) (Math.random()
* (max - min + 1) + min);
int num2 = (int) (Math.random() *
(max - min + 1) + min);
System.out.print((int) num1 + "
+ " + (int) num2 + " = ");
double ans = sc.nextDouble();
if (ans == (num1 + num2)) {
return
"correct";
} else {
return
"incorrect";
}
}
//subtraction method
public String subtraction(Scanner sc) {
int num1 = (int) (Math.random()
* (max - min + 1) + min);
int num2 = (int) (Math.random() *
(max - min + 1) + min);
System.out.print((int) num1 + "
- " + (int) num2 + " = ");
double ans = sc.nextDouble();
if (ans == (num1 - num2)) {
return
"correct";
} else {
return
"incorrect";
}
}
public String multiplication(Scanner sc) {
int num1 = (int) (Math.random()
* (max - min + 1) + min);
int num2 = (int) (Math.random() *
(max - min + 1) + min);
System.out.print((int) num1 + "
* " + (int) num2 + " = ");
double ans = sc.nextDouble();
if (ans == (num1 * num2)) {
return
"correct";
} else {
return
"incorrect";
}
}
public String division(Scanner sc) {
int num1 = (int) (Math.random()
* (max - min + 1) + min);
int num2 = (int) (Math.random() *
(max - min + 1) + min);
System.out.print((int) num1 + "
/ " + (int) num2 + " = ");
if (num2 == 0) {
System.out.println("Can't Divide!!");
}
double ans = sc.nextDouble();
double result = (num1 /
num2);
double result1 = Math.round(result
* 1000.0) / 1000.0;
if (ans == result1) {
return
"correct " + result1;
} else {
return
"incorrect " + result1;
}
}
}
/*****************************Question.java************************/
import java.util.Scanner;
import java.util.concurrent.TimeUnit;
import java.time.format.DateTimeFormatter;
import java.time.LocalDateTime;
public class Question {
public static void main(String arg[]) {
String name;
Scanner sc = new
Scanner(System.in);
int num, max, min, count =
0;
String str = "";
System.out.print("Enter your name :
");
name = sc.nextLine();
System.out.println(
"Enter \"A\" for Addition, \"S\" for
Subtraction, \"M\" for Multiplication, \"D\" for Division:
M");
System.out.print("Enter : ");
char ch = sc.next().charAt(0); //
input choice for operation
System.out.print("Enter the number
of problems you wish to work : ");
num = sc.nextInt();
System.out.println("What are the
low and high numbers you want in your problems?");
System.out.print("Enter the low
value for your problems : ");
min = sc.nextInt();
System.out.print("Enter the high
value for your problems : ");
max = sc.nextInt();
//create object of Operation
class
Operation op = new Operation(min,
max);
long startTime = System.nanoTime();
// start session for questions
if (ch == 'A') {
str =
"Addition";
for (int i = 0;
i < num; i++) {
String s = op.addition(sc);
if (s.equals("correct")) {
count++;
System.out.println("Correct");
} else {
System.out.println("Incorrect");
}
}
} else if (ch == 'S') {
str =
"Subtraction";
for (int i = 0;
i < num; i++) {
if (op.subtraction(sc).equals("correct"))
{
count++;
System.out.println("Correct");
} else {
System.out.println("Incorrect");
}
}
} else if (ch == 'M') {
str =
"Multiplication";
for (int i = 0;
i < num; i++) {
if (op.multiplication(sc).equals("correct"))
{
count++;
System.out.println("Correct");
} else {
System.out.println("Incorrect");
}
}
} else if (ch == 'D') {
str =
"Division";
for (int i = 0;
i < num; i++) {
String s = op.division(sc);
if (s.split(" ")[0].equals("correct")) {
count++;
System.out.println("Correct "
+ s.split(" ")[1]);
} else {
System.out.println("Incorrect
" + s.split(" ")[1]);
}
}
} else {
System.out.println("Wrong Input !");
}
long endTime = System.nanoTime(); // session end for question
System.out.println("");
System.out.println("Session Summary
: ");
System.out.println(num + "
problems," + count + " correct");
double avg = (count * 1.0 / num *
1.0) * 100;
long totalTime = endTime -
startTime; // calculate time in nanoseconds
long seconds =
TimeUnit.NANOSECONDS.toSeconds(totalTime); // convert time
nanoseconds to seconds
System.out.println("Score is " +
(Math.round(avg)) + ", Time is : " + seconds + " seconds");
DateTimeFormatter dtf =
DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss"); // date
LocalDateTime now =
LocalDateTime.now(); // local time
System.out.println("Session for " +
name + " was " + str + " on " + dtf.format(now));
}
}
/*********************output**********************/
Enter your name : JKS
Enter "A" for Addition, "S" for Subtraction, "M" for
Multiplication, "D" for Division: M
Enter : S
Enter the number of problems you wish to work : 3
What are the low and high numbers you want in your problems?
Enter the low value for your problems : 20
Enter the high value for your problems : 40
38 - 23 = 15
Correct
35 - 34 = 1
Correct
35 - 31 = 4
Correct
Session Summary :
3 problems,3 correct
Score is 100, Time is : 12 seconds
Session for JKS was Subtraction on 2020/10/09 09:31:16
Please let me know if you have any doubt or modify the answer, Thanks :)