Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

java calculator -- NEED HELP!!

Status
Not open for further replies.

cindyg919

Programmer
Feb 10, 2001
17
US
I'm trying to write a calculator java applet, so far i've declared the buttons and terxtfield and i've intialized it also, what I don't know how to do is add the numbers, subtract, multiply, divide, square root it, add to memory and so forth when the user presses the buttons...
Can any1 help in doing this?? I'm using the GridLayout technique...

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class Cal extends JFrame implements ActionListener
{
public static final int WIDTH = 400;
public static final int HEIGHT = 400;

Button one, two, three, four, five, six, seven, eight, nine, zero;
Button C,CE,MC,MR,MS,Mplus,plumin,dot,sqrt,perc,recip;
Button add,mult,div,sub,equals;

public static void main(String[] args)
{
Cal c = new Cal();
c.setVisible(true);
}

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

addWindowListener(new WindowDestroyer());
setTitle("Parul's Cal.");
Container content = getContentPane();
contentPane.setLayout(new FlowLayout());

text = new TextField("");
text.setForeground(Color.black);
text.setEditable(false);

Container contentPane = getContentPane();
contentPane.setlayout(new BorderLayout(0, 10));
contentPane.add(text, "North");
content.add(contentPane, "Center");

Container contentPane = getContentPane();
contentPane.setBackground(Color.cyan);
contentPane.setLayout(new GridLayout(5, 6));

JButton zeroButton = new JButton("0");
zeroButton.addActionListener(this);
contentPane.add(zeroButton);

JButton oneButton = new JButton("1");
oneButton.addActionListener(this);
contentPane.add(oneButton);

JButton twoButton = new JButton("2");
twoButton.addActionListener(this);
contentPane.add(twoButton);

JButton threeButton = new JButton("3");
threeButton.addActionListener(this);
contentPane.add(threeButton);

JButton fourButton = new JButton("4");
fourButton.addListener(this);
contentPane.add(fourButton);

JButton fiveButton = new JButton("5");
fiveButton.addListener(this);
contentPane.add(fiveButton);

JButton sixButton = new JButton("6");
sixButton.addListener(this);
contentPane.add(sixButton);

JButton sevenButton = new JButton("7");
sevenButton.addListener(this);
contentPane.add(sevenButton);

JButton eightButton = new JButton("8");
eightButton.addListener(this);
contentPane.add(eightButton);

JButton nineButton = new JButton("9");
nineButton.addListener(this);
contentPane.add(nineButton);

JButton MCButton = new JButton("MC");
MCButton.addListener(this);
contentPane.add(MCButton);

JButton MRButton = new JButton("MR");
MRButton.addListener(this);
contentPane.add(MRButton);

JButton MSButton = new JButton("MS");
MSButton.addListener(this);
contentPane.add(MSButton);

JButton mPlusButton = new JButton("M+");
mPlusButton.addListener(this);
contentPane.add(mPlusButton);

JButton CEButton = new JButton("CE");
CEButton.addListener(this);
contentPane.add(CEButton);

JButton CButton = new JButton("C");
CButton.addListener(this);
contentPane.add(CButton);

JButton divButton = new JButton("/");
divButton.addListener(this);
contentPane.add(divButton);

JButton multButton = new JButton("*");
multButton.addListener(this);
contentPane.add(multButton);

JButton addButton = new JButton("+");
addButton.addListener(this);
contentPane.add(addButton);

JButton subButton = new JButton("-");
subButton.addListener(this);
contentPane.add(subButton);

JButton dotButton = new JButton(".");
dotButton.addListener(this);
contentPane.add(dotButton);

JButton equalsButton = new JButton("=");
equalsButton.addListener(this);
contentPane.add(equalsButton);

JButton recipButton = new JButton("1/x");
recipButton.addListener(this);
contentPane.add(recipButton);

JButton percButton = new JButton("%");
percButton.addListener(this);
contentPane.add(percButton);

JButton sqrtButton = new JButton("sqrt");
sqrtButton.addListener(this);
contentPane.add(sqrtButton);

JButton plusminButton = new JButton("+/-");
plusminButton.addListener(this);
contentPane.add(plusminButton);
}

public void actionPerformed(ActionEvent e)
{
Container contentPane = getContentPane();

if(e.getActionCommand().equals("0"))
 
Something like this in your event handling.

public void actionPerformed(ActionEvent e)
{
if(e.getSource() == zeroButton)
{
if (nbrOneFlg) //entering first nbr
{
nbrOne += "0";
text.setText(nbrOne); // nbrOne instance String
}
else //entering second nbr
{
nbrTwo + "0";
text.setText(nbrTwo); // nbrTwo instance String
}
}
// do rest of buttons 1 - 9

if(e.getSource() == addButton)
{
nbrOneFlg = false;
typeCalc = '+'; // instance char var for type calc
}
if(e.getSource() == equalsButton)
{ // instance calcNbr as int
if (typeCalc == '+') // add
{
calcNbr = (Integer.parseInt(nbrOne)) +
(Integer.parseInt(nbrOne));
text.setText(String.valueOf(calcNbr));
}
// rest of calcs
}
}

good luck,
Brian
 
hi thanx but i kind of changed my program to panel:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class Calculator extends JFrame implements ActionListener
{
public static final int WIDTH = 400;
public static final int HEIGHT = 400;

private JTextField theText;

public static void main(String[] args)
{
Calculator cal = new Calculator();
Cal.setVisible(true);
}

public Calculator()
{
setTitle(“Parul’s Calculator”);
setSize(WIDTH, HEIGHT);
addWindowListener(new WindowDestroyer());
Container content = getContentPane();
content.setLayout(new BorderLayout());

Jpanel textPanel = new Jpanel();
textPanel.setBackground(Color.gray);
textPanel.setLayout(new FlowLayout());
theText = new JTextField(“0”, 30);
theText.setBackground(Color.white);
textPanel.add(theText);
content.add(textPanel, BorderLayout.NORTH);

JPanel buttonPanel = new JPanel();
buttonPanel.setBackground(Color.blue);
buttonPanel.setLayout(new GridLayout(5,6));
JButton zeroButton = new JButton(“0”);
zeroButton.addActionListener(this);
buttonPanel.add(zeroButton);
JButton oneButton = new JButton(“1”);
oneButton.addActionListener(this);
buttonPanel.add(oneButton);
JButton twoButton = new JButton(“2”);
twoButton.addActionListener(this);
buttonPanel.add(twoButton);
JButton threeButton = new JButton(“3”);
threeButton.addActionListener(this);
buttonPanel.add(threeButton);
JButton fourButton = new JButton(“4”);
fourButton.addActionListener(this);
buttonPanel.add(fourButton);
JButton fiveButton = new JButton(“5”);
fiveButton.addActionListener(this);
buttonPanel.add(fiveButton);
JButton sixButton = new JButton(“6”);
sixButton.addActionListener(this);
buttonPanel.add(sixButton);
JButton sevenButton = new JButton(“7”);
sevenButton.addActionListener(this);
buttonPanel.add(sevenButton);
JButton eightButton = new JButton(“8”);
eightButton.addActionListener(this);
buttonPanel.add(eightButton);
JButton nineButton = new JButton(“9”);
nineButton.addActionListener(this);
buttonPanel.add(nineButton);
JButton MCButton = new JButton(“MC”);
MCButton.addActionListener(this);
buttonPanel.add(MCButton);
JButton MRButton = new JButton(“MR”);
MRButton.addActionListener(this);
buttonPanel.add(MRButton);
JButton MSButton = new JButton(“MS”);
MSButton.addActionListener(this);
buttonPanel.add(MSButton);
JButton mPlusButton = new JButton(“M+”);
mPlusButton.addActionListener(this);
buttonPanel.add(mPlusButton);
JButton CEButton = new JButton(“CE”);
CEButton.addActionListener(this);
buttonPanel.add(CEButton);
JButton CButton = new JButton(“C”);
CButton.addActionListener(this);
buttonPanel.add(CButton);
JButton plusMinButton = new JButton(“+/-”);
plusMinButton.addActionListener(this);
buttonPanel.add(plusMinButton);
JButton dotButton = new JButton(“.”);
dotButton.addActionListener(this);
buttonPanel.add(dotButton);
JButton addButton = new JButton(“+”);
addButton.addActionListener(this);
buttonPanel.add(addButton);
JButton subButton = new JButton(“-”);
subButton.addActionListener(this);
buttonPanel.add(subButton);
JButton multButton = new JButton(“*”);
multButton.addActionListener(this);
buttonPanel.add(multButton);
JButton divButton = new JButton(“/”);
divButton.addActionListener(this);
buttonPanel.add(divButton);
JButton equalsButton = new JButton(“=”);
equalsButton.addActionListener(this);
buttonPanel.add(equalsButton);
JButton sqrtButton = new JButton(“sqrt”);
sqrtButton.addActionListener(this);
buttonPanel.add(sqrtButton);
JButton recipButton = new JButton(“1/x”);
recipButton.addActionListener(this);
buttonPanel.add(recipButton);
JButton percButton = new JButton(“%”);
percButton.addActionListener(this);
buttonPanel.add(percButton);
content.ad(buttonPanel,BorderLayout.CENTER);
}

private static double stringToDouble(String stringObject)
{
return Double.parseDouble(stringObject.trim());
}

public void actionPerformed(ActionEvent e)
{



would i not do something like this:
if(getActionCommand.equlas.(+))
num1+num2

?????



 
Yes, but I would add the values when the user presses the "=" button.

if(getActionCommand.equals("="))
num1+num2;

the getActionCommand just returns the caption of the button that caused the event. That is another way to know what button was pressed. It returns a string, so don't forget the quotes as I did above.

As you know num1, num2, answer will have to be declared as instance variables as you did with theText. You can use

num1 = Integer.parseInt(theText.getText());

to convert a string to an Integer. As each number button is pressed, append the new number string to theText, your JTextField. Then when the "+" button is pressed save this number in num1 as above. Then as the user presses numbers again for the 2nd num fill in theText again. Lastly, when the "=" is pressed convert the second number into num2 as an integer and add them together.

answer = num1 + num2;

Convert answer to a string and set it for your JTextField.

theText.setText(String.valueOf(answer));

Does that make sense? Think about the order the events occur with a real calculator. Your headed in the right direction. You will get it. Just break it down, write a little code, test it, and then move on.

Brian
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top