Java Abstract Error and also need to create a line graph

66 views Asked by At

I am currently taking an introductory Java course. Because I am only in an introductory course I am still very new to Java so make sure to keep that in mind.

Right now I am working on an assignment for school, and have run into a bit of a snag.

My assignment is to create a program that takes the values input by the user (bv or beginning value, ev1 or year 1 value, ev2 or year 2 value, so on so forth...), and then use the computeRate method that I made to calculate what the needed rate would be (this is for stuff like interest and what not).

I then need use all of the input values from the beginning value, to the final year value or year 5 to create a line graph like this one: Drawing a simple line graph in Java , except in my graph I would have to be able to take in all of the user input values from before, and then use those values and the rate to plot point, and connect them to make a line graph just like the one in that example.

Here is the code that I have so far:

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class Rate_Graph extends JApplet implements ActionListener
{
JLabel bv, ev1, ev2, ev3, ev4, ev5;
JTextField bv1, ev_1, ev_2, ev_3, ev_4, ev_5;
JButton go, add1, add2, add3, add4, add5;
public void init()
{
    setLayout(new FlowLayout());

    bv = new Jlabel("Enter beginning value:");
    bv1 = new JTextField(5);
    go = new JButton("Add!");
    go.addActionListener(this);

    ev1 = new Jlabel("Enter year 1 value:");
    ev_1 = new JTextField(5);
    add1 = new JButton("Add!");

    ev2 = new Jlabel("Enter year 2 value:");
    ev_2 = new JTextField(5);
    add2 = new JButton("Add!");

    ev3 = new Jlabel("Enter year 3 value:");
    ev_3 = new JTextField(5);
    add3 = new JButton("Add!");

    ev4 = new Jlabel("Enter year 4 value:");
    ev_4 = new JTextField(5);
    add4 = new JButton("Add!");

    ev5 = new Jlabel("Enter b value:");
    ev_5 = new JTextField(5);
    add5 = new JButton("Add!");
    add5.addActionListener(this);

    add(bv); add(ev1); add(ev2); add(ev3); add(ev4); add(ev5);
    add(bv1); add(ev_1); add(ev_2); add(ev_3); add(ev_4); add(ev_5);
    add(go); add(add1); add(add2); add(add3); add(add4); add(add5);
}
public double computeRate()
{
    double rate = (Math.pow(ev_5 / bv1, 1.0 / 5.0) - 1);
    System.out.println("The rate is:" + rate);
}
}

My first problem already arises on the first line under the import statements, the error code says that:

Rate_Graph is not abstract and does not override abstract method actionPerformed(java.awt.ActionEvent) in java.awt.event.ActionListener.

I have tried some of the more simple fixes for this problem (this included declaring the class abstract but that developed other problems with in the code so that is not the correct fix of course), but none of them seem to work.

My second problem is figuring out how to adapt that line graph code to be able to take in the user's input for all the values, and create a line graph. Any help would be appreciated, and again remember I am very new to Java still.

1

There are 1 answers

1
Scary Wombat On BEST ANSWER

When you type .... implements ActionListener you are telling the compiler that you are actually going to implement a interface. That involves implementing any un-implemented method.

In this case you need to implement the actionPerformed(java.awt.ActionEvent) method.

Have a look at this link to see about how to write this method.

If your class was abstract, then as abstract classes can not be instantiated, the compiler will assume that the implementation of this method would be in a class which sub-classed your class.