Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.example.lab04;
- import static java.lang.Math.abs;
- import static java.lang.Math.log10;
- import static java.lang.Math.pow;
- import static java.lang.Math.sqrt;
- import androidx.appcompat.app.AppCompatActivity;
- import android.annotation.SuppressLint;
- import android.os.Bundle;
- import android.view.View;
- import android.widget.Button;
- import android.widget.TextView;
- import org.w3c.dom.Text;
- public class MainActivity extends AppCompatActivity {
- boolean isNewOperator = true;
- TextView ResultTV;
- TextView WorkingsTV;
- String operator = "";
- String workings = "";
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- WorkingsTV = findViewById(R.id.workings);
- ResultTV = findViewById(R.id.result);
- }
- public void clickNumber(View view) {
- if (isNewOperator) {
- WorkingsTV.setText("");
- }
- isNewOperator = false;
- String number = WorkingsTV.getText().toString();
- switch (view.getId()) {
- case R.id.oneButton:
- number += "1";
- break;
- case R.id.twoButton:
- number += "2";
- break;
- case R.id.threeButton:
- number += "3";
- break;
- case R.id.fourButton:
- number += "4";
- break;
- case R.id.fiveButton:
- number += "5";
- break;
- case R.id.sixButton:
- number += "6";
- break;
- case R.id.sevenButton:
- number += "7";
- break;
- case R.id.eightButton:
- number += "8";
- break;
- case R.id.nineButton:
- number += "9";
- break;
- case R.id.zeroButton:
- number += "0";
- break;
- case R.id.minusButton:
- if(Double.parseDouble(number) > 0.0) {
- number = "-" + number;
- break;
- } else {
- number = String.valueOf(abs(Double.parseDouble(number)));
- break;
- }
- case R.id.commaButton:
- number += ".";
- break;
- }
- WorkingsTV.setText(number);
- }
- public void clickOperator(View view) {
- isNewOperator = true;
- workings = WorkingsTV.getText().toString();
- switch (view.getId()) {
- case R.id.sumButton:
- operator = "+";
- break;
- case R.id.subButton:
- operator = "-";
- break;
- case R.id.mulButton:
- operator = "x";
- break;
- case R.id.divButton:
- operator = "/";
- break;
- case R.id.logButton:
- operator = "log10";
- break;
- case R.id.sqrtButton:
- operator = "SQRT";
- break;
- case R.id.cuberootButton:
- operator = "^3";
- break;
- case R.id.squarerootButton:
- operator = "^2";
- break;
- case R.id.factorialButton:
- operator = "x!";
- break;
- }
- WorkingsTV.setText(operator);
- }
- public void showResult(View view) {
- String value = WorkingsTV.getText().toString();
- double result = 0.0;
- switch (operator) {
- case "+":
- result = Double.parseDouble(workings) + Double.parseDouble(value);
- break;
- case "-":
- result = Double.parseDouble(workings) - Double.parseDouble(value);
- break;
- case "x":
- result = Double.parseDouble(workings) * Double.parseDouble(value);
- break;
- case "/":
- result = Double.parseDouble(workings) / Double.parseDouble(value);
- break;
- case "SQRT":
- result = sqrt(Double.parseDouble(workings));
- break;
- case "^3":
- result = pow(Double.parseDouble(workings),3);
- break;
- case "^2":
- result = pow(Double.parseDouble(workings),2);
- break;
- case "log10":
- result = log10(Double.parseDouble(workings));
- break;
- case "x!":
- result = factorial(Double.parseDouble(workings));
- break;
- }
- ResultTV.setText(result + "");
- WorkingsTV.setText("");
- workings = "";
- }
- public double factorial(double num){
- double fact = 1;
- for(int i = 1; i <= num; i++) {
- fact *= i;
- }
- return fact;
- }
- public void allClear(View view) {
- WorkingsTV.setText("");
- ResultTV.setText("0");
- isNewOperator = true;
- }
- public void countPerc(View view) {
- double result = Double.parseDouble(WorkingsTV.getText().toString()) / 100;
- WorkingsTV.setText("%");
- ResultTV.setText(result + "");
- isNewOperator = true;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement