Link to home
Start Free TrialLog in
Avatar of SAMANTHA1066
SAMANTHA1066

asked on

Source code for a simple java programming calculator.

Note I am completely new to java. I need examples of source code for a simple calculator applet, yes it is an homework assigmnent.  I have set the layout for the button of the calculator but I do not know how to make the work.  For this assignment have to us the ActionListener method with the code provided:
//keep as original
import java.awt.*;
import java.applet.*;
import java.awt.event.*;

public class Calculator extends Applet implements ActionListener
{
      String myBuffer;
      String firstBuffer;
      String secondBuffer;
   
   String operation;
   
   int result;
   String passtoresult;
   
      Panel row1 = new Panel();
      Panel row2 = new Panel();
      Panel row3 = new Panel();
      
      Label titleLabel = new Label("Calculator", Label.CENTER);
      TextField resultField = new TextField(15);
      
      Button one = new Button ("1");
      Button zero = new Button ("0");
      Button two = new Button ("2");
      Button three = new Button ("3");
      Button four = new Button ("4");
      Button five = new Button ("5");
      Button six = new Button ("6");
      Button seven = new Button ("7");
      Button eight = new Button ("8");
      Button nine = new Button ("9");
      Button add = new Button ("add");
      Button subtract = new Button ("subtract");
      Button multiply = new Button ("multiply");
      Button divide = new Button ("divide");
      Button equals = new Button ("equals");
      
      public void init()
      {
            myBuffer="";
            firstBuffer="";
            secondBuffer="";
            
            one.addActionListener(this);
            zero.addActionListener(this);
            two.addActionListener(this);
            three.addActionListener(this);
            four.addActionListener(this);
            five.addActionListener(this);
            six.addActionListener(this);
            seven.addActionListener(this);
            eight.addActionListener(this);
            nine.addActionListener(this);
            add.addActionListener(this);
            subtract.addActionListener(this);
            multiply.addActionListener(this);
            divide.addActionListener(this);
            equals.addActionListener(this);
            
            setBackground(Color.white);
            
            GridLayout line1 = new GridLayout(2,1,100,10);
            row1.setLayout(line1);
            row1.add(titleLabel);row1.add(resultField);
            add(row1);
            
            GridLayout line2 = new GridLayout(3,4,2,2);
            row2.setLayout(line2);
            row2.add(one);row2.add(two);
            row2.add(three);row2.add(four);row2.add(five);
            row2.add(six);row2.add(seven);row2.add(eight);row2.add(nine);
      row2.add(zero);
            add(row2);      
            
            GridLayout line3 = new GridLayout(3,2,2,2);
            row3.setLayout(line3);
            row3.add(add);
            row3.add(subtract);
            row3.add(multiply);
            row3.add(divide);
            row3.add(equals);
            add(row3);
            
      }

      
      public void actionPerformed(ActionEvent event)
      {
            //if (event.getSource() == zero)
            //......add code here otherwise your compiler will complain
                  
                        
      }
}      
please help
ASKER CERTIFIED SOLUTION
Avatar of mmuruganandam
mmuruganandam
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
If that link doesn't work, then copy paste the url and enter to get the page.

there is some problem with the geocities.
Avatar of zzynx
1) As long a digits are pressed: display them in the result
2) When + or - is pressed, store the entered value before the + or - in an int/double,
    store the operator (+ or -)
3) When they press a digit again show it in the result field
4) As long a digits are pressed: display them in the result
5) When they press =, store the entered value before the = in another int/double
    Perform: variable1 operator variable2
6) Show the result in the result field
Here is another applet calculator with source code.

http://www.idoi.com/Java/Calculator.html


Regards,
Muruga
Avatar of qwert1515
qwert1515

/*Qwert1515
 *Caculator.java
 *Creates a graphical caculator
 *5/7/04
 */

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
//Caculator Class
public class Calculator extends JFrame implements ActionListener
{
    public static final int WIDTH = 150;
    public static final int HEIGHT = 320;
      private JTextField inputOutputField;
      private String action = "blank";
      private double value1 = 0;
    private double value2 = 0;
    private double memory1 =0.0;
    private double memory2 =0.0;
//Creates a caculator Object and sets it visible
    public static void main(String[] args)
    {
        Calculator calc = new Calculator();
        calc.setVisible(true);
    }

    public Calculator()
    {
        setSize(WIDTH, HEIGHT);

        addWindowListener(new WindowDestroyer());
        setTitle("Caculator");
        Container contentPane = getContentPane();
        contentPane.setBackground(Color.lightGray );
        contentPane.setLayout(new FlowLayout());
        JPanel textPanel = new JPanel();
        textPanel.setBackground(Color.blue);
        textPanel.setLayout(new FlowLayout());
        inputOutputField = new JTextField("", 12);
       inputOutputField.setBackground(Color.white);
       textPanel.add(inputOutputField);
       contentPane.add(textPanel, BorderLayout.CENTER);
   //Adds buttons to the caculators face
        JButton buttonOne = new JButton("1");
        buttonOne.addActionListener(this);
        contentPane.add(buttonOne);
       
        JButton buttonTwo = new JButton("2");
        buttonTwo.addActionListener(this);
       contentPane.add(buttonTwo);
     
       
        JButton buttonThree = new JButton("3");
        buttonThree.addActionListener(this);
        contentPane.add(buttonThree);
       
        JButton buttonFour = new JButton("4");
        buttonFour.addActionListener(this);
        contentPane.add(buttonFour);
       
        JButton buttonFive = new JButton("5");
        buttonFive.addActionListener(this);
        contentPane.add(buttonFive);
       
        JButton buttonSix = new JButton("6");
        buttonSix.addActionListener(this);
        contentPane.add(buttonSix);
       
             JButton buttonSeven = new JButton("7");
        buttonSeven.addActionListener(this);
        contentPane.add(buttonSeven);
       
        JButton buttonEight = new JButton("8");
        buttonEight.addActionListener(this);
        contentPane.add(buttonEight);
       
        JButton buttonNine = new JButton("9");
        buttonNine.addActionListener(this);
        contentPane.add(buttonNine);
       
        JButton buttonZero = new JButton("0");
        buttonZero.addActionListener(this);
        contentPane.add(buttonZero);
       
        JButton buttonDec = new JButton(".");
        buttonDec.addActionListener(this);
        contentPane.add(buttonDec);
       
        JButton buttonClear = new JButton("C");
        buttonClear.addActionListener(this);
        contentPane.add(buttonClear);
       
        JButton buttonAdd = new JButton("+");
        buttonAdd.addActionListener(this);
        contentPane.add(buttonAdd);
       
        JButton buttonSub = new JButton("-");
        buttonSub.addActionListener(this);
        contentPane.add(buttonSub);
       
        JButton buttonMult = new JButton("*");
        buttonMult.addActionListener(this);
        contentPane.add(buttonMult);
       
        JButton buttonDiv = new JButton("/");
        buttonDiv.addActionListener(this);
        contentPane.add(buttonDiv);
       
        JButton buttonEqu = new JButton("=");
        buttonEqu.addActionListener(this);
        contentPane.add(buttonEqu);
       
        JButton memoneplusButton = new JButton("M1+");
             memoneplusButton.addActionListener(this);
        contentPane.add(memoneplusButton);
       
         JButton memtwoplusButton = new JButton("M2+");
         memtwoplusButton.addActionListener(this);
        contentPane.add(memtwoplusButton);
       
        JButton mr1Button = new JButton("MR1");
        mr1Button.addActionListener(this);
        contentPane.add(mr1Button);
       
        JButton mr2Button = new JButton("MR2");
        mr2Button.addActionListener(this);
        contentPane.add(mr2Button);
       
       
       
    }
//Preforms the action when each button is pressed
    public void actionPerformed(ActionEvent e)
    {
       String currentText = inputOutputField.getText();

       Container contentPane = getContentPane();

            if(e.getActionCommand().equals("="))
            {
                  value2 = Double.parseDouble(currentText);
                  inputOutputField.setText("");      
                  
                  if(action.equals("add"))
                  {
                        action = "blank";
                        inputOutputField.setText(value1 + value2 + "");
                  }
                  else if(action.equals("sub"))
                  {
                        action = "blank";
                        inputOutputField.setText(value1 - value2 + "");
                  }
                  else if(action.equals("mult"))
                  {
                        action = "blank";
                        inputOutputField.setText(value1 * value2 + "");
                  }
                  else if(action.equals("div"))
                  {
                        action = "blank";
                        inputOutputField.setText(value1 / value2 + "");
                  }
                  
                  
                  else
                  {
                        if(currentText.length() > 0)
                        inputOutputField.setText(currentText);
                        else
                        inputOutputField.setText("");
                  }
                  
            }

            else if(e.getActionCommand().equals("+"))
            {
                  action = "add";
                  value1 = Double.parseDouble(currentText);
                  inputOutputField.setText("");      
            }
            else if(e.getActionCommand().equals("-"))
            {
                  action = "sub";
                  value1 = Double.parseDouble(currentText);
                  inputOutputField.setText("");      
            }
            else if(e.getActionCommand().equals("*"))
            {
                  action = "mult";
                  value1 = Double.parseDouble(currentText);
                  inputOutputField.setText("");      
            }
            else if(e.getActionCommand().equals("/"))
            {
                  action = "div";
                  value1 = Double.parseDouble(currentText);
                  inputOutputField.setText("");      
            }
            else if(e.getActionCommand().equals("C"))
            {
                  inputOutputField.setText("");      
            }
            else if(e.getActionCommand().equals("M1+"))
            {
                  memory1 = Double.parseDouble(currentText);        
                  inputOutputField.setText("");
            }
            else if(e.getActionCommand().equals("M2+"))
            {
                  memory2 = Double.parseDouble(currentText);        
                  inputOutputField.setText("");
            }
            
            else if(e.getActionCommand().equals("MR1"))
            {
                  inputOutputField.setText(memory1 +"");        
            }
            else if(e.getActionCommand().equals("MR2"))
            {
                  inputOutputField.setText(memory2 +"");        
            }
            else if(action.equals("."))
                  {
                        action = "blank";
                        inputOutputField.setText(currentText+=".");
                  }
      
          else
             inputOutputField.setText(currentText += e.getActionCommand());
       
    }
   
   
}