Hey Guys,

This is my first program where I have used JLabel and JText. Im trying to create a program that will solve a simple quadratic equation. I have the program to the point where it will solve the equation, I just cant get it to display in the window. Any suggestion on how to get this to display?? Thanks!

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

public class QuadraticEquationWindow extends JFrame
  implements ActionListener
{
	JTextField inputa, inputb, inputc, displayroot1, displayroot2;

	public QuadraticEquationWindow ()
	{
		super("Quadratic Equation Solver");
		JLabel labela= new JLabel("A: ", SwingConstants.RIGHT);
		inputa= new JTextField(5);
		JLabel labelb= new JLabel("B: ", SwingConstants.RIGHT);
		inputb= new JTextField(5);
		JLabel labelc= new JLabel("C: ", SwingConstants.RIGHT);
		inputc= new JTextField(5);
		JLabel labelroot1= new JLabel("X=", SwingConstants.RIGHT);
		displayroot1= new JTextField(5);
		displayroot1.setEditable(false);
		JButton go= new JButton("Compute");
		go.addActionListener(this);

		Container a= getContentPane();
		a.setBackground(Color.white);
		JPanel p= new JPanel();
		p.setLayout(new GridLayout(5, 1, 5, 5));
		p.add(labela);
		p.add(inputa);
		p.add(labelb);
		p.add(inputb);
		p.add(labelc);
		p.add(inputc);
		p.add(labelroot1);
		p.add(displayroot1);
		a.add(p, BorderLayout.CENTER);
		a.add(go, BorderLayout.SOUTH);
	}

	public void actionPerformed(ActionEvent e)
	{
		int a= Integer.parseInt(inputa.getText());
		int b= Integer.parseInt(inputb.getText());
		int c= Integer.parseInt(inputc.getText());
		double root1= calculateQuadratic(a, b, c);
		double root2= calculateQuadratic(a, b, c);
    	DecimalFormat df = new DecimalFormat("00000000.00000000");
		displayroot1.setText(df.format(root1+" and "+root2));
	}

	private double calculateQuadratic(int a, int b, int c)
	{
		double b2, b3, root1, root2, h, h1, h2, h3, h4, h5, i1, i2;
		b2= (-b);
		b3= Math.pow(b,2);
		h= (b3-4*a*c);
		h1= (2*a);
		if(h>0)
		{
			h2= Math.pow(h, .5);
			h3= b2+h2;
			h4= b2-h2;
			root1= h3/h1;
			root2= h4/h1;
			return root1+root2;

		}
		if(h==0)
		{
			h2= 0;
			h3= h2+b2;
			root1= h3/h1;
			root2= b2/h1;
			return root1+root2;
		}
		else
		{
			h2= Math.abs(h);
			h3= Math.pow(h2, .5);
			h4= b2+h3;
			root1= h4/h1;
			h5= b2-	h3;
			i1= h5/h1;
			i2= b2/h1;
			root1= i1+i2;
			root2= i1-i2;
			return root1+root2;
		}
	}

	public static void main(String[] args)
	{
		QuadraticEquationWindow w= new QuadraticEquationWindow ();
		w.setBounds(300, 300, 300, 250);
		w.setDefaultCloseOperation(EXIT_ON_CLOSE);
		w.setVisible(true);
	}
}

The problem is on line 52

displayroot1.setText(df.format(root1+" and "+root2));

As you are trying to format a Sting which is not a decimal

Replace the code as

displayroot1.setText(df.format(root1)+" and "+df.format(root2));
Member Avatar for hfx642

Also...
On line #31, you are defining your Grid Panel as 5 x 1,
when you are trying to add 8 objects.
This will be an issue.
You may want to consider defining your Grid Panel as 4 x 2 instead.

Thanks for that! I got the program up and running but the only problem is that it doesn't return the right answer. It return root1+root2. How can I get it to display "root1 and root2" and if its imaginary "root1i and root2i"?

Member Avatar for hfx642

Lines; 69, 78, and 91, are ALL returning root1+root2.
Is THIS what you want to do?

No, Im trying to get it to print out root1 and root2 separately. And in the case the the roots are imaginary, I want them to have a "i" after it.

hi,

Did you solve your problem?? Because I have to do the same project.

Pavidaminie: then write the code. this is not a "come and copy of us" site.
neither is this a valid reason to re-open a dead thread.

I am sorry but I tried a lot

can't tell that by the posts you made so far. a better approach would be:
start your own thread, show us the code you have so far, tell us what it has to do or what it doesn't do, or what it does wrong, and what error messages you get.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.