Advertisement
Guest User

MainActivity.java

a guest
Feb 22nd, 2016
36,411
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.85 KB | None | 0 0
  1. package com.okedroid.aplikasisaya;
  2.  
  3. import android.os.Bundle;
  4. import android.support.v7.app.AppCompatActivity;
  5. import android.view.View;
  6. import android.widget.EditText;
  7.  
  8.  
  9. public class MainActivity extends AppCompatActivity {
  10.  
  11.     public String str ="";
  12.     Character op = 'q';
  13.     float i,num,numtemp;
  14.     EditText showResult;
  15.     @Override
  16.     public void onCreate(Bundle savedInstanceState) {
  17.         super.onCreate(savedInstanceState);
  18.         setContentView(R.layout.activity_main);
  19.  
  20.         showResult = (EditText)findViewById(R.id.result_id);
  21.  
  22.     }
  23.     public void btn1Clicked(View v){
  24.         insert(1);
  25.  
  26.     }
  27.  
  28.     public void btn2Clicked(View v){
  29.         insert(2);
  30.  
  31.     }
  32.     public void btn3Clicked(View v){
  33.         insert(3);
  34.  
  35.     }
  36.     public void btn4Clicked(View v){
  37.         insert(4);
  38.  
  39.     }
  40.     public void btn5Clicked(View v){
  41.         insert(5);
  42.  
  43.     }
  44.     public void btn6Clicked(View v){
  45.         insert(6);
  46.     }
  47.     public void btn7Clicked(View v){
  48.         insert(7);
  49.  
  50.     }
  51.     public void btn8Clicked(View v){
  52.         insert(8);
  53.  
  54.     }
  55.     public void btn9Clicked(View v){
  56.         insert(9);
  57.  
  58.     }
  59.     public void btnplusClicked(View v){
  60.         perform();
  61.         op = '+';
  62.  
  63.     }
  64.  
  65.     public void btnminusClicked(View v){
  66.         perform();
  67.         op = '-';
  68.  
  69.     }
  70.     public void btndivideClicked(View v){
  71.         perform();
  72.         op = '/';
  73.  
  74.     }
  75.     public void btnmultiClicked(View v){
  76.         perform();
  77.         op = '*';
  78.  
  79.     }
  80.     public void btnequalClicked(View v){
  81.         calculate();
  82.  
  83.     }
  84.  
  85.     public void btnclearClicked(View v){
  86.         reset();
  87.     }
  88.     private void reset() {
  89.         // TODO Auto-generated method stub
  90.         str ="";
  91.         op ='q';
  92.         num = 0;
  93.         numtemp = 0;
  94.         showResult.setText("");
  95.     }
  96.     private void insert(int j) {
  97.         // TODO Auto-generated method stub
  98.         str = str+Integer.toString(j);
  99.         num = Integer.valueOf(str).intValue();
  100.         showResult.setText(str);
  101.  
  102.     }
  103.     private void perform() {
  104.         // TODO Auto-generated method stub
  105.         str = "";
  106.         calculateNoShow();
  107.         numtemp = num;
  108.  
  109.     }
  110.     private void calculate() {
  111.         // TODO Auto-generated method stub
  112.         if(op == '+')
  113.             num = numtemp+num;
  114.         else if(op == '-')
  115.             num = numtemp-num;
  116.         else if(op == '/')
  117.             num = numtemp/num;
  118.         else if(op == '*')
  119.             num = numtemp*num;
  120.         showResult.setText(""+num);
  121.     }
  122.  
  123.     private void calculateNoShow() {
  124.         // TODO Auto-generated method stub
  125.         if(op == '+')
  126.             num = numtemp+num;
  127.         else if(op == '-')
  128.             num = numtemp-num;
  129.         else if(op == '/')
  130.             num = numtemp/num;
  131.         else if(op == '*')
  132.             num = numtemp*num;
  133.     }
  134. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement