1. Member Join Date
Oct 2008
Posts
16
Rep Power
0

## 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

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

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

setSize(400,300);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}

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)
{
System.exit(0);
}
}
public static void main ( String[] args)
{
lab6 labwin = new lab6();
}

}```  Reply With Quote

2. ## 1) Create a non-GUI Quadratics class that has all the methods needed to calculate the equation in all ways needed.

2) Then when this class is bullet-proof (and only then) create a GUI class that uses the above class (don't inherit from the non-GUI class though -- rather use it through composition).  Reply With Quote

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•