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(thi s);
two.addActionListener(this );
three.addActionListener(th is);
four.addActionListener(thi s);
five.addActionListener(thi s);
six.addActionListener(this );
seven.addActionListener(th is);
eight.addActionListener(th is);
nine.addActionListener(thi s);
add.addActionListener(this );
subtract.addActionListener (this);
multiply.addActionListener (this);
divide.addActionListener(t his);
equals.addActionListener(t his);
setBackground(Color.white) ;
GridLayout line1 = new GridLayout(2,1,100,10);
row1.setLayout(line1);
row1.add(titleLabel);row1. add(result Field);
add(row1);
GridLayout line2 = new GridLayout(3,4,2,2);
row2.setLayout(line2);
row2.add(one);row2.add(two );
row2.add(three);row2.add(f our);row2. add(five);
row2.add(six);row2.add(sev en);row2.a dd(eight); row2.add(n ine);
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(ActionEven t event)
{
//if (event.getSource() == zero)
//......add code here otherwise your compiler will complain
}
}
please help
//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(thi
two.addActionListener(this
three.addActionListener(th
four.addActionListener(thi
five.addActionListener(thi
six.addActionListener(this
seven.addActionListener(th
eight.addActionListener(th
nine.addActionListener(thi
add.addActionListener(this
subtract.addActionListener
multiply.addActionListener
divide.addActionListener(t
equals.addActionListener(t
setBackground(Color.white)
GridLayout line1 = new GridLayout(2,1,100,10);
row1.setLayout(line1);
row1.add(titleLabel);row1.
add(row1);
GridLayout line2 = new GridLayout(3,4,2,2);
row2.setLayout(line2);
row2.add(one);row2.add(two
row2.add(three);row2.add(f
row2.add(six);row2.add(sev
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(ActionEven
{
//if (event.getSource() == zero)
//......add code here otherwise your compiler will complain
}
}
please help
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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
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
http://www.idoi.com/Java/Calculator.html
Regards,
Muruga
/*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.ligh tGray );
contentPane.setLayout(new FlowLayout());
JPanel textPanel = new JPanel();
textPanel.setBackground(Co lor.blue);
textPanel.setLayout(new FlowLayout());
inputOutputField = new JTextField("", 12);
inputOutputField.setBackgr ound(Color .white);
textPanel.add(inputOutputF ield);
contentPane.add(textPanel, BorderLayout.CENTER);
//Adds buttons to the caculators face
JButton buttonOne = new JButton("1");
buttonOne.addActionListene r(this);
contentPane.add(buttonOne) ;
JButton buttonTwo = new JButton("2");
buttonTwo.addActionListene r(this);
contentPane.add(buttonTwo) ;
JButton buttonThree = new JButton("3");
buttonThree.addActionListe ner(this);
contentPane.add(buttonThre e);
JButton buttonFour = new JButton("4");
buttonFour.addActionListen er(this);
contentPane.add(buttonFour );
JButton buttonFive = new JButton("5");
buttonFive.addActionListen er(this);
contentPane.add(buttonFive );
JButton buttonSix = new JButton("6");
buttonSix.addActionListene r(this);
contentPane.add(buttonSix) ;
JButton buttonSeven = new JButton("7");
buttonSeven.addActionListe ner(this);
contentPane.add(buttonSeve n);
JButton buttonEight = new JButton("8");
buttonEight.addActionListe ner(this);
contentPane.add(buttonEigh t);
JButton buttonNine = new JButton("9");
buttonNine.addActionListen er(this);
contentPane.add(buttonNine );
JButton buttonZero = new JButton("0");
buttonZero.addActionListen er(this);
contentPane.add(buttonZero );
JButton buttonDec = new JButton(".");
buttonDec.addActionListene r(this);
contentPane.add(buttonDec) ;
JButton buttonClear = new JButton("C");
buttonClear.addActionListe ner(this);
contentPane.add(buttonClea r);
JButton buttonAdd = new JButton("+");
buttonAdd.addActionListene r(this);
contentPane.add(buttonAdd) ;
JButton buttonSub = new JButton("-");
buttonSub.addActionListene r(this);
contentPane.add(buttonSub) ;
JButton buttonMult = new JButton("*");
buttonMult.addActionListen er(this);
contentPane.add(buttonMult );
JButton buttonDiv = new JButton("/");
buttonDiv.addActionListene r(this);
contentPane.add(buttonDiv) ;
JButton buttonEqu = new JButton("=");
buttonEqu.addActionListene r(this);
contentPane.add(buttonEqu) ;
JButton memoneplusButton = new JButton("M1+");
memoneplusButton.addAction Listener(t his);
contentPane.add(memoneplus Button);
JButton memtwoplusButton = new JButton("M2+");
memtwoplusButton.addAction Listener(t his);
contentPane.add(memtwoplus Button);
JButton mr1Button = new JButton("MR1");
mr1Button.addActionListene r(this);
contentPane.add(mr1Button) ;
JButton mr2Button = new JButton("MR2");
mr2Button.addActionListene r(this);
contentPane.add(mr2Button) ;
}
//Preforms the action when each button is pressed
public void actionPerformed(ActionEven t e)
{
String currentText = inputOutputField.getText() ;
Container contentPane = getContentPane();
if(e.getActionCommand().eq uals("="))
{
value2 = Double.parseDouble(current Text);
inputOutputField.setText(" ");
if(action.equals("add"))
{
action = "blank";
inputOutputField.setText(v alue1 + value2 + "");
}
else if(action.equals("sub"))
{
action = "blank";
inputOutputField.setText(v alue1 - value2 + "");
}
else if(action.equals("mult"))
{
action = "blank";
inputOutputField.setText(v alue1 * value2 + "");
}
else if(action.equals("div"))
{
action = "blank";
inputOutputField.setText(v alue1 / value2 + "");
}
else
{
if(currentText.length() > 0)
inputOutputField.setText(c urrentText );
else
inputOutputField.setText(" ");
}
}
else if(e.getActionCommand().eq uals("+"))
{
action = "add";
value1 = Double.parseDouble(current Text);
inputOutputField.setText(" ");
}
else if(e.getActionCommand().eq uals("-"))
{
action = "sub";
value1 = Double.parseDouble(current Text);
inputOutputField.setText(" ");
}
else if(e.getActionCommand().eq uals("*"))
{
action = "mult";
value1 = Double.parseDouble(current Text);
inputOutputField.setText(" ");
}
else if(e.getActionCommand().eq uals("/"))
{
action = "div";
value1 = Double.parseDouble(current Text);
inputOutputField.setText(" ");
}
else if(e.getActionCommand().eq uals("C"))
{
inputOutputField.setText(" ");
}
else if(e.getActionCommand().eq uals("M1+" ))
{
memory1 = Double.parseDouble(current Text);
inputOutputField.setText(" ");
}
else if(e.getActionCommand().eq uals("M2+" ))
{
memory2 = Double.parseDouble(current Text);
inputOutputField.setText(" ");
}
else if(e.getActionCommand().eq uals("MR1" ))
{
inputOutputField.setText(m emory1 +"");
}
else if(e.getActionCommand().eq uals("MR2" ))
{
inputOutputField.setText(m emory2 +"");
}
else if(action.equals("."))
{
action = "blank";
inputOutputField.setText(c urrentText +=".");
}
else
inputOutputField.setText(c urrentText += e.getActionCommand());
}
}
*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(
contentPane.setLayout(new FlowLayout());
JPanel textPanel = new JPanel();
textPanel.setBackground(Co
textPanel.setLayout(new FlowLayout());
inputOutputField = new JTextField("", 12);
inputOutputField.setBackgr
textPanel.add(inputOutputF
contentPane.add(textPanel,
//Adds buttons to the caculators face
JButton buttonOne = new JButton("1");
buttonOne.addActionListene
contentPane.add(buttonOne)
JButton buttonTwo = new JButton("2");
buttonTwo.addActionListene
contentPane.add(buttonTwo)
JButton buttonThree = new JButton("3");
buttonThree.addActionListe
contentPane.add(buttonThre
JButton buttonFour = new JButton("4");
buttonFour.addActionListen
contentPane.add(buttonFour
JButton buttonFive = new JButton("5");
buttonFive.addActionListen
contentPane.add(buttonFive
JButton buttonSix = new JButton("6");
buttonSix.addActionListene
contentPane.add(buttonSix)
JButton buttonSeven = new JButton("7");
buttonSeven.addActionListe
contentPane.add(buttonSeve
JButton buttonEight = new JButton("8");
buttonEight.addActionListe
contentPane.add(buttonEigh
JButton buttonNine = new JButton("9");
buttonNine.addActionListen
contentPane.add(buttonNine
JButton buttonZero = new JButton("0");
buttonZero.addActionListen
contentPane.add(buttonZero
JButton buttonDec = new JButton(".");
buttonDec.addActionListene
contentPane.add(buttonDec)
JButton buttonClear = new JButton("C");
buttonClear.addActionListe
contentPane.add(buttonClea
JButton buttonAdd = new JButton("+");
buttonAdd.addActionListene
contentPane.add(buttonAdd)
JButton buttonSub = new JButton("-");
buttonSub.addActionListene
contentPane.add(buttonSub)
JButton buttonMult = new JButton("*");
buttonMult.addActionListen
contentPane.add(buttonMult
JButton buttonDiv = new JButton("/");
buttonDiv.addActionListene
contentPane.add(buttonDiv)
JButton buttonEqu = new JButton("=");
buttonEqu.addActionListene
contentPane.add(buttonEqu)
JButton memoneplusButton = new JButton("M1+");
memoneplusButton.addAction
contentPane.add(memoneplus
JButton memtwoplusButton = new JButton("M2+");
memtwoplusButton.addAction
contentPane.add(memtwoplus
JButton mr1Button = new JButton("MR1");
mr1Button.addActionListene
contentPane.add(mr1Button)
JButton mr2Button = new JButton("MR2");
mr2Button.addActionListene
contentPane.add(mr2Button)
}
//Preforms the action when each button is pressed
public void actionPerformed(ActionEven
{
String currentText = inputOutputField.getText()
Container contentPane = getContentPane();
if(e.getActionCommand().eq
{
value2 = Double.parseDouble(current
inputOutputField.setText("
if(action.equals("add"))
{
action = "blank";
inputOutputField.setText(v
}
else if(action.equals("sub"))
{
action = "blank";
inputOutputField.setText(v
}
else if(action.equals("mult"))
{
action = "blank";
inputOutputField.setText(v
}
else if(action.equals("div"))
{
action = "blank";
inputOutputField.setText(v
}
else
{
if(currentText.length() > 0)
inputOutputField.setText(c
else
inputOutputField.setText("
}
}
else if(e.getActionCommand().eq
{
action = "add";
value1 = Double.parseDouble(current
inputOutputField.setText("
}
else if(e.getActionCommand().eq
{
action = "sub";
value1 = Double.parseDouble(current
inputOutputField.setText("
}
else if(e.getActionCommand().eq
{
action = "mult";
value1 = Double.parseDouble(current
inputOutputField.setText("
}
else if(e.getActionCommand().eq
{
action = "div";
value1 = Double.parseDouble(current
inputOutputField.setText("
}
else if(e.getActionCommand().eq
{
inputOutputField.setText("
}
else if(e.getActionCommand().eq
{
memory1 = Double.parseDouble(current
inputOutputField.setText("
}
else if(e.getActionCommand().eq
{
memory2 = Double.parseDouble(current
inputOutputField.setText("
}
else if(e.getActionCommand().eq
{
inputOutputField.setText(m
}
else if(e.getActionCommand().eq
{
inputOutputField.setText(m
}
else if(action.equals("."))
{
action = "blank";
inputOutputField.setText(c
}
else
inputOutputField.setText(c
}
}
there is some problem with the geocities.