Help on this program
Hi this is a calculator program I made.
import java.awt.* ;
import java.awt.event.*;
import javax.swing.*;
public class CalculatorProject extends JFrame implements ActionListener{
JButton num_9;
JButton num_8;
JButton num_7;
JButton num_6;
JButton num_5;
JButton num_4;
JButton num_3;
JButton num_2;
JButton num_1;
JButton num_0;
JButton equal_sign;
JButton clear;
JButton multiplication;
JButton division;
JButton addition;
JButton subtraction;
JButton decimal_point;
JButton cos_button;
JButton square_root;
double num1;
double num2;
double result;
String str;
String change;
JTextArea answer;
//JLabel answer_label;
JPanel answer_panel;
JPanel keypad_panel;
public CalculatorProject(){
setTitle("Calculator By Pranav Angara and Hyung Jinh Ahn");
setDefaultCloseOperation( EXIT_ON_CLOSE );
num_9 = new JButton("9");
num_8 = new JButton("8");
num_7 = new JButton("7");
num_6 = new JButton("6");
num_5 = new JButton("5");
num_4 = new JButton("4");
num_3 = new JButton("3");
num_2 = new JButton("2");
num_1 = new JButton("1");
num_0 = new JButton("0");
equal_sign = new JButton("=");
clear = new JButton("CLR");
multiplication = new JButton("*");
division = new JButton("/");
subtraction = new JButton("-");
addition = new JButton("+");
decimal_point = new JButton(".");
square_root = new JButton("Sqrt");
num_9.setActionCommand("9");
num_8.setActionCommand("8");
num_7.setActionCommand("7");
num_6.setActionCommand("6");
num_5.setActionCommand("5");
num_4.setActionCommand("4");
num_3.setActionCommand("3");
num_2.setActionCommand("2");
num_1.setActionCommand("1");
num_0.setActionCommand("0");
equal_sign.setActionCommand("=");
clear.setActionCommand("CLR");
multiplication.setActionCommand("*");
division.setActionCommand("/");
subtraction.setActionCommand("-");
addition.setActionCommand("+");
decimal_point.setActionCommand(".");
square_root.setActionCommand("Sqrt");
keypad_panel = new JPanel();
keypad_panel.setLayout(new GridLayout(6,3));
keypad_panel.add(num_9);
keypad_panel.add(num_8);
keypad_panel.add(num_7);
keypad_panel.add(num_6);
keypad_panel.add(num_5);
keypad_panel.add(num_4);
keypad_panel.add(num_3);
keypad_panel.add(num_2);
keypad_panel.add(num_1);
keypad_panel.add(num_0);
keypad_panel.add(addition);
keypad_panel.add(subtraction);
keypad_panel.add(multiplication);
keypad_panel.add(division);
keypad_panel.add(equal_sign);
keypad_panel.add(clear);
keypad_panel.add(decimal_point);
keypad_panel.add(square_root);
answer = new JTextArea(0,0);
answer.setEditable( true );
answer_panel = new JPanel();
answer_panel.setLayout(new GridLayout(1,1));
answer_panel.add(answer);
//System.out.println(answer.getPreferredSize());
keypad_panel.setBackground(Color.yellow);
getContentPane().setLayout( new BorderLayout() );
getContentPane().add(keypad_panel, BorderLayout.WEST);
getContentPane().add(answer_panel, BorderLayout.NORTH);
num_9.addActionListener(this);
num_8.addActionListener(this);
num_7.addActionListener(this);
num_6.addActionListener(this);
num_5.addActionListener(this);
num_4.addActionListener(this);
num_3.addActionListener(this);
num_2.addActionListener(this);
num_1.addActionListener(this);
num_0.addActionListener(this);
addition.addActionListener(this);
subtraction.addActionListener(this);
division.addActionListener(this);
clear.addActionListener(this);
equal_sign.addActionListener(this);
multiplication.addActionListener(this);
decimal_point.addActionListener(this);
square_root.addActionListener(this);
}
public void actionPerformed(ActionEvent evt){
String action = evt.getActionCommand();
if(action.equals("9"))
answer.append("9");
if(action.equals("8"))
answer.append("8");
if(action.equals("7"))
answer.append("7");
if(action.equals("6"))
answer.append("6");
if(action.equals("5"))
answer.append("5");
if(action.equals("4"))
answer.append("4");
if(action.equals("3"))
answer.append("3");
if(action.equals("2"))
answer.append("2");
if(action.equals("1"))
answer.append("1");
if(action.equals("0"))
answer.append("0");
if(action.equals("CLR"))
answer.setText("");
if(action.equals("+"))
{
num1 = new Double(answer.getText()).doubleValue();
str = "+";
answer.setText("");
}
if(action.equals("-"))
{
num1 = new Double(answer.getText()).doubleValue();
str = "-";
answer.setText("");
}
if(action.equals("*"))
{
num1 = new Double(answer.getText()).doubleValue();
str = "*";
answer.setText("");
}
if(action.equals("/"))
{
num1 = new Double(answer.getText()).doubleValue();
str = "/";
answer.setText("");
}
if(action.equals("."))
{
answer.append(".");
}
if(action.equals("Sqrt"))
{
num1 = new Double(answer.getText()).doubleValue();
str = "Sqrt";
answer.setText("");
}
if(action.equals("=") && str.equals("+"))
{
num2= new Double(answer.getText()).doubleValue();
result = (num1+num2);
change = ""+ result;
answer.setText(change.substring(0,11));
}
if(action.equals("=") && str.equals("-"))
{
num2=new Double(answer.getText()).doubleValue();
result = (num1-num2);
answer.setText(Double.toString(result));
}
if(action.equals("=") && str.equals("*"))
{
num2=new Double(answer.getText()).doubleValue();
result = (num1*num2);
change = ""+ result;
//if(change.length() > 10)
// {
// answer.setText(change.substring(0,10));
//}
//if(change.length() <= 10)
//{
answer.setText(change);
//}
}
if(action.equals("=") && str.equals("/"))
{
num2=new Double(answer.getText()).doubleValue();
if(num2 == 0.0)
{
answer.setText("Error Cannot Divide by Zero");
}
if(num2 != 0)
{
result = (num1/num2);
answer.setText(Double.toString(result));
}
}
if(action.equals("=") && str.equals("Sqrt"))
{
result = Math.sqrt(num1);
answer.setText(Double.toString(result));
}
}
public static void main ( String[] args )
{
CalculatorProject cp = new CalculatorProject() ;
WindowQuitter wquit = new WindowQuitter();
cp.addWindowListener( wquit );
cp.setSize( 250, 300 );
cp.setVisible( true );
}
}
class WindowQuitter extends WindowAdapter
{
public void windowClosing( WindowEvent e )
{
System.exit( 0 );
}
}
-
The problem I have with this program is that, if the answer of any operation e.g. Multiplication, exceeds a 10 digit limit, I want to display the answer to only 10 digits.
Suppose the answer is 21231231231313. I want only the first 10 digits.
Another condition I am not able to satisfy is that, if the input numbers are integers then the output must also be integer.
Example
if I enter 5+5, I want it to be 10, not 10.0.
but if I enter 4.5 + 2, I want it to be 6.5, not 6.
For the same condition if I divide 7.5/1.5, I want 3, not 3.0
Plz could anyone kindly come up with the solution.
Thanks ,
prv_aus.

