Thursday, November 14, 2013

NORMAL CALCULATOR


package normalcalc;

import java.applet.Applet;
import java.awt.Button;
import java.awt.Label;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 *
 * @author Karthi
 */
public class Normalcalc extends Applet implements ActionListener
{
    Label l1=new Label("A");
    Label l2=new Label("B");
   Label l3=new Label("Result");
    TextField t1=new TextField(20);
    TextField t2=new TextField(20);
    TextField t3=new TextField(20);
    Button b1=new Button("  +  ");
    Button b2=new Button("  -  ");
    Button b3=new Button("  *  ");
    Button b4=new Button("  /  ");
    public void init()
    {
        add(l1);
        add(t1);
        add(l2);
        add(t2);
        add(l3);
        add(t3);
        add(b1);
        add(b2);
        add(b3);
        add(b4);
        t1.addActionListener(this);
        t2.addActionListener(this);
      //t3.addActionListener(this);
        b1.addActionListener(this);
        b2.addActionListener(this);
        b3.addActionListener(this);
        b4.addActionListener(this);
     
    }
    public void actionPerformed(ActionEvent e)
    {
        int a,b,c;
        String str;
        str=t1.getText();
        a=Integer.parseInt(str);
        str=t2.getText();
        b=Integer.parseInt(str);
        if(e.getSource()==b1)
        {
         c=a+b;
         t3.setText(String.valueOf(c));
        }
        if(e.getSource()==b2)
        {
         c=a-b;
         t3.setText(String.valueOf(c));
        }
        if(e.getSource()==b3)
        {
         c=a*b;
         t3.setText(String.valueOf(c));
        }
        if(e.getSource()==b4)
        {
         c=a/b;
         t3.setText(String.valueOf(c));
        }
    }

 
}

GENERAL CASE IN JAVA USING APPLET



import java.lang.*;
import java.applet.Applet;
import java.awt.Button;
import java.awt.Label;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 *
 * @author Karthi
 */
public class Stringapplet extends Applet implements ActionListener
{
    Label l1=new Label("Input String");
    Label l2=new Label("Output String");
    TextField t1=new TextField(20);
    TextField t2=new TextField(20);
    Button b1=new Button("Upper Case");
    Button b2=new Button("Lower Case");
    Button b3=new Button("General Case");
     public void init()
    {
        add(l1);
        add(t1);
        add(l2);
        add(t2);
        add(b1);
        add(b2);
        add(b3);
        t1.addActionListener(this);
        b1.addActionListener(this);
        b2.addActionListener(this);
        b3.addActionListener(this);
     
    }
    public void actionPerformed(ActionEvent e)
    {
        String a,s1;
        if(e.getSource()==b1)
        {
        a=t1.getText();
        a = a.toUpperCase();
        t2.setText(a);
        }
        if(e.getSource()==b2)
        {
         a=t1.getText();
         a = a.toLowerCase();
         t2.setText(a);
        }
        if(e.getSource()==b3)
        {
          StringBuffer sb=new StringBuffer(t1.getText());
          int i=0;
          s1="";
          a=String.valueOf(sb.charAt(i));
          s1=s1+a.toUpperCase();
          for(i=1;i<sb.length();i++)
          {
              if(sb.charAt(i)!=' ')
              {
                  a=String.valueOf(sb.charAt(i));
                  s1=s1+a.toLowerCase();
               
              }
              else
              {
                  s1=s1+' ';
                  i=i+1;
                  a=String.valueOf(sb.charAt(i));
                  s1=s1+a.toUpperCase();
              }
          }
          t2.setText(s1);
                }
            }
}