A simple calculator using java

Source: Internet
Author: User
Use java to create a simple calculator-general Linux technology-Linux programming and kernel information. The following is a detailed description. There are two classes in total. It only completes the +,-, X, and merge operations.

The GUI only uses AWT, which is easy to understand.
Calculator. java

Public class Calculator {
Private String result = "0 ";
Private int op = 0, add = 1, sub = 2, mul = 3, div = 4;

Private double stringToDouble (String x ){
Double y = Double. parseDouble (x );
Return y;
}
Private void operate (String x ){
Double x1 = stringToDouble (x );
Double y = stringToDouble (result );
Switch (op ){
Case 0:
Result = x;
Break;
Case 1:
Result = String. valueOf (y + x1 );
Break;
Case 2:
Result = String. valueOf (y-x1 );
Break;
Case 3:
Result = String. valueOf (y * x1 );
Break;
Case 4:
If (x1! = 0 ){
Result = String. valueOf (y/x1 );
} Else {
Result = "The divisor can't be zero! ";
}
Break;
}
}

Public String opAdd (String x ){
Operate (x );
Op = add;
Return result;
}
Public String opSubtract (String x ){
Operate (x );
Op = sub;
Return result;
}
Public String opMultiply (String x ){
Operate (x );
Op = mul;
Return result;
}
Public String opDivide (String x ){
Operate (x );
Op = div;
Return result;
}
Public String opEquals (String x ){
Operate (x );
Op = 0;
Return result;
}
Public void opClean (){
Op = 0;
Result = "0 ";
}
}

-------------------------------------------------------------------

Second
CalculatorGUI. java

Import java. awt .*;
Import java. awt. event .*;
Import java. util. EventObject;

Public class CalculatorGUI {
Private Frame f;
Private Panel p1, p2;
Private Button b0, b1, b2, b3, b4, b5, b6, b7, b8, b9;
Private Button bPoint, bAdd, bDec, bMul, bDiv, bCal;
Private TextField tf;
Private String s, op;
Private Calculator cal = new Calculator ();
Private boolean ifOp;

Public CalculatorGUI (){
F = new Frame ("Calculator ");
P1 = new Panel ();
P2 = new Panel ();

B0 = new Button ("0 ");
B1 = new Button ("1 ");
B2 = new Button ("2 ");
B3 = new Button ("3 ");
B4 = new Button ("4 ");
B5 = new Button ("5 ");
B6 = new Button ("6 ");
B7 = new Button ("7 ");
B8 = new Button ("8 ");
B9 = new Button ("9 ");
BPoint = new Button (".");
BAdd = new Button ("+ ");
BDec = new Button ("-");
BMul = new Button ("*");
BDiv = new Button ("/");
BCal = new Button ("= ");

Tf = new TextField (25 );
Tf. setEditable (false );


}

Public void launchFrame (){
F. setSize (220,160 );
F. setResizable (false );
F. addWindowListener (new myWindowListener ());
P1.setLayout (new FlowLayout (FlowLayout. CENTER ));
P1.add (tf );
F. add (p1, BorderLayout. NORTH );
P2.setLayout (new GridLayout (4, 4 ));

B0.addActionListener (new setLabelText_ActionListener ());
B1.addActionListener (new setLabelText_ActionListener ());
B2.addActionListener (new setLabelText_ActionListener ());
B3.addActionListener (new setLabelText_ActionListener ());
B4.addActionListener (new setLabelText_ActionListener ());
B5.addActionListener (new setLabelText_ActionListener ());
B6.addActionListener (new setLabelText_ActionListener ());
B7.addActionListener (new setLabelText_ActionListener ());
B8.addActionListener (new setLabelText_ActionListener ());
B9.addActionListener (new setLabelText_ActionListener ());
BPoint. addActionListener (new setLabelText_ActionListener ());
BAdd. addActionListener (new setOperator_ActionListener ());
BDec. addActionListener (new setOperator_ActionListener ());
BMul. addActionListener (new setOperator_ActionListener ());
BDiv. addActionListener (new setOperator_ActionListener ());
BCal. addActionListener (new setOperator_ActionListener ());

P2.add (b7 );
P2.add (b8 );
P2.add (b9 );
P2.add (bAdd );
P2.add (b4 );
P2.add (b5 );
P2.add (b6 );
P2.add (bDec );
P2.add (b1 );
P2.add (b2 );
P2.add (b3 );
P2.add (bMul );
P2.add (b0 );
P2.add (bPoint );
P2.add (bCal );
P2.add (bDiv );
F. add (p2, BorderLayout. SOUTH );
F. setVisible (true );
}

Public void setTextFieldText_Temp (){
If (tf. getText (). length () <15 & (tf. getText (). indexOf (".") =-1 |! S. equals ("."))){
Tf. setText (tf. getText () + s );
} Else {
Tf. setText (tf. getText () + s). substring (0, 15 ));
}
}
Public void setTextFieldText (){
If (ifOp ){
IfOp = false;
Tf. setText ("");
SetTextFieldText_Temp ();
} Else {
SetTextFieldText_Temp ();
}
}

Public static void main (String [] args ){
CalculatorGUI calculator = new CalculatorGUI ();
Calculator. launchFrame ();
}

Class myWindowListener extends WindowAdapter {
Public void windowClosing (WindowEvent e ){
System. exit (0 );
}
}

Class setLabelText_ActionListener implements ActionListener {
Public void actionreceivmed (ActionEvent e ){
Button tempB = (Button) e. getSource ();
S = tempB. getLabel ();
SetTextFieldText ();
}
}

Class setOperator_ActionListener implements ActionListener {
Public void actionreceivmed (ActionEvent e ){
Button tempB = (Button) e. getSource ();
Op = tempB. getLabel ();
If (op. equals ("+ ")){
Tf. setText (cal. opAdd (tf. getText ()));
IfOp = true;
} Else if (op. equals ("-")){
Tf. setText (cal. opSubtract (tf. getText ()));
IfOp = true;
} Else if (op. equals ("*")){
Tf. setText (cal. opMultiply (tf. getText ()));
IfOp = true;
} Else if (op. equals ("/")){
Tf. setText (cal. opDivide (tf. getText ()));
IfOp = true;
} Else if (op. equals ("= ")){
Tf. setText (cal. opEquals (tf. getText ()));
IfOp = true;
}
}
}
}
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.