FileDocCategorySizeDatePackage
FullCalculator.javaAPI DocExample5757Tue Sep 12 15:58:18 BST 2000None

Calculator

public class Calculator extends Applet implements ActionListener

Fields Summary
int
currentValue
int
operand1
int
operand2
int
result
int
operation
TextField
calculatorDisplay
Panel
numPanel
Panel
opsPanel
Panel
arrPanel
Button[]
butArray
Button
one
Button
two
Button
three
Button
four
Button
five
Button
six
Button
seven
Button
eight
Button
nine
Button
zero
Button
plus
Button
minus
Button
times
Button
divide
Button
equals
Button
clear
Constructors Summary
Methods Summary
public voidactionPerformed(java.awt.event.ActionEvent e)

   		for (int index = 0; index < 10; index++)
		   {
		   	if (e.getSource() == butArray[index])
			   {
					currentValue = currentValue * 10 + index;
					calculatorDisplay.setText(Integer.toString(currentValue));
			   }
			}	
			if(e.getSource() == one)
			{
				currentValue = currentValue * 10 + 1;
				calculatorDisplay.setText(Integer.toString(currentValue));
			}
			else if(e.getSource() == two)
			{
				currentValue = currentValue * 10 + 2;
				calculatorDisplay.setText(Integer.toString(currentValue));
			}
			else if(e.getSource() == three)
			{
				currentValue = currentValue * 10 + 3;
				calculatorDisplay.setText(Integer.toString(currentValue));
			}
			else if(e.getSource() == four)
			{
				currentValue = currentValue * 10 + 4;
				calculatorDisplay.setText(Integer.toString(currentValue));
			}
			else if(e.getSource() == five)
			{
				currentValue = currentValue * 10 + 5;
				calculatorDisplay.setText(Integer.toString(currentValue));
			}
			else if(e.getSource() == six)
			{
				currentValue = currentValue * 10 + 6;
				calculatorDisplay.setText(Integer.toString(currentValue));
			}
			else if(e.getSource() == seven)
			{
				currentValue = currentValue * 10 + 7;
				calculatorDisplay.setText(Integer.toString(currentValue));
			}
			else if(e.getSource() == eight)
			{
				currentValue = currentValue * 10 + 8;
				calculatorDisplay.setText(Integer.toString(currentValue));
			}
			else if(e.getSource() == nine)
			{
				currentValue = currentValue * 10 + 9;
				calculatorDisplay.setText(Integer.toString(currentValue));
			}
			else if(e.getSource() == zero)
			{
				currentValue = currentValue * 10 + 0;
				calculatorDisplay.setText(Integer.toString(currentValue));
			}
			else if (e.getSource() == clear)
			{
				calculatorDisplay.setText("0");
				currentValue = 0;
				operand1     = 0;
				operand2     = 0;
			}
			else if (e.getSource() == plus)
			{
				operand1     = currentValue;
				currentValue = 0;
				calculatorDisplay.setText("0");
				operation    = 1;
			}
			else if (e.getSource() == minus)
			{
				operand1     = currentValue;
				currentValue = 0;
				calculatorDisplay.setText("0");
				operation    = 2;
			}				
			else if (e.getSource() == times)
			{
				operand1     = currentValue;
				currentValue = 0;
				calculatorDisplay.setText("0");
				operation    = 3;
			}				
			else if (e.getSource() == divide)
			{
				operand1     = currentValue;
				currentValue = 0;
				calculatorDisplay.setText("0");
				operation    = 4;
			}				
			else if (e.getSource() == equals)
			{
				operand2     = currentValue;
				currentValue = 0;
				switch (operation)
				{
					case 1: result = operand1  +  operand2; break;
					case 2: result = operand1  -  operand2; break;
					case 3: result = operand1  *  operand2; break;
					case 4: result = operand1  /  operand2; break;
				}
				calculatorDisplay.setText(Integer.toString(result));
				operand1 = operand2 = 0;
			
			}				
			
	
public voidinit()


	  
	
		numPanel.setLayout(new GridLayout(4,3,5,5));
		add(calculatorDisplay);
		numPanel.add(one);
		numPanel.add(two);
		numPanel.add(three);
		numPanel.add(four);
		numPanel.add(five);
		numPanel.add(six);
		numPanel.add(seven);
		numPanel.add(eight);
		numPanel.add(nine);
		numPanel.add(zero);
		
		opsPanel.setLayout(new GridLayout(2,4, 5, 5));		
		opsPanel.add(plus);
		opsPanel.add(minus);
		opsPanel.add(times);
		opsPanel.add(divide);
		opsPanel.add(equals);
		add(numPanel);
		add(opsPanel);
		add(clear);
		
		one.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);
		zero.addActionListener(this);
		clear.addActionListener(this);
		plus.addActionListener(this);
		minus.addActionListener(this);
		times.addActionListener(this);
		divide.addActionListener(this);
		equals.addActionListener(this);
	
		arrPanel.setLayout(new GridLayout(4,3,5,5));
		for (int index = 0; index < 10; index++)
		{
			butArray[index] = new Button(Integer.toString(index));
			arrPanel.add(butArray[index]);
			butArray[index].addActionListener(this);
		}
		add(arrPanel);
		currentValue = 0;
	
public voidpaint(java.awt.Graphics g)

		setForeground(Color.blue);
		setBackground(Color.yellow);
		g.drawString(" " + operand1 + "  " + operand2, 0, 380);