Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Is this correct.Am I close? Help please

  1. #1
    Junior Member
    Join Date
    Jun 2011
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Is this correct.Am I close? Help please

    Current problem only includes addition function. Here is what i did..is this correct for my problem where I need to modify it to include buttons and calculation for multiplication,division and subtraction.?

    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
     
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.JTextField;
     
    public class BasicCalculator {
    private static final int FRAME_WIDTH = 150;
    private static final int FRAME_HEIGHT = 120;
     
    // Keeps track of the current operation (subtract, add, etc)
    private static final int NO_OPERATION = 0;
    private static final int ADDITION = 1;
    public static int operation = NO_OPERATION;
     
    public static JTextField textFieldDisplay;
    public static double Value1 = 0; // holds the value before the operation
     
    public static void main(String[] args) {
    // Set up the user interface
    JFrame frame = new JFrame();
    JPanel buttonPanel = new JPanel();
    frame.add(buttonPanel);
     
    // create two buttons, plus and equal and a text box for answers
    textFieldDisplay = new JTextField(10);
    buttonPanel.add(textFieldDisplay);
    JButton buttonPlus = new JButton(" + ");
    buttonPanel.add(buttonPlus);
    JButton buttonEqual = new JButton(" = ");
    buttonPanel.add(buttonEqual);
     
    frame.setSize(FRAME_WIDTH, FRAME_HEIGHT);
    frame.setTitle("Basic Calculator");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOS E);
    frame.setVisible(true);
     
     
    // called when the equal sign '=' is pressed
    class EqualSignListener implements ActionListener {
     
    public void actionPerformed(ActionEvent event)
    {
    double Value2 = Double.parseDouble(textFieldDisplay.getText());
    if (operation == ADDITION) {
    // plus sign pressed before the equal sign
    Value2 += Value1;
    }
    // Convert from a answer to a string
    Double answer = new Double(Value2);
    textFieldDisplay.setText( answer.toString() );
    // Reset the operation to show no current operation
    operation = NO_OPERATION;
    }
    }
     
    // called when a plus sign '+' is pressed
    class PlusSignListener implements ActionListener {
    public void actionPerformed(ActionEvent event)
    {
    Value1 = Double.parseDouble(textFieldDisplay.getText());
    operation = ADDITION;
    }
    }
     
    // Add the methods that will be called when these buttons are pressed
    ActionListener plusSignListener = new PlusSignListener();
    buttonPlus.addActionListener(plusSignListener);
     
    ActionListener equalSignListener = new EqualSignListener();
    buttonEqual.addActionListener(equalSignListener);
     
    }
    }
    // called when a subtraction sign '-' is pressed
    class SubtractionSignListener implements ActionListener {
    public void actionPerformed(ActionEvent event)
    {
    Value1 = Double.parseDouble(textFieldDisplay.getText());
    operation = Subtraction;
    }
    }
     
    // Add the methods that will be called when these buttons are pressed
    ActionListener subtractionSignListener = new SubtractionSignListener();
    buttonPlus.addActionListener(subtractionSignListen er);
     
    ActionListener equalSignListener = new EqualSignListener();
    buttonEqual.addActionListener(equalSignListener);
    }
    )
    // called when a division sign '/' is pressed
    class DivisionSignListener implements ActionListener {
    public void actionPerformed(ActionEvent event)
    {
    Value1 = Double.parseDouble(textFieldDisplay.getText());
    operation = DIVISION;
    }
    }
     
    // Add the methods that will be called when these buttons are pressed
    ActionListener divisionSignListener = new DivisionSignListener();
    buttonPlus.addActionListener(plusSignListener);
     
    ActionListener equalSignListener = new EqualSignListener();
    buttonEqual.addActionListener(equalSignListener);
    }
    ) 
    // called when a plus sign '*' is pressed
    class MulitplicationSignListener implements ActionListener {
    public void actionPerformed(ActionEvent event)
    {
    Value1 = Double.parseDouble(textFieldDisplay.getText());
    operation = Multiplication;
    }
    }
     
    // Add the methods that will be called when these buttons are pressed
    ActionListener multiplicationSignListener = new MultiplicationSignListener();
    buttonPlus.addActionListener(plusSignListener);
     
    ActionListener equalSignListener = new EqualSignListener();
    buttonEqual.addActionListener(equalSignListener);
     
    }
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Is this a duplicate of another thread?

    Is this thread the same as: http://www.javaprogrammingforums.com...ification.html

    Please only use one thread for a problem.

Similar Threads

  1. I still have errors. Can you PLEASE correct my code?!
    By sam30317 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: May 6th, 2011, 06:10 AM
  2. Trying to add a close button
    By coyboss in forum Java Theory & Questions
    Replies: 5
    Last Post: February 12th, 2011, 03:28 PM
  3. Is My answers correct??
    By Java.Coder() in forum What's Wrong With My Code?
    Replies: 6
    Last Post: December 28th, 2010, 06:22 AM
  4. Correct Coupling
    By poulenc in forum Java Theory & Questions
    Replies: 0
    Last Post: December 26th, 2010, 04:28 AM
  5. It's not printing out the correct percentage...
    By JBow94 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 15th, 2010, 04:07 PM