Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old May 25th, 2005, 8:44 AM   #1
newdude
Newbie
 
Join Date: Mar 2005
Posts: 4
Rep Power: 0 newdude is on a distinguished road
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");
}
}
newdude is offline   Reply With Quote
Old May 25th, 2005, 9:25 AM   #2
kirkl_uk
Programmer
 
kirkl_uk's Avatar
 
Join Date: Apr 2005
Location: England
Posts: 86
Rep Power: 4 kirkl_uk is on a distinguished road
Send a message via MSN to kirkl_uk
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.
kirkl_uk is offline   Reply With Quote
Old May 27th, 2005, 7:58 AM   #3
newdude
Newbie
 
Join Date: Mar 2005
Posts: 4
Rep Power: 0 newdude is on a distinguished road
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");
}
}
newdude is offline   Reply With Quote
Old May 27th, 2005, 10:06 AM   #4
kirkl_uk
Programmer
 
kirkl_uk's Avatar
 
Join Date: Apr 2005
Location: England
Posts: 86
Rep Power: 4 kirkl_uk is on a distinguished road
Send a message via MSN to kirkl_uk
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.
kirkl_uk is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 2:03 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC