Solved

Source code for a simple java programming calculator.

Posted on 2004-04-15
5
82,672 Views
Last Modified: 2007-12-19
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
0
Comment
Question by:SAMANTHA1066
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
5 Comments
 
LVL 9

Accepted Solution

by:
mmuruganandam earned 500 total points
ID: 10832033
0
 
LVL 9

Expert Comment

by:mmuruganandam
ID: 10832042
If that link doesn't work, then copy paste the url and enter to get the page.

there is some problem with the geocities.
0
 
LVL 37

Expert Comment

by:zzynx
ID: 10832049
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
0
 
LVL 9

Expert Comment

by:mmuruganandam
ID: 10832087
Here is another applet calculator with source code.

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


Regards,
Muruga
0
 

Expert Comment

by:qwert1515
ID: 11074116
/*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());
       
    }
   
   
}

0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
The viewer will learn how to implement Singleton Design Pattern in Java.

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question