...

View Full Version : Java Troubles



mrprimo55
10-28-2004, 09:50 PM
I'm taking a Java class at school and almost every computer seems to heaving the same problem and we can't figure out what is wrong. We are using Codewarrior on XP machines. We wrote an applet and when we try to run it, everyone gets the same error saying that the main class needs to be declared as 'abstract'. Now the really weird thing about all this is that for some reason one person's applet worked, so I copied his .java file (which is the same as mine and everyone elses) onto a disk and put it in a new project on my computer and it worked fine. I've done this on every computer now and it works. I've gone through line by line and his program is EXACTLY the same as everyone elses. Does anyone know why this is happening and what the error means when it says the class needs to be declared as 'abstract'? Any help would be appreciated. Thanks.

JPM
10-29-2004, 07:47 AM
you should post the code/class

mrprimo55
10-29-2004, 11:11 PM
Ya I dont have class until Monday so I will get the code then and post it sometime Monday afternoon.

mrprimo55
11-01-2004, 09:34 PM
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
import java.text.DecimalFormat;

public class CheckBoxApplet extends Applet implements ItemListener
{
double dollars, answer;
int empCode;
Color darkRed = new Color(160,50,0);


Label promptLabel = new Label("Enter the sales amount (do not use commas or dollar signs):");
TextField salesField = new TextField(20);

Label codeLabel = new Label ("Select the appropriate commission code:");

CheckboxGroup codeGroup = new CheckboxGroup();
Checkbox telephoneBox = new Checkbox("Telephone Sales",false,codeGroup);
Checkbox inStoreBox = new Checkbox("In-Store Sales",false,codeGroup);
Checkbox outsideBox = new Checkbox("Outside Sales",false,codeGroup);
Checkbox hiddenBox = new Checkbox(" ",true,codeGroup);

Label outputLabel = new Label("Click an option button to calculate the sales commission.");

public void init()
{
setBackground(darkRed);
setForeground(Color.white);
add(promptLabel);
add(salesField);
salesField.requestFocus();
salesField.setForeground(Color.black);
add(codeLabel);
add(telephoneBox);
telephoneBox.addItemListener(this);
add(inStoreBox);
inStoreBox.addItemListener(this);
add(outsideBox);
outsideBox.addItemListener(this);
add(outputLabel);
}

public void itemStateChanged(ItemEvent choice)
{
try
{
dollars = getSales();
empCode = getCode();
answer = getComm(dollars,empCode);
output(answer,dollars);
}

catch (NumberFormatException e)
{
outputLabel.setText("You must enter a dollar amount greater then zero,");
hiddenBox.setState(true);
salesField.setText("");
salesField.requestFocus();

}
}

public double getSales() //getSalses method
{
double sales = Double.parseDouble(salesField.getText());

if (sales <=0) throw new NumberFormatException();

return sales;
}

public int getCode()
{
int code = 0;
if (telephoneBox.getState()) code = 1;
else
if (inStoreBox.getState()) code = 2;
else
if (outsideBox.getState()) code = 3;
return code;

}

public double getComm(double sales, int code)
{
double commission = 0.0;
switch(code)
{
case 1:
commission = .10 * sales;
break;

case 2:
commission = .14 * sales;
break;

case 3:
commission = .18 * sales;
break;

}
return commission;

}

public void output(double commission, double sales)
{
DecimalFormat twoDigits = new DecimalFormat("$#,000.00");
outputLabel.setText("Your commission on sales of "+twoDigits.format(sales)+" is "+twoDigits.format(commission));
}


public void paint(Graphics g)
{

}
}

mrprimo55
11-01-2004, 11:54 PM
The error I kept getting said something about needing to declare the CheckBoxApplet class as "abstract". I would post the entire error, but I can't get it anymore now that I've "fixed" the program. All I did was copy and paste the .java file into a new project and now it works. I'm not sure why that worked, but it did.

Antoniohawk
11-02-2004, 04:34 AM
Next time slap your code in the vbcode [code] tags so that it retains the formatting. :)

Roelf
11-02-2004, 08:26 AM
mostly (perhaps always) the reason for needing to declare a class as abstract, is because you don't implement all members of the interface. So it is not a true implementation of the interface, but you have to extend this class to get all interfacemembers implemented.

kcussemanliame
03-26-2005, 06:49 AM
I'm having the same exact problem. I work on applets at my school, none of the computers work when I try to use any Listeners, and no even my teacher knows what to do. Once, only once, we got it to work by changing the JDK Profile on an administrator account. Now we cant figure out what we did, why it worked, or why, no matter what we try, it still wont work. help, please. I need to figure this out to graduate. :(

-Peter

kcussemanliame
04-07-2005, 07:21 PM
forget it. i used his code to fix my own. i still dont know why my last code worked on one computer and not on any others, but my new code, and his code works fine. i'll just accept it now that i can make it work. thanks for posting, it helped me out. :thumbsup:



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum