Important alert: (current site time 5/25/2013 5:05:59 AM EDT)
 

VB icon

Calculator

Email
Submitted on: 4/24/2012 10:23:30 AM
By: AhmedNassar  
Level: Beginner
User Rating: Unrated
Compatibility: Java (JDK 1.4), Java (JDK 1.5)
Views: 1366
(About the author)
 
     My first calculator
 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
 
Terms of Agreement:   
By using this code, you agree to the following terms...   
  1. You may use this code in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
  2. You MAY NOT redistribute this code (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
  3. You may link to this code from another website, but ONLY if it is not wrapped in a frame. 
  4. You will abide by any additional copyright restrictions which the author may have placed in the code or code's description.
				
//**************************************
// Name: Calculator
// Description:My first calculator
// By: AhmedNassar
//
//This code is copyrighted and has// limited warranties.Please see http://www.Planet-Source-Code.com/vb/scripts/ShowCode.asp?txtCodeId=7015&lngWId=2//for details.//**************************************

package Swings;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.Frame;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextArea;
public class Calculator extends JFrame{
static double number_1; // First input
static double number_2; // Second input
static String operator; // Operator value
static boolean checkOperator; // Check: if one of the operators clicked, the screen will be cleaned 
static double input;// Input from the screen
static String result;// Result of the math operation
static JTextArea text_screen = new JTextArea("0",2,15);
JButton btn_1 = new JButton(" 1 ");
JButton btn_2 = new JButton(" 2 ");
JButton btn_3 = new JButton(" 3 ");
JButton btn_4 = new JButton(" 4 ");
JButton btn_5 = new JButton(" 5 ");
JButton btn_6 = new JButton(" 6 ");
JButton btn_7 = new JButton(" 7 ");
JButton btn_8 = new JButton(" 8 ");
JButton btn_9 = new JButton(" 9 ");
JButton btn_0 = new JButton(" 0 ");
JButton btn_square = new JButton("x^2");
JButton btn_inverse = new JButton("1/x");
JButton btn_sqrt = new JButton("sqrt");
JButton btn_plus_minus = new JButton("+/-");
JButton btn_float = new JButton(" . ");
JButton btn_plus = new JButton(" + ");
JButton btn_minus = new JButton(" - ");
JButton btn_multi = new JButton(" × ");
JButton btn_divide = new JButton(" / ");
JButton btn_equal = new JButton(" = ");
JButton btn_mod = new JButton(" % ");
JButton btn_backspace = new JButton(" Backspace ");
JButton btn_clean = new JButton("C");
JButton btn_pow = new JButton("x^n");
JButton btn_pi = new JButton("pi");
JButton btn_exp= new JButton("e");
JButton btn_log= new JButton("log");
JButton btn_sin= new JButton("sin");
JButton btn_cos= new JButton("cos");
JButton btn_tan= new JButton("tan");
JPanel panel_1 = new JPanel(new GridLayout());
JPanel panel_2 = new JPanel(new GridLayout(1,1,5,5));
JPanel panel_3 = new JPanel(new GridLayout(1,1,5,5));
JPanel panel_5 = new JPanel(new GridLayout(1,1,5,5));
JPanel panel_6 = new JPanel(new GridLayout(1,1,5,5));
JPanel panel_7 = new JPanel(new GridLayout(1,1,5,5));
JPanel panel_8 = new JPanel(new GridLayout(1,1,5,5));
JPanel panel_9 = new JPanel(new GridLayout(1,1,5,5));
JPanel panel_10 = new JPanel(new GridLayout(1,1,5,5));
public static void main(String[] args) {
Calculator cal = new Calculator();
cal.setCalculator();
cal.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
private void setCalculator(){
text_screen.setLineWrap(true);
text_screen.setEditable(false);
text_screen.setFont(new Font("Algerian",Font.PLAIN,20));
setSize(300, 430);
setVisible(true);
setResizable(false);
setTitle("Calculator");
setLocation(350, 250);
setLayout(new GridLayout(9,4,5,5));
addComponent();
}
private void addComponent() {
panel_1.add(text_screen);
panel_2.add(btn_backspace);
panel_2.add(btn_clean);
panel_3.add(btn_sin);
panel_3.add(btn_cos);
panel_3.add(btn_tan);
panel_5.add(btn_pow);
panel_5.add(btn_log);
panel_5.add(btn_exp);
panel_5.add(btn_pi);
panel_6.add(btn_square);
panel_6.add(btn_inverse);
panel_6.add(btn_sqrt);
panel_6.add(btn_plus_minus);
panel_7.add(btn_plus);
panel_7.add(btn_minus);
panel_7.add(btn_multi);
panel_7.add(btn_divide);
panel_7.add(btn_mod);
panel_8.add(btn_9);
panel_8.add(btn_8);
panel_8.add(btn_7);
panel_8.add(btn_6);
panel_9.add(btn_5);
panel_9.add(btn_4);
panel_9.add(btn_3);
panel_9.add(btn_2);
panel_10.add(btn_1);
panel_10.add(btn_0);
panel_10.add(btn_float);
panel_10.add(btn_equal);
add(panel_1);
add(panel_2);
add(panel_3);
add(panel_5);
add(panel_6);
add(panel_7);
add(panel_8);
add(panel_9);
add(panel_10);
btn_0.addActionListener(new MyLisetner());
btn_1.addActionListener(new MyLisetner());
btn_2.addActionListener(new MyLisetner());
btn_3.addActionListener(new MyLisetner());
btn_4.addActionListener(new MyLisetner());
btn_5.addActionListener(new MyLisetner());
btn_6.addActionListener(new MyLisetner());
btn_7.addActionListener(new MyLisetner());
btn_8.addActionListener(new MyLisetner());
btn_9.addActionListener(new MyLisetner());
btn_float.addActionListener(new MyLisetner());
btn_plus.addActionListener(new MyLisetner());
btn_minus.addActionListener(new MyLisetner());
btn_multi.addActionListener(new MyLisetner());
btn_divide.addActionListener(new MyLisetner());
btn_mod.addActionListener(new MyLisetner());
btn_plus_minus.addActionListener(new MyLisetner());
btn_equal.addActionListener(new MyLisetner());
btn_clean.addActionListener(new MyLisetner());
btn_backspace.addActionListener(new MyLisetner());
btn_square.addActionListener(new MyLisetner());
btn_sqrt.addActionListener(new MyLisetner());
btn_inverse.addActionListener(new MyLisetner());
btn_pi.addActionListener(new MyLisetner());
btn_exp.addActionListener(new MyLisetner());
btn_log.addActionListener(new MyLisetner());
btn_pow.addActionListener(new MyLisetner());
btn_sin.addActionListener(new MyLisetner());
btn_cos.addActionListener(new MyLisetner());
btn_tan.addActionListener(new MyLisetner());
}
public static void addDigit(int digit) {// Add digit to screen
if (checkOperator) {// Check: if one of the operators clicked, the screen will be cleaned
text_screen.setText("0");
checkOperator = false;
}
if (Double.parseDouble(text_screen.getText()) == 0) { //(To prevent the appearance of the useless left 0)
text_screen.setText("");
text_screen.setText(text_screen.getText() + digit); // Add digit
}else {
text_screen.setText(text_screen.getText() + digit); // Add digit
}
input = Double.parseDouble(text_screen.getText()); // save the last screen input as double
}
public void addOperator(String mathOp) {// Choose the operators 
operator = mathOp;
checkOperator = true;
}
public void changeScreen(double input) {// Add the new result to screen
text_screen.setText(Double.toString(input));
}
public class MyLisetner implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
operationButtons(e);
numberButtons(e);
equal(e);
}
public void numberButtons(ActionEvent e) {
JButton button = (JButton) e.getSource() ;
if (button == btn_0) {
addDigit(0);
}else if (button == btn_1) {
addDigit(1);
}else if (button == btn_2) {
addDigit(2);
}else if (button == btn_3) {
addDigit(3);
}else if (button == btn_4) {
addDigit(4);
}else if (button == btn_5) {
addDigit(5);
}else if (button == btn_6) {
addDigit(6);
}else if (button == btn_7) {
addDigit(7);
}else if (button == btn_8) {
addDigit(8);
}else if (button == btn_9) {
addDigit(9);
}else if (button == btn_float) {
if (!text_screen.getText().contains(".")) 
text_screen.append(".");
}else if (button == btn_plus_minus) {
input *= -1; 
changeScreen(input);
}else if (button == btn_square) {
input *= input;
changeScreen(input);
}else if (button == btn_inverse) {
input = 1/input;
changeScreen(input);
}else if (button == btn_sqrt) {
input = Math.sqrt(input);
changeScreen(input);
}else if (button == btn_pi) {
input = Math.PI;
changeScreen(input);
}else if (button == btn_exp) {
input = Math.exp(number_1);
changeScreen(input);
}else if (button == btn_log) {
input = Math.log10(number_1);
changeScreen(input);
}else if (button == btn_sin) {
input = Math.sin(number_1/ 180*Math.PI);
changeScreen(input);
}else if (button == btn_cos) {
input = Math.cos(number_1/ 180 *Math.PI);
changeScreen(input);
}else if (button == btn_tan) {
input = Math.tan(number_1/ 180*Math.PI);
changeScreen(input);
}
if (operator != null) {
number_2 = input;
}
if (operator == null) {
number_1 = input;
}
if (button == btn_clean) {
text_screen.setText("0");
number_1 = 0;
number_2 = 0;
operator = null;
}
if (button == btn_backspace) {
text_screen.setText(text_screen.getText().substring (0,text_screen.getText().length () - 1));
if (text_screen.getText ().length ()<=0 ) {
text_screen.setText("0");
}
}
}
public void operationButtons(ActionEvent e){
JButton button = (JButton) e.getSource() ;
if (button == btn_plus) {
addOperator("+");
}else if (button == btn_minus) {
 addOperator("-");
}else if (button == btn_multi) {
 addOperator("*");
}else if (button == btn_divide) {
 addOperator("/");
}else if (button == btn_mod) {
 addOperator("%");
}else if (button == btn_pow) {
 addOperator("pow");
}
}
public void equal(ActionEvent e) {
JButton button = (JButton) e.getSource() ;
if (button ==btn_equal) {
if (operator == "+") {
result = Double.toString(number_1 + number_2);
text_screen.setText(result);
number_1 = Double.parseDouble(result);
}else if (operator == "-") {
result = Double.toString(number_1 - number_2);
text_screen.setText(result);
number_1 = Double.parseDouble(result);
}else if (operator == "*") {
result = Double.toString(number_1 * number_2);
text_screen.setText(result);
number_1 = Double.parseDouble(result);
}else if (operator == "/") {
if (number_2 == 0) {
JOptionPane.showMessageDialog(null, "Can't divide by zero..");
text_screen.setText("0");
numberButtons(e);
}else if (number_2 !=0 ) {
result = Double.toString(number_1 / number_2);
text_screen.setText(result);
number_1 = Double.parseDouble(result);
}
}else if (operator == "%") {
result = Double.toString(number_1 + number_2);
text_screen.setText(result);
number_1 = Double.parseDouble(result);
}else if (operator == "pow") {
input = Math.pow(number_1, number_2);
changeScreen(input);
number_1 = input;
}
input = Double.parseDouble(text_screen.getText());
}
}
}
}


Other 7 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Beginner category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments


 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.