In: Computer Science
JAVAFX
You will create a GUI that solves the quadratic equation shown below.
Part 1: Create the GUI 25%
Create a user interface that offers the user a space to enter values for A, B, and C. (Use a different text field for each.) Be sure to either set the text in these so that the user knows which is for A, which is for B, and which is for C or add labels so that it is clear.
Create a button that the user will press to generate the answers. (Or make sure it’s clear how the user should submit their values in another way.)
Create a node that will display both X values that result from the math. This can be 2 labels, a single label, a Text object… whatever.
Put these in a simple layout and make sure everything displays before moving on. Add enough styling so that things look neat.
Part 2: Add EventHandler 75%
After your nodes are created but before your scene and stage commands, add
an event handler. I would like it to be an ActionEvent type.
Inside of the handle method, you will get the values from A, B, and C and solve for both X values. Make sure you store A, B, and C as doubles, not integers. Display the two answers that you get back to the GUI. There will be times where X will have no values. Handle this as addressed below.
Be sure to check first to see if A has a 0 in it. If it does, the math should not be performed and the user should be informed that A cannot be 0.
Also check to see if the discriminant is negative. This is the part that occurs under the square root. We know that if it’s negative, then finding the square root of it will result in an imaginary number. Your program won’t crash if this happens but Java will display either ??? or NaN. Have the program check the discriminant before continuing with the math and display a custom message if the discriminant is negative.
The only acceptable printed outcomes are: a message saying A cannot be 0; a message saying that the inputted values will result in an imaginary number; or the two X values that result from the equation.
One final task: Make sure both X answers are formatted to three decimal places. You can use String.format(), which was covered in Lab 1, the DecimalFormat class, or any other means for displaying 3 decimal places accurately.
Test values are below.
A: 3.0, B: 5.0, C: -2.0 → X = -2.000 and X = 0.333
A: -3.0, B: 10.0, C: 8.0 → X = 4.000 and X = -0.666
Code
/*
* To change this license header, choose License Headers in Project
Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package the.quadratic.equation;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.HPos;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.*;
import javafx.scene.control.Alert.AlertType;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
/**
*
* @author JOSEE
*/
public class TheQuadraticEquation extends Application {
private TextField tfAValue = new TextField();
private TextField tfBValue = new TextField();
private TextField tfCValue = new TextField();
private Label AText = new Label("Enter Value A: ");
private Label BText = new Label("Enter Value B: ");
private Label CText = new Label("Enter Value C: ");
private Label lblRoot1 = new Label();
private Label lblRoot2 = new Label();
// create a alert
private Alert alertBox = new Alert(AlertType.NONE);
private Button btn = new Button("Calculate");
@Override
public void start(Stage primaryStage) {
GridPane gridPane = new GridPane();
gridPane.setHgap(5);
gridPane.setVgap(10);
gridPane.add(AText,0,0);
gridPane.add(tfAValue,1,0);
gridPane.add(BText,0,1);
gridPane.add(tfBValue,1,1);
gridPane.add(CText,0,2);
gridPane.add(tfCValue,1,2);
gridPane.add(btn, 0, 3, 2, 1);
gridPane.add(lblRoot1,0,4,2,1);
gridPane.add(lblRoot2,0,5,2,1);
gridPane.setAlignment(Pos.CENTER);
GridPane.setHalignment(btn, HPos.CENTER);
GridPane.setHalignment(lblRoot1, HPos.CENTER);
GridPane.setHalignment(lblRoot2, HPos.CENTER);
btn.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
double a,b,c;
a=Double.parseDouble(tfAValue.getText());
b=Double.parseDouble(tfBValue.getText());
c=Double.parseDouble(tfCValue.getText());
if(a==0)
{
alertBox.setContentText("The value A can't be 0.");
alertBox.setAlertType(AlertType.ERROR);
alertBox.show();
}
else
{
double D=(b*b-4*a*c);
if(D<0)
{
alertBox.setContentText("The inputted values will result in an
imaginary number");
alertBox.setAlertType(AlertType.ERROR);
alertBox.show();
}
else
{
double r1,r2;
r1 = (-b + Math.sqrt(D)) / (2 * a);
r2 = (-b - Math.sqrt(D)) / (2 * a);
lblRoot1.setText(String.format("Root 1: %.3f",r1));
lblRoot2.setText(String.format("Root 2: %.3f",r2));
}
}
}
});
// Create a scene and place it in the stage
Scene scene = new Scene(gridPane, 400, 250);
primaryStage.setTitle("The Quadratic Equation Solver"); // Set
title
primaryStage.setScene(scene); // Place the scene in the stage
primaryStage.show(); // Display the stage
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
}
Outputs
If you have any query regarding the code please ask me in the comment i am here for help you. Please do not direct thumbs down just ask if you have any query. And if you like my work then please appreciates with up vote. Thank You.