Use of CheckBox in Android Studio

By this we can make multiple calculation function and many more thing.

For example- To find simple interest and compound interest with the help of CheckBox.

Program is-

public class MainActivity extends AppCompatActivity {
    EditText e1, e2, e3, e4;
    private CheckBox cb1, cb2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        e1= (EditText) findViewById(R.id.e1);
        e2= (EditText) findViewById(R.id.e2);
        e3= (EditText) findViewById(R.id.e3);
        e4= (EditText) findViewById(R.id.e4);
        cb1= (CheckBox) findViewById(R.id.cb1);
        cb2= (CheckBox) findViewById(R.id.cb2);
    }

    public void Perform(View view) {
        try {
            double result = 0.0;
            int p = Integer.parseInt("" + e1.getText());
            int r = Integer.parseInt("" + e2.getText());
            int t = Integer.parseInt("" + e3.getText());

            if (cb1.isChecked()) {

                result = (p * r * t) / 100.0;
            }
            if (cb2.isChecked()) {
                double A = p * Math.pow((1 + r / 100.0), t);
                result = A - p;
            }
            if (cb1.isChecked()&cb2.isChecked()) {
                double result1 = (p * r * t) / 100.0;
                double A = p * Math.pow((1 + r / 100.0), t);
                double result2 = A - p;
                result= result1+result2;
            }
            e4.setText("" + result);

        } catch (Exception ex) {
            System.out.println(ex);
            e4.setText(ex.getMessage());
        }
    }
}

REsult-

Leave a Reply