![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Mar 2005
Posts: 4
Rep Power: 0
![]() |
Abstract???!!!
Hey guys
ok so heres the deal I made a program that calculates whether 2 CD's will fit on a tape. ok so i used the code from a similar program and modified it to work. (I was told to do it this way). ok now when i complile it tells me that my class should be declared as an abstract. What the hell is an abstract??? Also could you tell me if my code is correct with everything else? Thanks Andrea Heres my code: import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.text.DecimalFormat; public class fit implements ActionListener { private JTextField inputt1, inputt2, t; public void init() { Container c = getContentPane(); c.setLayout(new FlowLayout()); c.add(new JLabel("t1:")); inputt1 = new JTextField(5); c.add(inputt1); c.add(new JLabel("t2")); inputt2 = new JTextField(5); c.add(inputt2); JButton calc = new JButton("Calculate Best Fit"); calc.addActionListener(this); c.add(calc); c.add(BestFit); } public double findBestFit(int t1, int t2, int t) { if (t1+t2<t) System.out.println ("3"); else if (t1<t) System.out.println ("1"); else if (t2<t) System.out.println ("2"); else if (t1>t) System.out.println ("0"); else if (t2>t) System.out.println ("0"); } } |
|
|
|
|
|
#2 |
|
Programmer
|
An abstract class is a class that cannot be instantiated. You're problem is that you are implementing the ActionListener interface, but are not writing the function defined in this interface. To fix your problem, add:
public void actionPerformed(ActionEvent evnt) {
}Then insert code into that function to handle any actions that occur on the button (click). In case you don't know, an interface just defines a set of methods, but it does not implement these methods. By implementing an interface yourself (public class MyClass implements MyInterface), you must provide definitions for the methods declared in the interface. This is why you must provide the actionPerformed method when implementing ActionListener. You're class as it stands is abstract, because it does not know how to implement the ActionListener interface, because of the above. Are you new to Java programming? There seems to be general problems with your program as it is, even with the suggested changes. For example, you haven't actually created a window to store the controls you have. Hope this helps, kirkl_uk Last edited by kirkl_uk; May 25th, 2005 at 9:44 AM. |
|
|
|
|
|
#3 |
|
Newbie
Join Date: Mar 2005
Posts: 4
Rep Power: 0
![]() |
I am very new to java. I have tried looking for help online but couldnt find anything decent.
Is theis what my code should look like?? Also how would I go about making the window for my controls? CODE: import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.text.DecimalFormat; public void actionPerformed(ActionEvent event) { implements ActionListener } { private JTextField inputt1, inputt2, t; public void init() { Container c = getContentPane(); c.setLayout(new FlowLayout()); c.add(new JLabel("t1:")); inputt1 = new JTextField(5); c.add(inputt1); c.add(new JLabel("t2")); inputt2 = new JTextField(5); c.add(inputt2); JButton calc = new JButton("Calculate Best Fit"); calc.addActionListener(this); c.add(calc); c.add(BestFit); } public double findBestFit(int t1, int t2, int t) { if (t1+t2<t) System.out.println ("3"); else if (t1<t) System.out.println ("1"); else if (t2<t) System.out.println ("2"); else if (t1>t) System.out.println ("0"); else if (t2>t) System.out.println ("0"); } } |
|
|
|
|
|
#4 |
|
Programmer
|
This is how your code should look with the incorporated actionPerformed method:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.text.DecimalFormat;
public class fit implements ActionListener
{
private JTextField inputt1, inputt2, t;
public void init()
{
Container c = getContentPane();
c.setLayout(new FlowLayout());
c.add(new JLabel("t1:"));
inputt1 = new JTextField(5);
c.add(inputt1);
c.add(new JLabel("t2"));
inputt2 = new JTextField(5);
c.add(inputt2);
JButton calc = new JButton("Calculate Best Fit");
calc.addActionListener(this);
c.add(calc);
c.add(BestFit);
}
public double findBestFit(int t1, int t2, int t)
{
if (t1+t2<t)
System.out.println ("3");
else if (t1<t)
System.out.println ("1");
else if (t2<t)
System.out.println ("2");
else if (t1>t)
System.out.println ("0");
else if (t2>t)
System.out.println ("0");
}
public void actionPerformed(ActionEvent evnt)
{
}
}When using a line such as: public class MyClass implements MyListener ...you are agreeing to provide definitions of the methods defined by the interface (MyListener). Therefore MyClass itslef must have an actionPerformed method, which will be called whenever an action is performed (because you are inheriting from ActionListener). As for creating a window, you can extend, for example, the JFrame class. If you don't know what this means, I suggest you spend some time reading through tutorials on the Web, because inheritance is a part of Java you're likely to use a lot. However, if you need help with understanding anything, I'm sure we can help you here at PF. kirkl_uk Last edited by kirkl_uk; May 27th, 2005 at 10:09 AM. |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|