Wednesday, 24 July 2019

Calculator


import android.widget.*;
import android.view.*;
import android.view.View.OnClickListener;
public class MainActivity extends Activity {
EditText t1,t2,t3;
Button b1,b2,b3,b4,b5,b6;
            @Override
            protected void onCreate(Bundle savedInstanceState) {
                        super.onCreate(savedInstanceState);
                        setContentView(R.layout.activity_main);
                        t1=(EditText) findViewById(R.id.editText1);
                        t2=(EditText) findViewById(R.id.editText2);
                        t3=(EditText) findViewById(R.id.editText3);
                        b1=(Button) findViewById(R.id.button1);
                        b2=(Button) findViewById(R.id.button2);
                        b3=(Button) findViewById(R.id.button3);
                        b4=(Button) findViewById(R.id.button4);
                        b5=(Button) findViewById(R.id.button5);
                        b6=(Button) findViewById(R.id.button6);
                        b1.setOnClickListener(s1);
                        b2.setOnClickListener(s1);
                        b3.setOnClickListener(s1);
                        b4.setOnClickListener(s1);
                        b5.setOnClickListener(s1);
                        b6.setOnClickListener(s1);     }
public OnClickListener s1=new OnClickListener()
            {
               public void onClick(View v){
                           if(v==b1){
                           int x=Integer.parseInt(t1.getText().toString());
                           int y=Integer.parseInt(t2.getText().toString());
                           int z=x+y;
                           t3.setText(String.valueOf(z));
                          }
                           if(v==b2){
                                       int x=Integer.parseInt(t1.getText().toString());
                                       int y=Integer.parseInt(t2.getText().toString());
                                       int z=x-y;
                                       t3.setText(String.valueOf(z));
                                      }
                           if(v==b3){
                                       int x=Integer.parseInt(t1.getText().toString());
                                       int y=Integer.parseInt(t2.getText().toString());
                                       int z=x*y;
                                       t3.setText(String.valueOf(z));
                                      }
                           if(v==b4){
                                       int x=Integer.parseInt(t1.getText().toString());
                                       int y=Integer.parseInt(t2.getText().toString());
                                       int z=x/y;
                                       t3.setText(String.valueOf(z));
                                      }
                           if(v==b5){
                                       int x=Integer.parseInt(t1.getText().toString());
                                       int y=Integer.parseInt(t2.getText().toString());
                                       int z=x%y;
                                       t3.setText(String.valueOf(z));
                                      }
                           if(v==b6){
                                       t1.setText("");
                                       t2.setText("");
                                       t3.setText("");
                                      }
               }
             };
}



No comments:

Post a Comment