Saturday, November 1, 2014

Java Example pro



Java Programming Creating a Simple Calculator
import java.util.Scanner;
public class SimpleCalculator
{
 public static void main (String[] args)
{
            int a,b,result=0;
            char c;
Scanner input = new Scanner(System.in);

             System.out.println("Enter no. :");
             a = input.nextInt();
             System.out.println("Enter no. :");
             b = input.nextInt();
             System.out.println("Enter Operator :");
             String st = input.next();
             c = st.charAt(0);
           switch(c)
             {
             case '+':
                 result = a+b;
                 System.out.println("Result = "+result);
                 break;
             case '-':
                 result = a-b;
                 System.out.println("Result = "+result);
                 break;
             case 'x':
                 result = a*b;
                 System.out.println("Result = "+result);
                 break;
             case '/':
                 result = a/b;
                 System.out.println("Result = "+result);
                 break;
             default:
                System.out.println("Syntax Error");
             }
        }
}

Java AWT program for Calculator Application

import java.awt.*;
import java.awt.event.*;
class Calc extends Frame implements ActionListener, TextListener, FocusListener
{
    Label la1,la2,la3;
    TextField te1,te2,te3;
    Button bn1, bn2, bn3, bn4, bn5;
    int y,z,re=0;
    public Calc(String title)
    {
        super(title);
        setLayout(new FlowLayout());
        la1=new Label("Number 1:");//Object creation
        la2=new Label("Number 2:");
        la3=new Label("Result     :");
        te1=new TextField(10);
        te1.addFocusListener(this);//Calling the method
        te2=new TextField(10);
        te3=new TextField(10);
        te3.setEditable(false);
        bn1=new Button("Add");
        bn2=new Button("Sub");
        bn3=new Button("Mul");
        bn4=new Button("Div");
        bn5=new Button("Exit");
        bn1.setEnabled(false);
        bn2.setEnabled(false);
        bn3.setEnabled(false);
        bn4.setEnabled(false);
        bn1.addActionListener(this);
        bn2.addActionListener(this);
        bn3.addActionListener(this);
        bn4.addActionListener(this);
        bn5.addActionListener(this);
        te1.addTextListener(this);
        te2.addTextListener(this);
        add(la1);
        add(te1);
        add(la2);
        add(te2);
        add(la3);
        add(te3);
        add(bu1);
        add(bu2);
        add(bu3);
        add(bu4);
        add(bu5);
    }
    public static void main(String args[])//Main function
    {
        Calc c=new Calc("Calculator");
        c.setSize(200,200);
        c.show();
    }
    public void actionPerformed(ActionEvent e)//Method definition
    {
        x=Integer.parseInt(te1.getText());
        y=Integer.parseInt(te2.getText());
        if(e.getSource()==bu1) //Decision making statement
            re=x+y;
        else if(e.getSource()==bu2)
            re=x-y;
        else if(e.getSource()==bu3)
            re=x*y;
        else if(e.getSource()==bu4)
            re=x/y;
        else if(e.getSource()==bu5)
            System.exit(0);
        te3.setText(String.valueOf(re));
    }
    public void copy()
    {
        x=Integer.parseInt(te1.getText());
        y=Integer.parseInt(te2.getText());
    }
       public void textValueChanged(TextEvent tu)
    {
        if(!te1.getText().equals("")  & (!te2.getText().equals("")))
        {
            bu1.setEnabled(true);
            bu2.setEnabled(true);
            bu3.setEnabled(true);
            bu4.setEnabled(true);
        }
        else
        {
            bu1.setEnabled(false);
            bu2.setEnabled(false);
            bu3.setEnabled(false);
            bu4.setEnabled(false);
        }
    }
    public void focusGained(FocusEvent fe)
    {
        }
    public void focusLost(FocusEvent fe)
    {
        if(te1.getText().equals(""))
        {
            javax.swing.JOptionPane.showMessageDialog(this,"The field should not be left blank");
        }
    }

}

No comments:

Post a Comment