Code.GeekInterview.com
 
 

Java Code Base

 
Code Samples Java
 

Simple Calculator


Code ResourceAuthor: srinivasaraobora  

Difficulty Level:

Published: 7th Nov 2006   Read: 9685 times  

Filed in: Java
Add Comment


 

 

Sponsored Links


 

 

This is useful for airthmetic basic operrations, and this is useful for how to use AWT ,Swings just compile like this javac Sricalc.java java Sricalc.

 


Sample Code
  1.  
  2. //Author:Srinivas.b
  3. //version:java1.5
  4. //This program is for Calculator.
  5. import java.awt.*;
  6. import java.awt.event.*;
  7. import javax.swing.*;
  8. import javax.swing.event.*;
  9. class Sricalc
  10. {
  11.  public static void main(String[] args)
  12.         {
  13.                 Mycalc1 m = new Mycalc1();
  14.                 m.setSize(350,300);
  15.                 m.setTitle("My Own Calculator");
  16.                 m.show();
  17.         }
  18. }
  19. class Mycalc1 extends JFrame  
  20. {
  21.          boolean   startNumber = true;  
  22.      String    previousOp  = "=";  
  23.      CalcLogic2 l = new CalcLogic2();
  24.  
  25.         JButton b1,b2,b3,b4,b5,b6,b7,b8,b9,c1,c2,c3,c4,c5,c6,c7;
  26.         JTextField no1;
  27.    
  28.         Mycalc1()
  29.         {
  30.             Container c = getContentPane();
  31.             c.setBackground(new Color(50,70,90));
  32.             c.setLayout(null);
  33.                
  34.         ActionListener numListener = new NumListener();
  35.         ActionListener opListener = new OpListener();
  36.                 no1 = new JTextField();
  37.         no1.setHorizontalAlignment(JTextField.RIGHT);
  38.         no1.setBackground(new Color(0,45,30));
  39.         no1.setForeground(Color.white);
  40.                 no1.setBounds(150,70,180,20);
  41.         c.add(no1);
  42.         b1 = new JButton("1");
  43.             b1.setBackground(Color.green);
  44.             b1.setForeground(Color.red);
  45.         b1.setBounds(100,100,50,20);
  46.                 b2 = new JButton("2");
  47.             b2.setBackground(Color.white);
  48.             b2.setForeground(Color.blue);
  49.         b2.setBounds(160,100,50,20);
  50.         b3 = new JButton("3");
  51.             b3.setBackground(Color.yellow);
  52.             b3.setForeground(Color.black);
  53.         b3.setBounds(220,100,50,20);
  54.         b4 = new JButton("4");
  55.             b4.setBackground(Color.black);
  56.             b4.setForeground(Color.red);
  57.         b4.setBounds(280,100,50,20);
  58.         b5 = new JButton("5");
  59.             b5.setBackground(Color.black);
  60.             b5.setForeground(Color.blue);
  61.         b5.setBounds(100,130,50,20);
  62.         b6 = new JButton("6");
  63.             b6.setBackground(Color.yellow);
  64.             b6.setForeground(Color.green);
  65.         b6.setBounds(160,130,50,20);
  66.         b7 = new JButton("7");
  67.             b7.setBackground(Color.blue);
  68.             b7.setForeground(Color.yellow);
  69.         b7.setBounds(220,130,50,20);
  70.         b8 = new JButton("8");
  71.             b8.setBackground(Color.white);
  72.             b8.setForeground(Color.green);
  73.         b8.setBounds(280,130,50,20);
  74.         b9 = new JButton("9");
  75.             b9.setBackground(Color.red);
  76.             b9.setForeground(Color.yellow);
  77.         b9.setBounds(100,160,50,20);
  78.                 c1 = new JButton("0");
  79.             c1.setBackground(Color.black);
  80.             c1.setForeground(Color.yellow);
  81.         c1.setBounds(160,160,50,20);
  82.         c2 = new JButton("=");
  83.         c2.setBounds(220,160,50,20);
  84.                 c3 = new JButton("CE");
  85.         c3.setBackground(Color.black);
  86.         c3.setForeground(Color.blue);
  87.         c3.setBounds(280,160,50,20);
  88.                 c4 = new JButton("+");
  89.         c4.setBounds(100,190,50,20);
  90.         c5 = new JButton("-");
  91.         c5.setBounds(160,190,50,20);
  92.                 c6 = new JButton("*");
  93.         c6.setBounds(220,190,50,20);
  94.                 c7 = new JButton("/");
  95.         c7.setBounds(280,190,50,20);
  96.        
  97.        
  98.        
  99.                 c.add(b1);
  100.                 c.add(b2);
  101.                 c.add(b3);
  102.                 c.add(b4);
  103.                 c.add(b5);
  104.         c.add(b6);
  105.         c.add(b7);
  106.                 c.add(b8);
  107.                 c.add(b9);
  108.                 c.add(c1);
  109.                 c.add(c2);
  110.         c.add(c3);
  111.         c.add(c4);
  112.         c.add(c5);
  113.                 c.add(c6);
  114.                 c.add(c7);
  115.        
  116.        
  117.         b1.addActionListener(numListener);
  118.                 b2.addActionListener(numListener);
  119.                 b3.addActionListener(numListener);
  120.         b4.addActionListener(numListener);
  121.         b5.addActionListener(numListener);
  122.                 b6.addActionListener(numListener);
  123.                 b7.addActionListener(numListener);
  124.         b8.addActionListener(numListener);
  125.         b9.addActionListener(numListener);
  126.                 c1.addActionListener(numListener);
  127.                 c2.addActionListener(opListener);
  128.         c3.addActionListener(new ClearListener());
  129.         c4.addActionListener(opListener);
  130.                 c5.addActionListener(opListener);
  131.                 c6.addActionListener(opListener);
  132.         c7.addActionListener(opListener);
  133.        
  134.                
  135.                 no1.setText("0");
  136.                 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  137.         }
  138.          public void action_clear()
  139.     {
  140.         startNumber = true;        
  141.         no1.setText("0");
  142.         previousOp  = "=";
  143.         l.setTotal("0");
  144.     }
  145.  
  146.    //This Listener for operators
  147.     class OpListener implements ActionListener
  148.     {
  149.         public void actionPerformed(ActionEvent e)
  150.         {
  151.            
  152.             if (startNumber)
  153.             {
  154.                 action_clear();
  155.                 no1.setText("ERROR");
  156.             }
  157.             else
  158.             {
  159.                 startNumber = true;
  160.                 try {
  161.                    
  162.                     String str = no1.getText();
  163.    
  164.                     if (previousOp.equals("="))
  165.                     {
  166.                         l.setTotal(str);
  167.                     }  
  168.                     else if (previousOp.equals("+"))
  169.                     {
  170.                         l.add(str);
  171.                     }
  172.                     else if (previousOp.equals("-"))
  173.                     {
  174.                         l.subtract(str);
  175.                     }
  176.                     else if (previousOp.equals("*"))
  177.                     {
  178.                         l.multiply(str);
  179.                     }
  180.                     else if (previousOp.equals("/"))
  181.                     {
  182.                         l.divide(str);
  183.                     }
  184.                  
  185.                     no1.setText("" + l.getTotalString());
  186.    
  187.                 }
  188.                 catch (NumberFormatException ex) {
  189.                     action_clear();
  190.                     no1.setText("Error");
  191.                 }
  192.    
  193.              
  194.                 previousOp = e.getActionCommand();
  195.             }
  196.         }
  197.     }
  198.    
  199.  
  200.     // This Listener is used For set number's in to Textfield
  201.     class NumListener implements ActionListener
  202.     {
  203.         public void actionPerformed(ActionEvent e)
  204.         {
  205.             String str1 = e.getActionCommand();
  206.             if (startNumber)
  207.             {
  208.                
  209.                 no1.setText(str1);
  210.                 startNumber = false;
  211.             }
  212.             else
  213.             {
  214.                
  215.                 no1.setText(no1.getText() + str1);
  216.             }
  217.         }
  218.     }
  219.    
  220.    
  221.    //This Listener is Clear the textfield.
  222.     class ClearListener implements ActionListener
  223.     {
  224.         public void actionPerformed(ActionEvent e)
  225.         {
  226.             action_clear();
  227.         }
  228.     }
  229. }
  230.  
  231.  class CalcLogic2
  232. {
  233.    
  234.    
  235.     private float total;  
  236.    
  237.    
  238.     public CalcLogic2()
  239.     {
  240.         total = 0;
  241.     }
  242.    
  243.     public String getTotalString()
  244.     {
  245.         return ""+total;
  246.     }
  247.    
  248.     public void setTotal(String n)
  249.     {
  250.         total = Integer.parseInt(n);
  251.     }
  252.    
  253.     public void add(String n)
  254.     {
  255.         total += Integer.parseInt(n);
  256.     }
  257.    
  258.     public void subtract(String n)
  259.     {
  260.         total -= Integer.parseInt(n);
  261.     }
  262.    
  263.     public void multiply(String n)
  264.     {
  265.         total *= Integer.parseInt(n);
  266.     }
  267.    
  268.    public void divide(String n)
  269.     {
  270.         total /= Integer.parseInt(n);
  271.     }
  272. }  
  273.  
Copyright GeekInterview.com


Next Article: Random Password Generation function


 

Latest Code Samples

 

Popular Code Samples

 

Related Code Samples

 

Post Your Comment:

Members Please Login
Your Name:*
e-mail ID:(required for notification)*
Image Verification: 
 
 Subscribe    



Comments

it is very nice
Comment posted by: lenin on 2006-11-08T11:46:25
Very useful for me.
Comment posted by: M.Vignesh on 2010-09-29T07:32:20

Popular Coders

# Coder NameHits
1. srinivasaraobora130587
2. iamdvr97637
3. venki_madesh35132
4. Raju18459
5. parmod kumar duhan15743
6. Kiran.jakkaraju14037
7. chowsys7450
8. Venkateswara Rao6469
9. ashish.cns6342
10. Vamshidhar Matam5424

Active Coders

Refined Tags