KEMBAR78
Lab2 SimpleCalculator | PDF | Computing | Software
0% found this document useful (0 votes)
19 views3 pages

Lab2 SimpleCalculator

The document is a Java code for a simple calculator application using Android's AppCompatActivity. It defines buttons for digits, operations, and functionalities to handle user input and perform calculations based on the input. The app displays results in an EditText field and shows error messages for invalid inputs using Toast notifications.

Uploaded by

ajiet.headstart
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views3 pages

Lab2 SimpleCalculator

The document is a Java code for a simple calculator application using Android's AppCompatActivity. It defines buttons for digits, operations, and functionalities to handle user input and perform calculations based on the input. The app displays results in an EditText field and shows error messages for invalid inputs using Toast notifications.

Uploaded by

ajiet.headstart
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

/* MainActivity.

java */

package com.example.calculator;

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import java.util.regex.Pattern;
public class MainActivity extends AppCompatActivity implements View.OnClickListener
{
Button one, two, three, four, five, six;
Button seven, eight, nine, zero;
Button adds, subs, muls, divs;
Button clears, equals, dots;
EditText result;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

one = (Button) findViewById(R.id.button_one);


one.setOnClickListener(this);

two = (Button) findViewById(R.id.button_two);


two.setOnClickListener(this);

three = (Button) findViewById(R.id.button_three);


three.setOnClickListener(this);

four = (Button) findViewById(R.id.button_four);


four.setOnClickListener(this);

five = (Button) findViewById(R.id.button_five);


five.setOnClickListener(this);

six = (Button) findViewById(R.id.button_six);


six.setOnClickListener(this);

seven = (Button) findViewById(R.id.button_seven);


seven.setOnClickListener(this);

eight = (Button) findViewById(R.id.button_eight);


eight.setOnClickListener(this);

nine = (Button) findViewById(R.id.button_nine);


nine.setOnClickListener(this);

zero = (Button) findViewById(R.id.button_zero);


zero.setOnClickListener(this);

adds = (Button) findViewById(R.id.button_add);


adds.setOnClickListener(this);

subs = (Button) findViewById(R.id.button_sub);


subs.setOnClickListener(this);
muls = (Button) findViewById(R.id.button_mul);
muls.setOnClickListener(this);

divs = (Button) findViewById(R.id.button_div);


divs.setOnClickListener(this);

clears = (Button) findViewById(R.id.button_clear);


clears.setOnClickListener(this);

equals = (Button) findViewById(R.id.button_equal);


equals.setOnClickListener(this);

dots = (Button) findViewById(R.id.button_dot);


dots.setOnClickListener(this);

result = (EditText) findViewById(R.id.txt_result);


result.setText("");
}
public void onClick(View v)
{
if (v.equals(one))
result.append("1");
if (v.equals(two))
result.append("2");
if (v.equals(three))
result.append("3");
if (v.equals(four))
result.append("4");
if (v.equals(five))
result.append("5");
if (v.equals(six))
result.append("6");
if (v.equals(seven))
result.append("7");
if (v.equals(eight))
result.append("8");
if (v.equals(nine))
result.append("9");
if (v.equals(zero))
result.append("0");
if (v.equals(dots))
result.append(".");
if (v.equals(clears))
result.setText("");
if (v.equals(adds))
result.append("+");
if (v.equals(subs))
result.append("-");
if (v.equals(muls))
result.append("*");
if (v.equals(divs))
result.append("/");

if (v.equals(equals)) {
try {
String data = result.getText().toString();
if (data.contains("/")) {
String[] operands = data.split("/");
if (operands.length == 2) {
double operand1 = Double.parseDouble(operands[0]);
double operand2 = Double.parseDouble(operands[1]);
double tempResult = operand1 / operand2;
result.setText(String.valueOf(tempResult));
} else {
Toast.makeText(getBaseContext(), "Invalid Input",
Toast.LENGTH_LONG).show();
}
} else if (data.contains("*")) {
String[] operands = data.split(Pattern.quote("*"));
if (operands.length == 2) {
double operand1 = Double.parseDouble(operands[0]);
double operand2 = Double.parseDouble(operands[1]);
double tempResult = operand1 * operand2;
result.setText(String.valueOf(tempResult));
} else {
Toast.makeText(getBaseContext(), "Invalid Input",
Toast.LENGTH_LONG).show();
}
} else if (data.contains("+")) {
String[] operands = data.split(Pattern.quote("+"));
if (operands.length == 2) {
double operand1 = Double.parseDouble(operands[0]);
double operand2 = Double.parseDouble(operands[1]);
double tempResult = operand1 + operand2;
result.setText(String.valueOf(tempResult));
} else {
Toast.makeText(getBaseContext(), "Invalid Input",
Toast.LENGTH_LONG).show();
}
} else if (data.contains("-")) {
String[] operands = data.split("-");
if (operands.length == 2) {
double operand1 = Double.parseDouble(operands[0]);
double operand2 = Double.parseDouble(operands[1]);
double tempResult = operand1 - operand2;
result.setText(String.valueOf(tempResult));
} else {
Toast.makeText(getBaseContext(), "Invalid Input",
Toast.LENGTH_LONG).show();
}
}
} catch (Exception e) {
Toast.makeText(getBaseContext(), "Invalid Input",
Toast.LENGTH_LONG).show();
}
}

}
}

You might also like