Question

In: Computer Science

JAVAFX You will create a GUI that solves the quadratic equation shown below. Part 1: Create...

JAVAFX

You will create a GUI that solves the quadratic equation shown below.

  • Part 1: Create the GUI 25%

  1. 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.

  1. 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.)

  1. 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.

  1. 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%

  1. 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.

  1. 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.

  1. 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.  

  1. 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.

  1. 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.  

  1. 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

Solutions

Expert Solution

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.


Related Solutions

Write a C++ program that solves a quadratic equation to find its roots. The roots of...
Write a C++ program that solves a quadratic equation to find its roots. The roots of a quadratic equation ax2 + bx + c = 0 (where a is not zero) are given by the formula (–b ± sqrt(b2 – 4ac)) / 2a *Use #include for the use of sqrt. The value of the discriminant (b2 – 4ac) determines the nature of roots. If the value of the discriminant is zero, then the equation has a single real root. If...
Using excel UserForm construct a Flowchart that solves a quadratic equation ax^2+bx+c=0 for changingvalues of a,...
Using excel UserForm construct a Flowchart that solves a quadratic equation ax^2+bx+c=0 for changingvalues of a, b and c. Please also display the code you have used. Please use excel UserForm Thanks
Write a Java program that solves a set of quadratic equations as per Requirement #1 (without...
Write a Java program that solves a set of quadratic equations as per Requirement #1 (without using global variables), and reads an integer number entered in the command line, as follows: > java program_name input_number and depending on the value of this number does the following: 1) If the number entered is positive, the program shall solve (running in a loop) as many quadratic equations of the following form, as the input_number tells: a * x2 + b * x...
Create a GUI matlab code that computes for the position, velocity, and acceleration of the equation...
Create a GUI matlab code that computes for the position, velocity, and acceleration of the equation and displays their respective graph.
How many different ways can you solve a quadratic equation? List them. Create a trinomial that...
How many different ways can you solve a quadratic equation? List them. Create a trinomial that can be factored and write it in standard form. Factor x^2 – 7x + 10, 4x^2 – 81 and p (x) = 3x^3 – 12x What key features of a quadratic graph can be identified and how are the graphs affected when constants or coefficients are added to the parent quadratic equations? Compare the translations to the graph of linear function. Create examples of...
1) Solve the given quadratic equation by using Completing the Square procedure and by Quadratic formula...
1) Solve the given quadratic equation by using Completing the Square procedure and by Quadratic formula ( you must do it both ways). Show all steps for each method and put your answer in simplest radical form possible. 2) Which part of the Quadratic Formula can help you to find the Nature of the roots for the Quadratic Equation. Explain how you can find the nature of the roots and provide one Example for each possible case with solution.
Use the below info to create a java program A GUI interface to ensure a user...
Use the below info to create a java program A GUI interface to ensure a user is old enough to play a game. Properly formatted prompts to input name, address, phone number, and age. Remember that name, address, phone number, etc. can be broken out in additional fields. Refer to the tutorial from this week’s Reading Assignment Multiple vs. Single Field Capture for Phone Number Form Input for help with this. Instructions to ensure that the information is displayed back...
modify the code below to create a GUI program that accepts a String from the user...
modify the code below to create a GUI program that accepts a String from the user in a TextField and reports whether or not there are repeated characters in it. Thus your program is a client of the class which you created in question 1 above. N.B. most of the modification needs to occur in the constructor and the actionPerformed() methods. So you should spend time working out exactly what these two methods are doing, so that you can make...
In java, Create a GUI which works as an accumulator 1. There is a textfield A...
In java, Create a GUI which works as an accumulator 1. There is a textfield A which allows user to enter a number 2. There is also another textfield B with value start with 0. 3. When a user is done with entering the number in textfield A and press enter, calculate the sum of the number in textfield B and the number user just entered in textfield A, and display the updated number in textfield B
PYTHON PROGRAMMING Using tktinker, create a gui that surrounds the code below that has buttons and...
PYTHON PROGRAMMING Using tktinker, create a gui that surrounds the code below that has buttons and labels and an overall theme. # Meet the chatbot Eve print('Hi there! Welcome to the ChatBot station. I am going to ask you a series of questions and all you have to do is answer!') print(' ') print('Lets get started') #begin questions firstName = input('What is your first name?: ') lastName = input('What is your last name? ') print("Hi there, ", firstName + lastName,...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT