Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 9 of 9

Thread: Java Troubles

  1. #1
    New Coder
    Join Date
    Sep 2004
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Java Troubles

    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.

  • #2
    JPM
    JPM is offline
    Regular Coder
    Join Date
    Mar 2004
    Location
    Norway
    Posts
    204
    Thanks
    0
    Thanked 0 Times in 0 Posts
    you should post the code/class
    <JPM />

  • #3
    New Coder
    Join Date
    Sep 2004
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Ya I dont have class until Monday so I will get the code then and post it sometime Monday afternoon.

  • #4
    New Coder
    Join Date
    Sep 2004
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Code:
    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) 
    	{
    		
    	}
    }
    Last edited by Antoniohawk; 11-02-2004 at 04:33 AM.

  • #5
    New Coder
    Join Date
    Sep 2004
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.

  • #6
    Senior Coder
    Join Date
    Aug 2002
    Location
    Kansas City, Kansas
    Posts
    1,518
    Thanks
    0
    Thanked 2 Times in 2 Posts
    Next time slap your code in the vbcode [code] tags so that it retains the formatting.

  • #7
    Senior Coder
    Join Date
    Jun 2002
    Location
    Zwolle, The Netherlands
    Posts
    1,120
    Thanks
    2
    Thanked 31 Times in 31 Posts
    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.
    I am the luckiest man in the world

  • #8
    New to the CF scene
    Join Date
    Mar 2005
    Location
    California
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    I'm glad you figured it out; now how about the rest of us.

    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

  • #9
    New to the CF scene
    Join Date
    Mar 2005
    Location
    California
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    nevermind

    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.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •