Calculator in Java + Code
We will create one would calculate without interface and another one with interfaceÂ
Basic calculator for novices without interface: D
Making a calculator where we will enter two I number and conducted the operations to add, subtractions, to multiply and to divide.
import javax.swing.*; public class Menu \ { public static void main (String [] agrs) \ { int op=0; double n1, n2, s, m, d, r; do \ { op=Integer.parseInt (JOptionPane.showInputDialog (“nCalculadoran " + “************ n " + “[1] SUMARn " + “[2] RESTARn " + “[3] MULTIPLICARn " + “[4] DIVIDIRn " + “[5] SALIRn " + “An option enters: �)); switch (op) \ { 1 marries: n1=Double.parseDouble (JOptionPane.showInputDialog (“It enters I number 1�)); n2=Double.parseDouble (JOptionPane.showInputDialog (“It enters I number 2�)); s=n1+n2; JOptionPane.showMessageDialog (null, “the sum is: “+s); break; it marries 2: n1=Double.parseDouble (JOptionPane.showInputDialog (“It enters I number 1�)); n2=Double.parseDouble (JOptionPane.showInputDialog (“It enters I number 2�)); r=n1-n2; JOptionPane.showMessageDialog (null, “the subtraction is: “+r); break; it marries 3: n1=Double.parseDouble (JOptionPane.showInputDialog (“It enters I number 1�)); n2=Double.parseDouble (JOptionPane.showInputDialog (“It enters I number 2�)); m=n1*n2; JOptionPane.showMessageDialog (null, “the multiplication is: “+m); break; it marries 4: n1=Double.parseDouble (JOptionPane.showInputDialog (“It enters I number 1�)); n2=Double.parseDouble (JOptionPane.showInputDialog (“It enters I number 2�)); d=n1/n2; JOptionPane.showMessageDialog (null, “the division is: “+d); break; } } while (op! =5); }}
Calculator with Interface in Java
Previously I have shared an example of a basic calculator using the pictures of dialogue in java, which is a little tedious at the time of conducting the operations, but in post it will show something but graphic, using what it has been explained in the previous entrances: To create basic windows, Bellboys, Forms, JPaneles and adjustments of bellboys.
The divided project this in two parts, one which contains the main method, and the other class contains the interface, events and operations conducted by the exercise I practice. Programilla conducts the four basic operations of the mathematics (sum, remains, multiplication and division), furthermore counts on the use of memories, total erasure (screen and memory) square root, and decimal handling. Good already knowing that it realises the exercise, it will continue showing the code for the application.
- CLASS MAIN…
import java.util.logging.Level; import to java.util.logging.Logger; import to javax.swing.UIManager; import javax.swing.UnsupportedLookAndFeelException; public class CalculadoraGUI \ { public static void main (String [] args) \ { String ax = System.getProperty (“os.name�); //variable that takes the name from the operating system that is had installed if (ax.equals (“Windows 7�) || ax.equals (“Windows�))\ {//si is a system Windows is applied skin that has java for Windows try \ { UIManager.setLookAndFeel (“com.sun.java.swing.plaf.windows.WindowsLookAndFeel�); interface w = new interface (); //EVERYTHING code application logic here } catch (ex- ClassNotFoundException) \ { Logger.getLogger (interfaz.class.getName ()).log (Level.SEVERE, null, ex-); } catch (ex- InstantiationException) \ { Logger.getLogger (interfaz.class.getName ()).log (Level.SEVERE, null, ex-); } catch (ex- IllegalAccessException) \ { Logger.getLogger (interfaz.class.getName ()).log (Level.SEVERE, null, ex-); } catch (ex- UnsupportedLookAndFeelException) \ { Logger.getLogger (interfaz.class.getName ()).log (Level.SEVERE, null, ex-); } } if (ax.equals (“Linux�))\ {//si is a system linux is applied skin that has java for linux try \ { UIManager.setLookAndFeel (“com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel�); interface w = new interface (); //EVERYTHING code application logic here } catch (ex- ClassNotFoundException) \ { Logger.getLogger (interfaz.class.getName ()).log (Level.SEVERE, null, ex-); } catch (ex- InstantiationException) \ { Logger.getLogger (interfaz.class.getName ()).log (Level.SEVERE, null, ex-); } catch (ex- IllegalAccessException) \ { Logger.getLogger (interfaz.class.getName ()).log (Level.SEVERE, null, ex-); } catch (ex- UnsupportedLookAndFeelException) \ { Logger.getLogger (interfaz.class.getName ()).log (Level.SEVERE, null, ex-); } } }}
- CLASS INTERFACE…
import java.awt.BorderLayout; import java.awt.Color; import java.awt.Font; import java.awt.Insets; import java.awt.Panel; import java.awt.event.ActionEvent; import to java.awt.event.ActionListener; import javax.swing.BorderFactory; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JTextField; public class interface implements ActionListener \ { JTextField jt1, jt2; Panel pN, pb1, pb3; JPanel pS, pb2; JButton mc, mr, ms, mMas, mMenos, numbers [], operations []; String to oper [] = \ {“R�, “C�, “+�, “/�, “-�, “*�, “=�}, ax= ""; float n1, n2, nr, M; //variables for the operations int tipOp; //para to control the type of operation that is realised they boolean t=false; //control on writing a new one I number after some operation changes true when an operation has been realised public interface () \ { JFrame jfMain = new JFrame (“Calculator�); jfMain.setLayout (new BorderLayout (4, 4)); north (); the south (); jfMain.add (pN, BorderLayout.NORTH); jfMain.add (pS, BorderLayout.CENTER); jfMain.setLocation (100, 80); jfMain.setResizable (false); jfMain.setVisible (true); jfMain.setSize (300, 380); jfMain.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE); } public North void () \ { pN = new Panel (null); jt1 = new JTextField (""); jt2 = new JTextField (“0�); jt1.setHorizontalAlignment (JTextField.RIGHT); jt2.setHorizontalAlignment (JTextField.RIGHT); //Quitar wild to the text fields to jt1.setBorder (BorderFactory.createLineBorder (Color.white)); to jt2.setBorder (BorderFactory.createLineBorder (Color.white)); //desabilitando the text fields jt1.setEditable (false); jt2.setEditable (false); jt1.setBackground (Color.white); jt2.setBackground (Color.white); pN.add (jt1); pN.add (jt2); jt1.setBounds (35, 10, 200, 15); jt2.setBounds (35, 25, 200, 30); pN.setSize (270, 47); pN.setVisible (true); } public South void () \ { pS = new JPanel (new BorderLayout (6, 50)); pS.setLayout (new BorderLayout (4, 4)); botMem (); botNum (); botOpe (); pS.add (pb1, BorderLayout.NORTH); pS.add (pb2, BorderLayout.CENTER); pS.add (pb3, BorderLayout.EAST); pS.setSize (270, 330); } public void botMem () \ { pb1 = new Panel (null); mc = new JButton (“MC�); mr = new JButton (“MR�); ms = new JButton (“MS�); mMas = new JButton (“M+�); mMenos = new JButton (“m�); mc.setFont (new Font (“Arial�, Font.BOLD, 11)); mr.setFont (new Font (“Arial�, Font.BOLD, 11)); ms.setFont (new Font (“Arial�, Font.BOLD, 11)); mMas.setFont (new Font (“Arial�, Font.BOLD, 11)); mMenos.setFont (new Font (“Arial�, Font.BOLD, 11)); mc.setMargin (new Insets (1, 1, 1, 1)); mr.setMargin (new Insets (1, 1, 1, 1)); ms.setMargin (new Insets (1, 1, 1, 1)); mMas.setMargin (new Insets (1, 1, 1, 1)); mMenos.setMargin (new Insets (1, 1, 1, 1)); mc.setBounds (35, 0, 33, 33); mr.setBounds (78, 0, 33, 33); ms.setBounds (121, 0, 33, 33); mMas.setBounds (164, 0, 33, 33); mMenos.setBounds (207, 0, 33, 33); pb1.add (mc); pb1.add (mr); pb1.add (ms); pb1.add (mMas); pb1.add (mMenos); to mc.addActionListener (this); to mr.addActionListener (this); to ms.addActionListener (this); to mMas.addActionListener (this); to mMenos.addActionListener (this); pb1.setSize (270, 45); pb1.setVisible (true); } public void botNum () \ { pb2 = new JPanel (null); int nx3=121, nx2=121, nx1=121, n3y=0, n2y=43, n1y=86; numbers = new JButton [11]; //***************************************** //bloque to create the bellboys, to add them and to assign numbers for (int i=0; i<=10; i++) \ { if (i<=9) \ { numbers [i] = new JButton ("" +i); pb2.add (numbers [i]); numbers [i] .setMargin (new Insets (1, 1, 1, 1)); numbers [i] to.addActionListener (this); } else \ { numbers [i] = new JButton (“. �); pb2.add (numbers [i]); numbers [i] .setMargin (new Insets (1, 1, 1, 1)); numbers [i] to.addActionListener (this); } } //****************************************** //bloque to position bellboys for (int i=10; i>=0; i--) \ { if (i==10) \ { numbers [i] .setBounds (121, 129, 35, 35); } else \ { if (i<=9 && i>=7) \ { numbers [i] .setBounds (nx3, n3y, 35, 35); nx3-=43; } else if (i<=6 && i>=4) \ { n3y+=43; numbers [i] .setBounds (nx2, n2y, 35, 35); nx2-=43; } else if (i<=3 && i>=1) \ { n3y+=43; numbers [i] .setBounds (nx1, n1y, 35, 35); nx1-=43; } else if (i==0) \ { numbers[i] .setBounds (35, 129, 78, 35); } } } pb2.setSize (170, 150); pb2.setVisible (true); } public void botOpe () \ { pb3 = new Panel (null); int c=0, x=0, y=0; operations = new JButton [7]; for (int i=0; i<=6; i++) \ { if (c<=1) \ { operations [i] = new JButton (to oper [i]); pb3.add (operations [i]); operations [i] .setBounds (x, and, 30, 35); operations [i] .setMargin (new Insets (1, 1, 1, 1)); operations [i] to.addActionListener (this); x+=33; c++; } else \ { if (i==6) \ { x=0; y+=43; operations [i] = new JButton (to oper [i]); pb3.add (operations [i]); operations [i] .setBounds (x, and, 65, 35); operations [i] .setMargin (new Insets (1, 1, 1, 1)); operations [i] to.addActionListener (this); x+=33; c++; } else \ { c=0; x=0; y+=43; operations [i] = new JButton (to oper [i]); pb3.add (operations [i]); operations [i] .setBounds (x, and, 30, 35); operations [i] .setMargin (new Insets (1, 1, 1, 1)); operations [i] to.addActionListener (this); x+=33; c++; } } } pb3.setVisible (true); pb3.setSize (120, 200); } public boolean isN (String ax) \ { try \ { int n = Integer.parseInt (ax); return true; } catch (NumberFormatException e) \ { return false; } } @Override public void actionPerformed (ActionEvent e) \ { String op= ""; if (isN (e.getActionCommand ()))\ {//cuando presses numbers if (jt1.getText () .equals (""))\ { ax += e.getActionCommand (); jt2.setText (ax); } else \ { if (tipOp==0) \ { if (t) \ { ax= ""; jt1.setText (jt2.getText ()); ax += e.getActionCommand (); jt2.setText (ax); t = false; } else \ { ax= ""; ax += jt2.getText () +e.getActionCommand (); jt2.setText (ax); } } else \ { ax= ""; ax += jt2.getText () +e.getActionCommand (); jt2.setText (ax); } } } else \ {//cuando presses the rest of bellboys if (e.getActionCommand () .equals (“R�)) \ { jt1.setText (""); Float a = Float.parseFloat (jt2.getText ()); jt2.setText ("" +Math.sqrt (a)); } if (e.getActionCommand () .equals (“C�)) \ {//para to reinitiate values and of cleaning screen tipOp=0; n1 = 0; n2 =0; nr=0; jt1.setText (""); jt2.setText (“0�); ax= ""; } if (e.getActionCommand () .equals (“MC�))\ {//para to clean the memory of the calculator ms.setForeground (Color.black); jt1.setText (""); jt2.setText (“0�); M=0; } if (e.getActionCommand () .equals (“MR�))\ {//para to show value stored in the memory jt1.setText (""); jt2.setText (String.valueOf (M)); } if (e.getActionCommand () .equals (“MS�))\ {to //guardar a value in the memory ms.setForeground (Color.red); M = Float.parseFloat (jt2.getText ()); } if (e.getActionCommand () .equals (“M+�))\ {to //sumar value of the screen with the value of the memory M += Float.parseFloat (jt2.getText ()); } if (e.getActionCommand () .equals (“m�))\ {to //restar value of the screen with the value of the memory M - = Float.parseFloat (jt2.getText ()); } if (e.getActionCommand () .equals (“. �))\ {to //usar the point for the decimal ax= ""; if (10 numbers [] .isEnabled ())\ { 10 numbers [] .setEnabled (false); ax = jt2.getText () +�. �; jt2.setText (ax); } } if (e.getActionCommand () .equals (“+�)) \ {//boton sum 10 numbers [] .setEnabled (true); ax= ""; if (tipOp==1) \ { } else if (tipOp==0) \ {//validacion not to hit other operations if (jt1.getText () .equals ("")) \ { n1 = Float.parseFloat (jt2.getText ()); ax += jt1.getText () +jt2.getText (); jt1.setText (ax+� + “); jt2.setText (""); tipOp = 1; } else \ { if (! t) \ {//validacion for new operation n1 = Float.parseFloat (jt2.getText ()); ax += jt2.getText (); jt1.setText (ax+� + “); jt2.setText (""); tipOp = 1; } else \ {to //usar other operations with the sum n1 = Float.parseFloat (jt2.getText ()); ax += jt1.getText (); jt1.setText (ax+� + “); jt2.setText (""); tipOp = 1; } } } } if (e.getActionCommand () .equals (“-�)) \ {//cuando is decided to remain 10 numbers [] .setEnabled (true); ax= ""; if (tipOp==2) \ { } else if (tipOp==0) \ {//validacion not to hit other operations if (jt1.getText () .equals (""))\ { n1 = Float.parseFloat (jt2.getText ()); ax += jt1.getText () + jt2.getText (); jt1.setText (ax+� - “); jt2.setText (""); tipOp = 2; } else \ { if (! t) \ {//validacion for new operation n1 = Float.parseFloat (jt2.getText ()); ax += jt2.getText (); jt1.setText (ax+� - “); jt2.setText (""); tipOp = 2; } else \ {to //usar other operations with the sum n1 = Float.parseFloat (jt2.getText ()); ax += jt1.getText (); jt1.setText (ax+� - “); jt2.setText (""); tipOp = 2; } } } } if (e.getActionCommand () .equals (“*�)) \ {//cuando is decided to multiply 10 numbers [] .setEnabled (true); ax= ""; if (tipOp==3) \ { } else if (tipOp==0) \ {//validacion not to hit other operations if (jt1.getText () .equals (""))\ { n1 = Float.parseFloat (jt2.getText ()); ax += jt1.getText () +jt2.getText (); jt1.setText (ax+� * “); jt2.setText (""); tipOp = 3; } else \ { if (! t) \ {//validacion for new operation n1 = Float.parseFloat (jt2.getText ()); ax += jt2.getText (); jt1.setText (ax+� * “); jt2.setText (""); tipOp = 3; } else \ {to //usar other operations with the sum n1 = Float.parseFloat (jt2.getText ()); ax += jt1.getText (); jt1.setText (ax+� * “); jt2.setText (""); tipOp = 3; } } } } if (e.getActionCommand () .equals (“/�)) \ {//cuando is decided to divide 10 numbers [] .setEnabled (true); ax= ""; if (tipOp==4) \ { } else if (tipOp==0) \ {//validacion not to hit other operations if (jt1.getText () .equals (""))\ { n1 = Float.parseFloat (jt2.getText ()); ax += jt1.getText () +jt2.getText (); jt1.setText (ax+�/“); jt2.setText (""); tipOp = 4; } else \ { if (! t) \ {//validacion for new operation n1 = Float.parseFloat (jt2.getText ()); ax += jt2.getText (); jt1.setText (ax+�/“); jt2.setText (""); tipOp = 4; } else \ {to //usar other operations with the sum n1 = Float.parseFloat (jt2.getText ()); ax += jt1.getText (); jt1.setText (ax+�/“); jt2.setText (""); tipOp = 4; } } } } if (e.getActionCommand () .equals (“=�) &&! jt2.getText () .equals (""))\ { t = true; if (tipOp==1) \ {//operación for the sum tipOp = 0; ax= ""; ax+=jt1.getText () + jt2.getText (); jt1.setText (ax); n2 = Float.parseFloat (jt2.getText ()); nr=n1+n2; jt2.setText (String.valueOf (nr)); } else if (tipOp==2) \ {//operacion for the subtraction tipOp = 0; ax= ""; ax+=jt1.getText () +jt2.getText (); jt1.setText (ax); n2 = Float.parseFloat (jt2.getText ()); nr=n1-n2; jt2.setText (String.valueOf (nr)); } if (tipOp==3) \ {//operacion for the multiplication tipOp = 0; ax= ""; ax+=jt1.getText () +jt2.getText (); jt1.setText (ax); n2 = Float.parseFloat (jt2.getText ()); nr=n1*n2; jt2.setText (String.valueOf (nr)); } if (tipOp==4) \ {//operacion for the multiplication if (Float.parseFloat (jt2.getText ())! =0) \ { tipOp = 0; ax= ""; ax+=jt1.getText () +jt2.getText (); jt1.setText (ax); n2 = Float.parseFloat (jt2.getText ()); nr=n1/n2; jt2.setText (String.valueOf (nr)); } else JOptionPane.showMessageDialog (null, “cannot be realised divison by 0�); } } } } }
Once executed the application we will see the following thing:
The second class is long, since in this several validations were realised, as also at the time of conducting the operations, the exercise treatment to be realised but possible similarity to the calculator of Windows. I hope that it serves to them.
Source: yahoo