Solved

Source code for a simple java programming calculator.

Posted on 2004-04-15
5
82,050 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
  • 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
V.simple question about Android packages and libraries 1 30
by zero exception 10 52
login form jsp example 2 37
Java 8 to Java 6 8 22
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…

828 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