views:

36

answers:

1

i used this code and I am having problem in the very basic step of how to use operator. Moreover I am even having problem taking more then 1 digit. If you please just add up the missing statements which would help me out. In the given code I have removed those steps that created problems in actionPerformed function

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

public class calculator1 implements ActionListener
{
private JFrame f;
private JButton a,a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14,a15;
JTextField tf;
String msg="";

public calculator1()
{
f=new JFrame("Calculator");
f.setLayout(null);
a=new JButton("1");
a.setActionCommand("1");
a1=new JButton("2");
a1.setActionCommand("2");
a2=new JButton("3");
a2.setActionCommand("3");
a3=new JButton("4");
a3.setActionCommand("4");
a4=new JButton("5");
a4.setActionCommand("5");
a5=new JButton("6");
a5.setActionCommand("6");
a6=new JButton("7");
a6.setActionCommand("7");
a7=new JButton("8");
a7.setActionCommand("8");
a8=new JButton("9");
a8.setActionCommand("9");
a9=new JButton("0");
a9.setActionCommand("0");
a10=new JButton("+");
a10.setActionCommand("+");
a11=new JButton("-");
a11.setActionCommand("-");
a12=new JButton("*");
a12.setActionCommand("*");
a13=new JButton("/");
a13.setActionCommand("/");
a14=new JButton("=");
a14.setActionCommand("=");
a15=new JButton("00");
a15.setActionCommand("00");
tf= new JTextField(30);
}
public void launchframe()
{
f.setSize(500,600);
a.setBounds(100,200,50,50);
a.addActionListener(this);
a1.setBounds(160,200,50,50);
a1.addActionListener(this);
a2.setBounds(220,200,50,50);
a2.addActionListener(this);
a3.setBounds(100,300,50,50);
a3.addActionListener(this);
a4.setBounds(160,300,50,50);
a4.addActionListener(this);
a5.setBounds(220,300,50,50);
a5.addActionListener(this);
a6.setBounds(100,400,50,50);
a6.addActionListener(this);
a7.setBounds(160,400,50,50);
a7.addActionListener(this);
a8.setBounds(220,400,50,50);
a8.addActionListener(this);
a9.setBounds(100,500,50,50);
a9.addActionListener(this);
a10.setBounds(300,200,50,50);
a10.addActionListener(this);
a11.setBounds(300,300,50,50);
a11.addActionListener(this);
a12.setBounds(300,400,50,50);
a12.addActionListener(this);
a13.setBounds(300,500,50,50);
a13.addActionListener(this);
a14.setBounds(160,500,50,50);
a14.addActionListener(this);
a15.setBounds(220,500,50,50);
a15.addActionListener(this);

f.add(a);
f.add(a1);
f.add(a2);
f.add(a3);
f.add(a4);
f.add(a5);
f.add(a6);
f.add(a7);
f.add(a8);
f.add(a9);
f.add(a10);
f.add(a11);
f.add(a12);
f.add(a13);
f.add(a14);
f.add(a15);
tf.setBounds(100,150,250,30);
f.add(tf);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}

public void actionPerformed(ActionEvent ae)
{
String s=ae.getActionCommand();
tf.setText(s);
}

public static void main(String[]arg)
{

calculator1 c1=new calculator1();
c1.launchframe();
}
}
+1  A: 

Implement the calculator logic.

You have not implemented any logic in your program. You're only displaying whatever button was pressed.

You need to create a variable that contains the value being displayed. When you press a digit button, multiply this value by 10 and add the value of the button to it. Then always display this value. This will allow you to type in sequential digits.

Operators are more tricky. You'll need a variable to store the current operation as well as one to hold the running total. When an operator is pressed:

  • If there already was an operator pressed, then execute that operation on the running total and the value being displayed. Store the result back into the running total.
  • If the button pressed was not the equals, set the current operator and clear the value being displayed.
  • If the button pressed was the equals, copy the running total into the value being displayed and clear the running total.

This will give you a good loop to do operations on your calculator.

Erick Robertson
i know that the code is just the outline i have removed those codes an have asked for those "specific lines only"
tushar
I'm not going to write the code for you. Just follow the steps in my outline and think about it for yourself.
Erick Robertson
i'll surely was just justifying myself that why there is no logic present
tushar