please help

I'm trying to make a quadratic solver with a GUI interface, does anybody have this or can anybody tell me what I need to fix with what I've done.

C:\Documents and Settings\Administrator\My Documents\Quadratic12.java:9: Quadratic12 is not abstract and does not override abstract method actionPerformed(java.awt.event.ActionEvent) in java.awt.event.ActionListener

public class Quadratic12 extends JFrame

^

1 error

Tool completed with exit code 1

import java.io.*;

import java.util.StringTokenizer;

import javax.swing.*;

import java.util.*;

import java.awt.event.*;

import java.awt.*;

import java.awt.event.ActionEvent.*;

public class Quadratic12 extends JFrame

implements ActionListener {

public JTextField jtfNumA, jtfNumB, jtfNumC, jtfResult1, jtfResult2;

public JButton jbtSolve, jbtClose, jbtClear;

public double a;

public double b;

public double c;

public double[] roots;

public static void main(String [] args){

Quadratic12 quad = new Quadratic12();

quad.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

quad.setSize(600,130);

quad.setTitle("Quadratic Solution Program");

quad.setVisible(true);

}

public Quadratic12(double a, double b, double c){

this.a=a;

this.b=b;

this.c=c;

}

public Quadratic12() {

JPanel p1 = new JPanel();

p1.setLayout( new FlowLayout());

p1.add(new JLabel("Quadratic Equation : "));

p1.add(jtfNumA = new JTextField(10));

p1.add(new JLabel(" X^2 + "));

p1.add(jtfNumB = new JTextField(10));

p1.add(new JLabel(" X + "));

p1.add(jtfNumC = new JTextField(10));

JPanel p2 = new JPanel();

p2.setLayout( new FlowLayout());

p2.add(new JLabel("X = : "));

p2.add(jtfResult1 = new JTextField(10));

p2.add(new JLabel("or"));

p2.add(jtfResult2 = new JTextField(10));

jtfResult1.setEditable(false);

jtfResult2.setEditable(false);

JPanel p3 = new JPanel();

p3.setLayout(new FlowLayout());

p3.add(jbtSolve = new JButton("Solve Quadratic for X"));

p3.add(jbtClose = new JButton("Exit Program"));

p3.add(jbtClear = new JButton("Clear"));

getContentPane().setLayout( new BorderLayout());

getContentPane().add(p1, BorderLayout.NORTH);

getContentPane().add(p2, BorderLayout.CENTER);

getContentPane().add(p3, BorderLayout.SOUTH);

jbtSolve.addActionListener(this);

jbtClose.addActionListener(this);

jbtClear.addActionListener(this);

}

public double[] solve(){

if (a==0&&b==0&&c==0)

roots=null;

else if (a==0&&b==0)

roots=new double[0];

else if (a==0)

roots=new double[]{-c/b};

else{

double d= b*b-4*a*c;

if (d<0)

roots=new double[0];

else if (d==0)

roots=new double[]{ (-b + Math.sqrt(d))/(2*a)};

else

roots=new double[]{

(-b + Math.sqrt(d))/(2*a),

(-b - Math.sqrt(d))/(2*a)

};

}

return roots;

}

{

try{

BufferedReader reader=new BufferedReader(new

InputStreamReader(System.in));

while(true){

System.out.println("1. Calculate quadratic");

System.out.println("2. End");

System.out.print("Input option: ");

String line=reader.readLine();

int option;

try{

option=Integer.parseInt(line);

}

catch(NumberFormatException e){

System.out.println("Invalid entry. Try again");

continue;

}

if (option==2) System.exit(0);

if (option!=1){

System.out.println("Invalid option. Try once more");

continue;

}

System.out.println("Input a, b, and c separated by spaces");

line=reader.readLine();

StringTokenizer tokenizer=new StringTokenizer(line);

if (tokenizer.countTokens()!=3){

System.out.println("Invalid coefficients: expected 3 coefficients");

continue;

}

int a,b,c;

try{

a=Integer.parseInt(tokenizer.nextToken());

b=Integer.parseInt(tokenizer.nextToken());

c=Integer.parseInt(tokenizer.nextToken());

}

catch(NumberFormatException e){

System.out.println("coeffecients must be integers");

continue;

}

Quadratic quadratic=new Quadratic(a,b,c);

System.out.println("Solving the equation: "+a+"*x^2 + "+b+"*x +"+c+" = 0");

double[]roots=quadratic.solve();

if (roots==null)

System.out.println("All x are solutions");

else if (roots.length==0)

System.out.println("No solutions");

else if (roots.length==1)

System.out.println("Solution is: x= "+roots[0]);

else if (roots.length==2)

System.out.println("Solutions are: X= "+roots[0]+" or X="+roots[1]);

}

}

catch(Exception e){

System.out.println("Unexpected error: "+e);

System.exit(1);

}

}

}

[5031 byte] By [an23dya] at [2007-11-27 11:10:13]
# 1

You implemented ActionListener (according to the class definition line) but did not implement the method "actionPerformed". Define an actionPerformed method. If you do not know what the actionPerformed method is suppossed to do, or how it works, read through the Swing tutorials.

masijade.a at 2007-7-29 13:40:19 > top of Java-index,Java Essentials,New To Java...
# 2

What in the world are you trying to do over here? create a separate class for Quadrtaic12 with all the getter and setter and equation solving methods and then create a GUI terminal class. Why are you using System.out.println()? do you know how to get input and display output in swing? if not go read the tutorial on java forums.

lrngjavaa at 2007-7-29 13:40:19 > top of Java-index,Java Essentials,New To Java...
# 3

You add an ActionListener to your three buttons:

jbtSolve.addActionListener(this);

jbtClose.addActionListener(this);

jbtClear.addActionListener(this);

but Quadratic12 is not an Action Listener, so what's is supposed to happen when you click the buttons?

You need to have Quadratic12 implement ActionListener:

public class Quadratic12 extends JFrame implements Action Listener {

// your code here

public void actionPerformed(ActionEvent event) {

// code called when you click on buttons

}

// your other code here

}

Next time, please use a meaningful subject line, and indent your code with code tags (the "code" button in your reply window).

P.S. I did not see the other replies to the OP's message.

java_knighta at 2007-7-29 13:40:19 > top of Java-index,Java Essentials,New To Java...
# 4

> You need to have Quadratic12 implement

> ActionListener:

>

He does, but he uses a line break so that appears on the second line. ;-)

Which I assume you have noticed yourself by now. ;-)

masijade.a at 2007-7-29 13:40:19 > top of Java-index,Java Essentials,New To Java...
# 5

import java.awt.BorderLayout;

import java.awt.Container;

import java.awt.FlowLayout;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import javax.swing.JButton;

import javax.swing.JFrame;

import javax.swing.JLabel;

import javax.swing.JPanel;

import javax.swing.JTextField;

public class QuadraticGui extends JFrame implements ActionListener{

private JButton calculate = new JButton("Calculate");

private JTextField a1 = new JTextField("10"),

b1 = new JTextField("10"),

c1 = new JTextField("10");

private JPanel textPanel, resultLabel;

private Quadratic quadratic;

private JLabel result1 = new JLabel("result1"),

result2 = new JLabel("result2");

private double discr, ans1, ans2;

public static void main(String[] args) {

new QuadraticGui().setVisible(true);

}

public QuadraticGui() {

initComponents();

}

private void initComponents() {

Container contentPane = getContentPane();

setSize(350,200);

setTitle("Quadratic Calculator");

calculate.addActionListener(this);

a1.setText("1.0");

b1.setText("4.0");

c1.setText("2.0");

textPanel = new JPanel(new FlowLayout());

resultLabel = new JPanel(new FlowLayout());

textPanel.add(a1);

textPanel.add(b1);

textPanel.add(c1);

textPanel.add(calculate);

resultLabel.add(result1);

resultLabel.add(result2);

contentPane.add(textPanel, BorderLayout.NORTH);

contentPane.add(resultLabel, BorderLayout.CENTER);

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

}

public void actionPerformed(ActionEvent e) {

double a = Double.parseDouble(a1.getText());

double b = Double.parseDouble(b1.getText());

double c = Double.parseDouble(c1.getText());

discr = Math.sqrt((b * b) - (4 * a * c));

if(discr > 0) {

ans1 = (-b + discr)/2 * a;

ans2 = (-b - discr)/2 * a;

}

result1.setText("Answer 1: " + ans1);

result2.setText("Answer 2: " + ans2);

}

}

lrngjavaa at 2007-7-29 13:40:19 > top of Java-index,Java Essentials,New To Java...
# 6

> He does, but he uses a line break so that appears on

> the second line. ;-)

Gosh, I didn't notice it. My apologies.

The code I suggested in my previous post is correct anyway -- as Masijade correctly said, you need to implement the actionPerformed(ActionEvent event) method.

java_knighta at 2007-7-29 13:40:19 > top of Java-index,Java Essentials,New To Java...
# 7

Edit: oops, mistake: I thought this post had no replies yet...

; )

prometheuzza at 2007-7-29 13:40:19 > top of Java-index,Java Essentials,New To Java...