Quadratic GUi

ok so i have to use GUI and OOD to make a program that calculates the quadratic eqaution.

not sure how to plug this equation into the Calculate Button, this is my first time using JFrame ext. please help. here are some of the instructions:

For the quadratic equation, ax2 + bx + c = 0, the two solutions (roots x1, and x2) for x are given by:

1. Create a GUI program, similar to the examples shown in chapter 6, that has text fields for the coefficients a, b, and c.

2. Create two additional text fields that will display the roots x1, and x2.

3. Create a button labeled Calculate that will perform the following behavior:

a. The program will first check to verify that a is not equal to zero. When a is equal to zero the quadratic equation is not in a quadratic form and computational attempts to use the quadratic equation will result in a divide-by-zero error. If a is equal to zero, flag a warning to user (in a JOptionPane pop-up or by any method you prefer) and set the root text fields to blank.

b. The program then checks to verify that the quantity (b*b -4ac) >= 0. If this is NOT the case, the program will result in non-real roots that cannot easily be displayed AND attempts to use the quadratic equation will result in attempting to take the square root of a negative value. If this is the case, flag a warning similar to the warning in section a, and set the root text fields to blank.

c. If a is not equal to 0 and (b*b -4ac) >= 0, then proceed to use quadratic equation to calculate the two roots and then populate the root text fields with these values.

4. The user will have then have the option of utilizing the quadratic equation for multiple coefficient sets, and can exit the program by selecting the "X".

The user interface does not have to be beautiful, advanced GUI is covered in chapter 13, but there should be ample field area to easily read labels, and enter any values the user selects. There should be text labels to easily identify each field.

here is my code so far


/* Programmer:
Title: Lab 6
Date: October 28, 2008
Filename: lab6.java

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class lab6 extends JFrame
private JTextField CoA, CoB, CoC, Root1, Root2;
private JLabel CoAx, CoBx, CoCx, Root1x, Root2x;
private JButton calculateB, exitB;
private CalculateButtonHandler cbHandler;
private ExitButtonHandler ebHandler;

public lab6()
setTitle("Quadratic Fun"); //Title of the window
//Create the five labels
CoAx =
new JLabel("", SwingConstants.RIGHT);
CoBx =
new JLabel("", SwingConstants.RIGHT);
CoCx =
new JLabel("", SwingConstants.RIGHT);
Root1x =
new JLabel("", SwingConstants.RIGHT);
Root2x =
new JLabel("", SwingConstants.RIGHT);
//Create the five text fields
CoA = new JTextField(10);
CoB = new JTextField(10);
CoC = new JTextField(10);
Root1 = new JTextField(10);
Root2 = new JTextField(10);
//Create Calculate Button
calculateB = new JButton("Calculate");
cbHandler = new CalculateButtonHandler();
//Create Exit Button
exitB = new JButton("Exit");
ebHandler = new ExitButtonHandler();

Container pane = getContentPane();//Get the container
pane.setLayout(new GridLayout(4, 2));//Set the Layout
//Place the components in the pane


private class CalculateButtonHandler implements ActionListener//Creates a class on top of the interface
public void actionPerformed(ActionEvent e)
double a, b, c, R1, R2;

a = Double.parseDouble(CoA.getText());
b = Double.parseDouble(CoB.getText());
c = Double.parseDouble(CoC.getText());

Root1.setText("Root 1" + R1);
Root2.setText("Root 2" + R2);


private class ExitButtonHandler implements ActionListener//Creates a class on top of the interface
public void actionPerformed(ActionEvent e)
public static void main ( String[] args)
lab6 labwin = new lab6();



  • The instructor is looking for your ability to tack up a Java GUI.

    As such, it is difficult to help. The only action is when the "calculate" button is pressed. You need to create an event listener for this button, and make it a nested internal class of your main applet.
    The event listener queries the other fields, makes sure they contain valid numbers, and does the quardratic equation.
    The equation itself is so simple it doesn't justify a separate class, but give it a method of its own.

    Start by getting the "calculate" button on screen and printing a diagnostic to System.out when it is pressed.
    Then add the edit fields, and get the calcualte button to query them and print out their values.
    Then get it to print text out to a text field / label.

    Finally hook up the real quatratic equation solver and an error-messages field.

    Make plenty of use of the Java API documentation and don't be afraid to do a little exploratory programming to get something to work.

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!


In this Discussion