Results 1 to 2 of 2
Thread: Quadratic GUI
 10302008, 03:42 AM #1Member
 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 dividebyzero error. If a is equal to zero, flag a warning to user (in a JOptionPane popup 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 nonreal 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(); calculateB.addActionListener(cbHandler); //Create Exit Button exitB = new JButton("Exit"); ebHandler = new ExitButtonHandler(); exitB.addActionListener(ebHandler); Container pane = getContentPane();//Get the container pane.setLayout(new GridLayout(4, 2));//Set the Layout //Place the components in the pane pane.add(CoA); pane.add(CoAx); pane.add(CoB); pane.add(CoBx); pane.add(CoC); pane.add(CoCx); pane.add(Root1); pane.add(Root1x); pane.add(Root2); pane.add(Root2x); pane.add(calculateB); pane.add(exitB); 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(); } }

Similar Threads

Help with quadratic equation in java
By paul in forum New To JavaReplies: 1Last Post: 07252007, 08:23 PM
Bookmarks