This discussion is archived
3 Replies Latest reply: Oct 16, 2012 9:20 AM by rukbat RSS

HELP [URGENT]

968683 Newbie
Currently Being Moderated
Hi, I really need someone to help me with this code it needs to be done by tonight and it wont work if some one would be willing to fix it for me it would be greatly appreciated
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!"); 
     }
          }
Edited by: 965680 on Oct 16, 2012 7:46 AM
  • 1. Re: HELP [URGENT]
    TPD-Opitz-Consulting-com Expert
    Currently Being Moderated
    Welcome to the forum.

    This is not urgent for anybody else than you.
    So if you want someone to help you we need a little mor information about in which way your code fails. What's the expected behavior and what does it instead?

    bye
    TPD
  • 2. Re: HELP [URGENT]
    doremifasollatido Journeyer
    Currently Being Moderated
    You seem to have taken the code verbatim from the following thread:
    Re: Java Calculator HELP!

    What was wrong with the advice given in that thread? At least you posted the code with code tags this time.
  • 3. Re: HELP [URGENT]
    rukbat Guru Moderator
    Currently Being Moderated
    Moderator Action and Comment:
    Since this is a duplicate of that earlier thread, this one is locked.

    If you indeed are a different person (else you just couldn't be bothered re-posting under that other forum account - so you created a new one for this post), get together with the other one and study together. It seems you both have the same classwork assignment to do.


    .
    ... and to reinforce what was said in the first reply here, there is no "urgent" in these forums. That is your issue and not anyone else's.


    Edit:
    A Google search, plugging the first couple of lines of the code into the search engine, finds numerous posts on this class assignment in various forums that go back 8+ years.

    Edited by: rukbat on Oct 16, 2012 12:18 PM

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points