0% found this document useful (0 votes)
6 views6 pages

Experiment-3 (Madl) DP

The document outlines the development of a native calculator application for Android, detailing the Java code for the MainActivity class, which includes button functionalities for numbers and operations. It also provides the XML layout for the user interface, including the display and button grid, as well as color and style resources for the application's theme. The calculator supports basic arithmetic operations and includes error handling for invalid expressions.

Uploaded by

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

Experiment-3 (Madl) DP

The document outlines the development of a native calculator application for Android, detailing the Java code for the MainActivity class, which includes button functionalities for numbers and operations. It also provides the XML layout for the user interface, including the display and button grid, as well as color and style resources for the application's theme. The calculator supports basic arithmetic operations and includes error handling for invalid expressions.

Uploaded by

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

Dinesh Pandian G

953622205017
EXPERIMENT-3
DEVELOP A NATIVE CALCULATOR APPLICATION
MainActivity.java:
package com.example.ex_3;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
private EditText display;
private String input = "";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
display = findViewById(R.id.display);
// Number Buttons
Button button0 = findViewById(R.id.button_0);
Button button1 = findViewById(R.id.button_1);
Button button2 = findViewById(R.id.button_2);
Button button3 = findViewById(R.id.button_3);
Button button4 = findViewById(R.id.button_4);
Button button5 = findViewById(R.id.button_5);
Button button6 = findViewById(R.id.button_6);
Button button7 = findViewById(R.id.button_7);
Button button8 = findViewById(R.id.button_8);
Button button9 = findViewById(R.id.button_9);
// Operation Buttons
Button buttonAdd = findViewById(R.id.button_add);
Button buttonSubtract = findViewById(R.id.button_subtract);
Button buttonMultiply = findViewById(R.id.button_multiply);
Button buttonDivide = findViewById(R.id.button_divide);
Button buttonEqual = findViewById(R.id.button_equal);
Button buttonClear = findViewById(R.id.button_clear);
// Set click listeners for all buttons
View.OnClickListener numberClickListener = v -> {
Button button = (Button) v;
appendToInput(button.getText().toString());
};
button0.setOnClickListener(numberClickListener);
button1.setOnClickListener(numberClickListener);
Dinesh Pandian G
953622205017
button2.setOnClickListener(numberClickListener);
button3.setOnClickListener(numberClickListener);
button4.setOnClickListener(numberClickListener);
button5.setOnClickListener(numberClickListener);
button6.setOnClickListener(numberClickListener);
button7.setOnClickListener(numberClickListener);
button8.setOnClickListener(numberClickListener);
button9.setOnClickListener(numberClickListener);
buttonAdd.setOnClickListener(v -> appendToInput("+"));
buttonSubtract.setOnClickListener(v -> appendToInput("-"));
buttonMultiply.setOnClickListener(v -> appendToInput("*"));
buttonDivide.setOnClickListener(v -> appendToInput("/"));
buttonClear.setOnClickListener(v -> {
input = "";
display.setText("");
});
buttonEqual.setOnClickListener(v -> {
try {
double result = evaluateExpression(input);
display.setText(String.valueOf(result));
input = String.valueOf(result);
} catch (Exception e) {
Toast.makeText(MainActivity.this, "Invalid Expression",
Toast.LENGTH_SHORT).show();
}
});
}
private void appendToInput(String value) {
input += value;
display.setText(input);
}
private double evaluateExpression(String expression) throws Exception {
return new Object() {
int pos = -1, c;

void nextChar() {
c = (++pos < expression.length()) ? expression.charAt(pos) : -1;
}
boolean eat(int charToEat) {
while (c == ' ') nextChar();
if (c == charToEat) {
nextChar();
return true;
}
Dinesh Pandian G
953622205017
return false;
}
double parse() throws Exception {
nextChar();
double x = parseExpression();
if (pos < expression.length()) throw new Exception("Unexpected: " + (char) c);
return x;
}
double parseExpression() throws Exception {
double x = parseTerm();
for (; ; ) {
if (eat('+')) x += parseTerm();
else if (eat('-')) x -= parseTerm();
else return x;
}
}
double parseTerm() throws Exception {
double x = parseFactor();
for (; ; ) {
if (eat('*')) x *= parseFactor();
else if (eat('/')) x /= parseFactor();
else return x;
}
}
double parseFactor() throws Exception {
if (eat('+')) return parseFactor();
if (eat('-')) return -parseFactor();
double x;
int startPos = this.pos;
if (eat('(')) {
x = parseExpression();
eat(')');
} else if ((c >= '0' && c <= '9') || c == '.') {
while ((c >= '0' && c <= '9') || c == '.') nextChar();
x = Double.parseDouble(expression.substring(startPos, this.pos));
} else {
throw new Exception("Unexpected: " + (char) c);
}
return x;
}
}.parse();
}
}
Dinesh Pandian G
953622205017
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:background="@color/colorPrimary">
<!-- Display for the calculator -->
<EditText
android:id="@+id/display"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:padding="16dp"
android:textSize="32sp"
android:gravity="end"
android:focusable="false"
android:editable="false"
android:background="@android:color/white"
android:textColor="@android:color/black"
android:hint="0"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"/>
<!-- Grid Layout for buttons -->
<GridLayout
android:id="@+id/button_grid"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:columnCount="4"
android:rowCount="5"
android:padding="16dp"
android:layout_marginTop="16dp"
app:layout_constraintTop_toBottomOf="@id/display"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent">
<!-- Row 1 -->
<Button android:id="@+id/button_7" style="@style/CalculatorButton"
android:text="7"/>
<Button android:id="@+id/button_8" style="@style/CalculatorButton"
android:text="8"/>
Dinesh Pandian G
953622205017
<Button android:id="@+id/button_9" style="@style/CalculatorButton"
android:text="9"/>
<Button android:id="@+id/button_divide" style="@style/OperatorButton"
android:text="/"/>
<!-- Row 2 -->
<Button android:id="@+id/button_4" style="@style/CalculatorButton"
android:text="4"/>
<Button android:id="@+id/button_5" style="@style/CalculatorButton"
android:text="5"/>
<Button android:id="@+id/button_6" style="@style/CalculatorButton"
android:text="6"/>
<Button android:id="@+id/button_multiply" style="@style/OperatorButton"
android:text="*"/>
<!-- Row 3 -->
<Button android:id="@+id/button_1" style="@style/CalculatorButton"
android:text="1"/>
<Button android:id="@+id/button_2" style="@style/CalculatorButton"
android:text="2"/>
<Button android:id="@+id/button_3" style="@style/CalculatorButton"
android:text="3"/>
<Button android:id="@+id/button_subtract" style="@style/OperatorButton"
android:text="-"/>
<!-- Row 4 -->
<Button android:id="@+id/button_0" style="@style/CalculatorButton"
android:text="0"/>
<Button android:id="@+id/button_clear" style="@style/ClearButton"
android:text="C"/>
<Button android:id="@+id/button_equal" style="@style/EqualButton"
android:text="="/>
<Button android:id="@+id/button_add" style="@style/OperatorButton"
android:text="+"/>
</GridLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
colors.xml:
<resources>
<color name="colorPrimary">#FF5722</color>
<color name="colorPrimaryDark">#283593</color>
<color name="colorAccent">#FF9800</color>

<!-- Button Colors -->


<color name="buttonBackground">#4CAF50</color>
<color name="buttonTextColor">#FFFFFF</color>
<color name="operatorBackground">#FF5722</color>
Dinesh Pandian G
953622205017
<color name="equalBackground">#FFC107</color>
<color name="clearBackground">#D32F2F</color>
</resources>
Styles.xml:
<resources>
<!-- Main Theme -->
<style name="Theme.Calculator_1"
parent="Theme.MaterialComponents.DayNight.DarkActionBar">
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryVariant">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
<item name="android:windowBackground">@color/colorPrimary</item>
</style>
<!-- Number Button Style -->
<style name="CalculatorButton">
<item name="android:layout_width">90dp</item>
<item name="android:layout_height">90dp</item>
<item name="android:background">@color/buttonBackground</item>
<item name="android:textColor">@color/buttonTextColor</item>
<item name="android:textSize">24sp</item>
<item name="android:padding">16dp</item>
</style>
<!-- Operator Buttons Style -->
<style name="OperatorButton" parent="CalculatorButton">
<item name="android:background">@color/operatorBackground</item>
</style>
<!-- Equal Button Style -->
<style name="EqualButton" parent="CalculatorButton">
<item name="android:background">@color/equalBackground</item>
</style>
<!-- Clear Button Style -->
<style name="ClearButton" parent="CalculatorButton">
<item name="android:background">@color/clearBackground</item>
</style>
</resources>

You might also like