Results 1 to 13 of 13
  1. #1
    parkerp1000 is offline Member
    Join Date
    Oct 2012
    Posts
    6
    Rep Power
    0

    Default Calculator Help URGENT!

    Hi i'm working on a project for school and needs to be finished by Wednesday it docent give a result when u push the add subtract divide or mulitiply button. you operate this by putting the number in and then pushing the function.please help

    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*; 
    public class Calculator extends JFrame  {
    
    	JButton add ,subtract, multiply, divide; 
    	JTextField num1 , num2 ; 
    	JLabel result , enter1 , enter2 ; 
    	public Calculator () {
    		setLayout (new GridBagLayout());
    		GridBagConstraints c = new GridBagConstraints ();
    		
    		enter1 = new JLabel ("lst: ");
    		c.fill = GridBagConstraints.HORIZONTAL;
    		c.gridx = 0; 
    		c.gridy = 0;
    		add(enter1,c);
    		
    		num1 = new JTextField(10); 
    		c.fill = GridBagConstraints.HORIZONTAL;
    		c.gridx = 1;
    		c.gridy = 0; 
    		c.gridwidth = 3;
    		add(num1 , c ) ;
    		
    		enter2 = new JLabel ("2nd: ") ;
    		c.fill = GridBagConstraints.HORIZONTAL ; 
    		c.gridx = 0;
    		c.gridy = 1;
    		c.gridwidth = 1;
    		add (enter2 , c ) ;
    		
    		num2 = new  JTextField(10); 
    		c.fill = GridBagConstraints.HORIZONTAL ; 
    		c.gridx = 1;
    		c.gridy = 1; 
    		c.gridwidth = 3; 
    		add (num2 , c ) ; 
    		
    		add = new JButton ("+");
    		c.fill = GridBagConstraints.HORIZONTAL ; 
    		c.gridx = 0; 
    		c.gridy = 2;
    		c.gridwidth = 1;
    		add (add , c ) ; 
    		
    		subtract = new JButton ("-") ;
    		c.fill = GridBagConstraints.HORIZONTAL ;
    		c.gridx = 1; 
    		c.gridx = 2;
    		add (subtract , c ) ; 
    		
    		multiply = new JButton ("*") ; 
    		c.fill = GridBagConstraints.HORIZONTAL ;
    		c.gridx = 2;
    		c.gridy = 2;
    		add (multiply , c );
    		
    		divide = new JButton ("/"); 
    		c.fill = GridBagConstraints.HORIZONTAL ; 
    		c.gridx = 3;
    		c.gridy = 2;
    		add (divide , c); 
    		
    		result = new JLabel(""); 
    		c.fill = GridBagConstraints.HORIZONTAL ;
    		c.gridx = 0;
    		c.gridy = 4;
    		c.gridwidth = 4;
    		
    		
    		event a = new event () ; 
    		add.addActionListener(a) ; 
    		subtract.addActionListener(a) ; 
    		multiply.addActionListener(a) ; 
    		divide.addActionListener(a) ; 
    		 
    	
    	}
    	public class event implements ActionListener {
    		public void actionPreformed (ActionEvent a) {
    			double number1 , number2 ; 
    			
    			try {
    				number1 = Double.parseDouble(num1.getText()) ; 
    			} catch (NumberFormatException e) {
    				result.setText("Illegal Data");
    				result.setForeground(Color.RED) ; 
    				return; 
    				
    				}
    			try {
    			    number2 = Double.parseDouble(num2.getText()) ; 
    			}catch (NumberFormatException e) {
    			    result.setText("illegal data");
    			    result.setForeground(Color.RED);
    			    return; 
    			}
    					String op = a.getActionCommand();
    					
    					if (op.equals("+")) {
    						double sum = number1 + number2 ;
    						result.setText(number1 + "+" + number2 + "=" + sum ) ;
    						result.setForeground(Color.RED) ; 
    					} else if (op.equals("-")) {
    						double diff = number1 - number2 ; 
    						result.setText (number1 + "-" + number2 + "=" + diff) ;	
    						result.setForeground(Color.RED);
    					
    					} else if (op.equals("*")) { 
    						double factor = number1 * number2 ; 
    						result.setText (number1 + "*" + number2 + "=" + factor) ;	
    						result.setForeground(Color.RED);
    					
    						}else if (op.equals("/")) { 
    							if (number2 == 0) {
    								result.setText("Cannot Divide by 0") ; 
    								result.setForeground(Color.RED);
    							} else {
    								double quotient = number1 / number2 ; 
    								result.setText(number1 + "/" + number2 + "=" + quotient);
    								result.setForeground(Color.RED); 
    							}
    						}
    							
    				}
    			}
    		public static void main (String args []) {
    			Calculator gui = new Calculator (); 
    			gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    			gui.setVisible(true); 
    			gui.setSize(250,175);
    			gui.setTitle("Event Night Product!"); 
    	}
    		}
    Last edited by parkerp1000; 10-08-2012 at 06:32 PM.

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,945
    Rep Power
    19

    Default Re: Calculator Help URGENT!

    Please use [code] tags [/code] when posting code.
    Few people will bother reading unformatted code.

    For your actual problem, stick some debugging code (eg System.out.println("some meaningful comment")) in there to see what's happening.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    parkerp1000 is offline Member
    Join Date
    Oct 2012
    Posts
    6
    Rep Power
    0

    Default Re: Calculator Help URGENT!

    I get this error when I push the funtion.

    Exception in thread "AWT-EventQueue-0" java.lang.Error: Unresolved compilation problem:
    The type Calculator.event must implement the inherited abstract method ActionListener.actionPerformed(ActionEvent)

    at Calculator$event.actionPerformed(Calculator.java:8 1)
    at javax.swing.AbstractButton.fireActionPerformed(Abs tractButton.java:2028)
    at javax.swing.AbstractButton$Handler.actionPerformed (AbstractButton.java:2351)
    at javax.swing.DefaultButtonModel.fireActionPerformed (DefaultButtonModel.java:387)
    at javax.swing.DefaultButtonModel.setPressed(DefaultB uttonModel.java:242)
    at javax.swing.plaf.basic.BasicButtonListener.mouseRe leased(BasicButtonListener.java:236)
    at java.awt.Component.processMouseEvent(Component.jav a:6382)
    at javax.swing.JComponent.processMouseEvent(JComponen t.java:3275)
    at java.awt.Component.processEvent(Component.java:614 7)
    at java.awt.Container.processEvent(Container.java:208 3)
    at java.awt.Component.dispatchEventImpl(Component.jav a:4744)
    at java.awt.Container.dispatchEventImpl(Container.jav a:2141)
    at java.awt.Component.dispatchEvent(Component.java:45 72)
    at java.awt.LightweightDispatcher.retargetMouseEvent( Container.java:4619)
    at java.awt.LightweightDispatcher.processMouseEvent(C ontainer.java:4280)
    at java.awt.LightweightDispatcher.dispatchEvent(Conta iner.java:4210)
    at java.awt.Container.dispatchEventImpl(Container.jav a:2127)
    at java.awt.Window.dispatchEventImpl(Window.java:2489 )
    at java.awt.Component.dispatchEvent(Component.java:45 72)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.j ava:704)
    at java.awt.EventQueue.access$400(EventQueue.java:82)
    at java.awt.EventQueue$2.run(EventQueue.java:663)
    at java.awt.EventQueue$2.run(EventQueue.java:661)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.AccessControlContext$1.doIntersectio nPrivilege(AccessControlContext.java:87)
    at java.security.AccessControlContext$1.doIntersectio nPrivilege(AccessControlContext.java:98)
    at java.awt.EventQueue$3.run(EventQueue.java:677)
    at java.awt.EventQueue$3.run(EventQueue.java:675)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.AccessControlContext$1.doIntersectio nPrivilege(AccessControlContext.java:87)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java: 674)
    at java.awt.EventDispatchThread.pumpOneEventForFilter s(EventDispatchThread.java:296)
    at java.awt.EventDispatchThread.pumpEventsForFilter(E ventDispatchThread.java:211)
    at java.awt.EventDispatchThread.pumpEventsForHierarch y(EventDispatchThread.java:201)
    at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:196)
    at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:188)
    at java.awt.EventDispatchThread.run(EventDispatchThre ad.java:122)

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Calculator Help URGENT!

    Quote Originally Posted by parkerp1000 View Post
    I get this error when I push the funtion.

    Exception in thread "AWT-EventQueue-0" java.lang.Error: Unresolved compilation problem:
    The type Calculator.event must implement the inherited abstract method ActionListener.actionPerformed(ActionEvent)

    at Calculator$event.actionPerformed(Calculator.java:8 1)
    This means that you're trying to run code that won't compile, something that you should avoid doing. Try compiling the code first and fixing the errors the compiler tells you about. Your current error seems to be due to your having a class that implements the ActionListener interface but that does not have all the methods required of this interface, namely a valid actionPerformed method. The fix is to give the class this method.

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Calculator Help URGENT!

    Edit:

    Aha!!!
    Java Code:
      public void actionPreformed (ActionEvent a) {
    Note that actionPreformed != actionPerformed. Look carefully.

    Consider using the @Override annotation before any and all overrided methods.

  6. #6
    parkerp1000 is offline Member
    Join Date
    Oct 2012
    Posts
    6
    Rep Power
    0

    Default Re: Calculator Help URGENT!

    Quote Originally Posted by Fubarable View Post
    This means that you're trying to run code that won't compile, something that you should avoid doing. Try compiling the code first and fixing the errors the compiler tells you about. Your current error seems to be due to your having a class that implements the ActionListener interface but that does not have all the methods required of this interface, namely a valid actionPerformed method. The fix is to give the class this method.
    Java Code:
    public class event implements ActionListener {
    		public void actionPreformed (ActionEvent a) {
    This is the code that you are saying were the actionPreformed (ActionEvent a) is not implemented were would I implement this? and how.

  7. #7
    parkerp1000 is offline Member
    Join Date
    Oct 2012
    Posts
    6
    Rep Power
    0

    Default Re: Calculator Help URGENT!

    Calculator Help URGENT!-screen-shot-2012-10-09-8.08.30-am.png
    I tried @overide before but it gives me many more errors. the above photo show what eclipse is showing me that I need to do.

  8. #8
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,945
    Rep Power
    19

    Default Re: Calculator Help URGENT!

    Quote Originally Posted by Fubarable View Post
    Note that actionPreformed != actionPerformed. Look carefully.
    ...to quote Fubarable.

    Also:
    Quote Originally Posted by Fubarable View Post
    Consider using the @Override annotation before any and all overrided methods.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  9. #9
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,945
    Rep Power
    19

    Default Re: Calculator Help URGENT!

    Quote Originally Posted by parkerp1000 View Post
    I tried @overide before but it gives me many more errors. the above photo show what eclipse is showing me that I need to do.
    Well, it would first off help spelling it correctly.
    And then secondly heed the errors!
    It's telling you that there is no actionPreformed method to override!
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  10. #10
    parkerp1000 is offline Member
    Join Date
    Oct 2012
    Posts
    6
    Rep Power
    0

    Default Re: Calculator Help URGENT!

    I tried useing the @Overide before and it didn't work. if you can show me exactly were I need to add this that would be of great help and when you say
    Note that
    Java Code:
     actionPreformed != actionPerformed
    . Look carefully.
    I'm not sure what you want me to do here either I tried to put the != there but im not sure what i need to do here.

  11. #11
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,945
    Rep Power
    19

    Default Re: Calculator Help URGENT!

    Java Code:
    @Override
    public void someMethod() {
    }
    If someMethod is not in a parent of the class this has been written in then the compiler will complain, as it is an error.

    As for what I (and Fubarable) wrote, your code has:
    Java Code:
    public void actionPreformed (ActionEvent a)
    Look at the spelling.
    Compare that spelling to the API for ActionListener.

    This is the reason the @Override annotation exists.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  12. #12
    parkerp1000 is offline Member
    Join Date
    Oct 2012
    Posts
    6
    Rep Power
    0

    Default Re: Calculator Help URGENT!

    it still dosent work i have tried every thing that you are saying

  13. #13
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,945
    Rep Power
    19

    Default Re: Calculator Help URGENT!

    Show me then.
    Show the code as it now stands, with the @Override annotation and the spelling of the method corrected.
    And paste the errors, highlighting the lines they occur on.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. RMD Calculator
    By java software in forum Java Software
    Replies: 0
    Last Post: 10-08-2011, 05:42 PM
  2. Help in a calculator
    By Ayannie in forum New To Java
    Replies: 6
    Last Post: 01-04-2011, 08:21 PM
  3. [URGENT]SHA Encryption System...need urgent helps
    By java_idiot in forum New To Java
    Replies: 6
    Last Post: 05-02-2010, 10:04 AM
  4. help with calculator
    By kalibballer in forum New To Java
    Replies: 8
    Last Post: 04-01-2009, 12:57 PM
  5. Calculator help.
    By madkidd02 in forum New To Java
    Replies: 2
    Last Post: 10-25-2008, 07:42 AM

Posting Permissions

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