Breaking News
Loading...
Monday, 15 February 2016

calculator using runtime designing/console using two textfields (s5)

07:27:00
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;


public class CalculatorForm extends JFrame implements ActionListener{
            JButton btnplus ,btnMinus,btnmultiply,btndivide,btnsin,btncos,btntan,btnlog;
           
          JTextField txtval1,txtval2;
            Calculator obj=new Calculator();

     public CalculatorForm()
    {
        super("my calculator"); 
        //setSize(200,300);it will run first instead obj
        //import should use
        Container c = getContentPane();
        //import will use
         txtval1 = new JTextField(20);
         txtval2 = new JTextField(20);
             btnplus = new JButton("+");
             btnplus.addActionListener(this);
             btnMinus = new JButton("-");
             btnMinus.addActionListener(this);
             btnmultiply= new JButton("*");
             btnmultiply.addActionListener(this);
             btndivide=new JButton("/");
             btndivide.addActionListener(this);
             btnsin=new JButton("Sin");
             btnsin.addActionListener(this);
             btncos=new JButton("Cos");
             btncos.addActionListener(this);
             btntan=new JButton("Tan");
             btntan.addActionListener(this);
             btnlog=new JButton("log");
             btnlog.addActionListener(this);
        //import will use
        JPanel p = new JPanel();
         p.add(txtval1);
         p.add(txtval2);
         p.add(btnplus);        
         p.add(btnMinus);
         p.add(btnmultiply);
         p.add(btndivide);
         p.add(btnsin);
         p.add(btncos);
         p.add(btntan);
         p.add(btnlog);
   
 c.add(p);
  
    }
    public static void main(String args[]){
    CalculatorForm obj=new CalculatorForm();
    obj.setVisible(true);
    obj.setSize(250,350);
    obj.setDefaultCloseOperation(3);
   
    }

    @Override
    public void actionPerformed(ActionEvent ae) {
        if(ae.getSource()== btnplus)
        {
         int a=Integer.parseInt(txtval1.getText());
         int b=Integer.parseInt(txtval2.getText());
         //int c=a+b;
         int c=obj.sum(a,b);
     System.out.println("sum is="+c );
         txtval1.setText(""+c);
         txtval2.setText("");
        } else if (ae.getSource()==btnMinus){
          int a=Integer.parseInt(txtval1.getText());
         int b=Integer.parseInt(txtval2.getText());
         //int c=a-b;
         int c=obj.subtraction (a,b);
         txtval1.setText(""+c);
         //empty value from field 2
         txtval2.setText("");
         }else if(ae.getSource()==btnmultiply){
         int a=Integer.parseInt(txtval1.getText());
         int b=Integer.parseInt(txtval2.getText());
         int c=a*b;
        txtval1.setText(""+c); 
        txtval2.setText("");
  
     }
           if(ae.getSource()==btndivide)
     {
         int val1=Integer.parseInt(txtval1.getText());
         int val2=Integer.parseInt(txtval2.getText());
         int c=val1/val2;
        txtval1.setText(""+c); 
        txtval2.setText("");
     }
            if(ae.getSource()==btnsin)
     {
         double d=Double.parseDouble(txtval1.getText());
         double val1=Math.sin(d);
         double c=val1;
        txtval1.setText(""+c); 
        txtval2.setText("");
    } else if(ae.getSource()==btncos)
     {
         double a=Double.parseDouble(txtval1.getText());
         double v=Math.toRadians(a);
          double  val1=Math.cos(v);
          double c=val1;
        txtval1.setText(""+c); 
        txtval2.setText("");
     }else if(ae.getSource()==btntan){
         double d=Double.parseDouble(txtval1.getText());
         double val1=Math.sin(d);
         double c=val1;
        txtval1.setText(""+c); 
        txtval2.setText("");
    }else if(ae.getSource()==btnlog){
         double d=Double.parseDouble(txtval1.getText());
         double val1=Math.log(d);
         double c=val1;
        txtval1.setText(""+c); 
        txtval2.setText("");
    }
     
        }
      
    }

0 comments:

Post a Comment

 
Toggle Footer