In: Computer Science
Create an application that can be used to practice adding, subtracting, multiplying, and dividing numbers. The application should display a math problem on the screen and then allow the student to enter the answer and also verify that the answer is correct. The application should give the student as many chances as necessary to answer the problem correctly. The math problems should use random integers from 1 through 20, inclusive. The subtraction problems should never ask the student to subtract a larger number from a smaller one. The division problems should never ask the student to divide a smaller number by a larger number. Also, the answer to the division problems should always result in a whole number. The application should keep track of the number of correct and incorrect responses made by the student. The interface should include a button that allows the user to reset the counters for a different student.
//Code in java
import javafx.application.Application;
import java.util.*;
import javafx.scene.Scene;
import javafx.stage.Stage;
import javafx.stage.Stage;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.GridPane;
import javafx.event.EventHandler;
import javafx.event.ActionEvent;
public class MyApplication extends Application{
public void start(Stage stage) {
Random R= new Random();
GridPane root = new
GridPane();
int a,b;
Label a1=new Label("First
Number ");
Label a2 = new Label("Second
Number ");
Label a3 = new
Label("Operation ");
Label a4 = new Label("Enter
Your answer ");
Label a5 = new Label("Result
after checking ");
TextField tf1=new
TextField();
TextField tf2=new
TextField();
TextField tf3=new
TextField();
TextField tf4=new
TextField();
TextField tf5=new
TextField();
tf1.setEditable(false);
tf2.setEditable(false);
tf3.setEditable(false);
tf5.setEditable(false);
Button b1 = new Button("Check Result");
Button b2 = new
Button("CLEAR");
a = R.nextInt(20);
b = R.nextInt(20);
tf1.setText( Integer.toString(a));
tf2.setText( Integer.toString(b));
if(b > 0 && a%b == 0 ){
tf3.setText("Devision");
}else if(a > b) {
tf3.setText("Subtraction");
}else if(b%2 == 0){
tf3.setText("Addition");
}else{
tf3.setText("Multiplication");
}
b1.setOnAction(e ->{
String st = tf4.getText();
String s = tf3.getText();
int an,ans=
Integer.parseInt(st);
if(s.compareTo("Devision") == 0){
an=a/b;
}else if(s.compareTo("Subtraction")
== 0 ){
an = a-b;
}else
if(s.compareTo("Addition")==0){
an = a+b;
}else
an = a*b;
if(an == ans ){
tf5.setText("Right answer ");
}else
tf5.setText("Wrong answer Correct answer is "+an);
});
root.addRow(0, a1,
tf1);
root.addRow(1, a2,
tf2);
root.addRow(2, a3,tf3);
root.addRow(3, a4,tf4);
root.addRow(5, b1 ,b2);
root.addRow(6, a5,tf5);
b2.setOnAction(new
EventHandler
public
void handle(ActionEvent event) {
tf1.clear();
tf2.clear();
tf3.clear();
tf4.clear();
}
});
Scene scene=new
Scene(root,350,200);
stage.setScene(scene);
stage.setTitle("Text Field
Example");
stage.show();
}
}
//Screenshot of output terminal