Create a calculator with JCreator
Dear Java Friends;
I have got a problem when coding a java program to create a calculator with JCreator. Please help me to write codes for decimal point. :confused:
- Decimal point should not be set to the text more than 1 time until an operator is pressed.
- How can I use a Boolean value to solve this problem.
Re: Create a calculator with JCreator
What code do you have so far?
You would use a boolean variable to remember an event. It would be false until the event happened, then it would be set true.
Re: Create a calculator with JCreator
Code :
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class Calculator extends JFrame{
double firstNumber;
boolean calculate = false;
char operator;
JTextField txtDisplay;
JButton bt1;
JButton bt2;
JButton bt3;
JButton bt4;
JButton bt5;
JButton bt6;
JButton bt7;
JButton bt8;
JButton bt9;
JButton bt0;
JButton btAdd;
JButton btSub;
JButton btDiv;
JButton btMul;
JButton btEqual;
JButton btDec;
JButton btClear;
JButton btSqrt;
JPanel pane;
Calculator(){
setTitle("Calculator");
txtDisplay=new JTextField();
add(txtDisplay,"North");
pane=new JPanel();
pane.setLayout(new GridLayout(5,4,6,6));
bt1=new JButton("1");
bt1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
String s = txtDisplay.getText();
if (calculate) {
txtDisplay.setText("1");
calculate=false;
} else {
txtDisplay.setText(s + "1");
}
}
} );
bt2=new JButton("2");
bt2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
String s = txtDisplay.getText();
if (calculate) {
txtDisplay.setText("2");
calculate=false;
} else {
txtDisplay.setText(s + "2");
}
}
} );
bt3=new JButton("3");
bt3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
String s = txtDisplay.getText();
if (calculate) {
txtDisplay.setText("3");
calculate=false;
} else {
txtDisplay.setText(s + "3");
}
}
} );
bt4=new JButton("4");
bt4.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
String s = txtDisplay.getText();
if (calculate) {
txtDisplay.setText("4");
calculate=false;
} else {
txtDisplay.setText(s + "4");
}
}
} );
bt5=new JButton("5");
bt5.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
String s = txtDisplay.getText();
if (calculate) {
txtDisplay.setText("5");
calculate=false;
} else {
txtDisplay.setText(s + "5");
}
}
} );
bt6=new JButton("6");
bt6.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
String s = txtDisplay.getText();
if (calculate) {
txtDisplay.setText("6");
calculate=false;
} else {
txtDisplay.setText(s + "6");
}
}
} );
bt7=new JButton("7");
bt7.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
String s = txtDisplay.getText();
if (calculate) {
txtDisplay.setText("7");
calculate=false;
} else {
txtDisplay.setText(s + "7");
}
}
} );
bt8=new JButton("8");
bt8.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
String s = txtDisplay.getText();
if (calculate) {
txtDisplay.setText("8");
calculate=false;
} else {
txtDisplay.setText(s + "8");
}
}
} );
bt9=new JButton("9");
bt9.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
String s = txtDisplay.getText();
if (calculate) {
txtDisplay.setText("9");
calculate=false;
} else {
txtDisplay.setText(s + "9");
}
}
} );
bt0=new JButton("0");
bt0.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
String s = txtDisplay.getText();
if (calculate) {
txtDisplay.setText("0");
calculate=false;
} else {
txtDisplay.setText(s + "0");
}
}
} );
btClear=new JButton("C");
btClear.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
txtDisplay.setText(" ");
}
} );
btAdd=new JButton("+");
btAdd.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
String s = txtDisplay.getText();
firstNumber = Double.parseDouble(s);
calculate = true;
operator='+';
}
});
btSub=new JButton("-");
btSub.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
String s = txtDisplay.getText();
firstNumber = Double.parseDouble(s);
calculate = true;
operator='-';
}
});
btDiv=new JButton("/");
btDiv.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
String s = txtDisplay.getText();
firstNumber = Double.parseDouble(s);
calculate = true;
operator='/';
}
});
btMul=new JButton("*");
btMul.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
String s = txtDisplay.getText();
firstNumber = Double.parseDouble(s);
calculate = true;
operator='*';
}
});
btEqual=new JButton("=");
btEqual.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
String s = txtDisplay.getText();
double secondNumber=Double.parseDouble(s);
double result=secondNumber;
switch(operator){
case '+':
result=firstNumber+secondNumber;
break;
case '-':
result=firstNumber-secondNumber;
break;
case '/':
result=firstNumber/secondNumber;
break;
case '*':
result=firstNumber*secondNumber;
break;
}
txtDisplay.setText(""+result);
}
});
btDec=new JButton(".");
btDec.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
String s = txtDisplay.getText();
// int count=0;
if (calculate) {
txtDisplay.setText(".");
calculate=false;
} else {
txtDisplay.setText(s + ".");
// calculate=true;
}
}
} );
btSqrt=new JButton("Sqrt");
btSqrt.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
String s = txtDisplay.getText();
s=txtDisplay.getText();
double q=Math.sqrt(Double.parseDouble(s));
txtDisplay.setText(q+"");
}
} );
pane.add(bt1);
pane.add(bt2);
pane.add(bt3);
pane.add(btAdd);
pane.add(bt4);
pane.add(bt5);
pane.add(bt6);
pane.add(btSub);
pane.add(bt7);
pane.add(bt8);
pane.add(bt9);
pane.add(btDiv);
pane.add(bt0);
pane.add(btDec);
pane.add(btEqual);
pane.add(btMul);
pane.add(btClear);
pane.add(btSqrt);
add(pane);
}
}
class DemoFrame{
public static void main(String args[]){
Calculator c1=new Calculator();
c1.pack();
c1.show();
}
}
This is the code that I have coded. Please help me to re-correct my program, as the decimal point is not working properly.=((
Re: Create a calculator with JCreator
Quote:
the decimal point is not working properly
Can your be more specific about the problem?
Show what program does, describe why that is wrong and show what it should do.
Re: Create a calculator with JCreator
Hello Java Friends;
Sorry for the delay reply...
The Decimal Point is not working properly. :((
The program should not set the decimal point to a number more than one time; until an operator is pressed. I have given the code that I have coded. But I can't re-correct the error. Please help me to solve this problem. :(
Re: Create a calculator with JCreator
Quote:
program should not set the decimal point to a number more than one time
It would help if you could post an example of the program doing this.
Use println() to output the wrong results with a comment about how its wrong.
It sounds like you need a flag to remember if a decimal point has been set and to not allow another if one has been entered.