0% found this document useful (0 votes)
7 views

Assignment 3

The document details the code for a native calculator application. It includes the XML layout code with the design elements like buttons, text views and edit texts. It also includes the Java code with onclick listeners for the buttons to perform calculations like addition, subtraction, multiplication and division on the input numbers.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Assignment 3

The document details the code for a native calculator application. It includes the XML layout code with the design elements like buttons, text views and edit texts. It also includes the Java code with onclick listeners for the buttons to perform calculations like addition, subtraction, multiplication and division on the input numbers.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Vishwakarma Institute of Technology

Department of Engineering Sciences and Humanities (DESH)

Assignment 3

BY Krishna Endela
Rollno 44
Division J Batch 2 Group 8

Problem Statement:
Develop a native calculator application.

Program code(.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">

<TextView
android:layout_width="242dp"
android:layout_height="43dp"
android:layout_marginTop="71dp"
android:text="CALC"
android:textSize="32sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.958"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.019" />

<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginLeft="16dp"
android:layout_marginTop="181dp"
android:text="NUMBER 1"

1
Vishwakarma Institute of Technology
Department of Engineering Sciences and Humanities (DESH)

android:textSize="20dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="12dp"
android:layout_marginLeft="12dp"
android:layout_marginTop="230dp"
android:text="NUMBER 2"
android:textSize="20dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<EditText
android:id="@+id/editTextNumber"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="128dp"
android:layout_marginLeft="128dp"
android:layout_marginTop="163dp"
android:ems="10"
android:inputType="number"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<EditText
android:id="@+id/editTextNumber2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="128dp"
android:layout_marginLeft="128dp"
android:layout_marginTop="212dp"
android:ems="10"
android:inputType="number"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<Button
android:id="@+id/button6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="6dp"
android:layout_marginLeft="6dp"
android:layout_marginTop="331dp"
android:text="+"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

2
Vishwakarma Institute of Technology
Department of Engineering Sciences and Humanities (DESH)

<Button
android:id="@+id/button7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="154dp"
android:layout_marginLeft="154dp"
android:layout_marginTop="331dp"
android:text="-"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<Button
android:id="@+id/button8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="291dp"
android:layout_marginLeft="291dp"
android:layout_marginTop="331dp"
android:text="x"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<Button
android:id="@+id/button9"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="11dp"
android:layout_marginLeft="11dp"
android:layout_marginTop="432dp"
android:text="/"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<Button
android:id="@+id/button10"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="150dp"
android:layout_marginLeft="150dp"
android:layout_marginTop="432dp"
android:text="Power"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<Button
android:id="@+id/button11"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="291dp"
android:layout_marginLeft="291dp"
android:layout_marginTop="432dp"

3
Vishwakarma Institute of Technology
Department of Engineering Sciences and Humanities (DESH)

android:text="Log(e)"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<Button
android:id="@+id/button12"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginLeft="16dp"
android:layout_marginTop="533dp"
android:text="Sin"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<Button
android:id="@+id/button13"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="150dp"
android:layout_marginLeft="150dp"
android:layout_marginTop="533dp"
android:text="Cos"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<Button
android:id="@+id/button14"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="291dp"
android:layout_marginLeft="291dp"
android:layout_marginTop="533dp"
android:text="Tan"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<TextView
android:id="@+id/textView4"
android:layout_width="105dp"
android:layout_height="29dp"
android:layout_marginStart="162dp"
android:layout_marginLeft="162dp"
android:layout_marginTop="602dp"
android:text="Result"
android:textSize="25sp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<EditText
android:id="@+id/editTextNumber3"

4
Vishwakarma Institute of Technology
Department of Engineering Sciences and Humanities (DESH)

android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="114dp"
android:layout_marginLeft="114dp"
android:layout_marginTop="639dp"
android:layout_marginEnd="87dp"
android:layout_marginRight="87dp"
android:layout_marginBottom="47dp"
android:ems="10"
android:inputType="number"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

MainActivity.java:
package com.example.assingnment3;
import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
EditText t1 = (EditText) findViewById(R.id.editTextNumber);
EditText t2 = (EditText) findViewById(R.id.editTextNumber2);
EditText result = (EditText) findViewById(R.id.editTextNumber3);
Button add = (Button) findViewById(R.id.button6);
Button sub = (Button) findViewById(R.id.button7);
Button mul = (Button) findViewById(R.id.button8);
Button div = (Button) findViewById(R.id.button9);
Button power1 = (Button) findViewById(R.id.button10);
Button log1 = (Button) findViewById(R.id.button11);
Button sin1 = (Button) findViewById(R.id.button12);
Button cos1 = (Button) findViewById(R.id.button13);
Button tan1 = (Button) findViewById(R.id.button14);
add.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {

5
Vishwakarma Institute of Technology
Department of Engineering Sciences and Humanities (DESH)

float num1 = Float.parseFloat(t1.getText().toString());


float num2 = Float.parseFloat(t2.getText().toString());
float sum = num1+num2;
//result.setText(sum); //
result.setText(Float.toString(sum));
}
});
sub.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
float num1 = Float.parseFloat(t1.getText().toString());
float num2 = Float.parseFloat(t2.getText().toString());
float diff = num1-num2;
result.setText(Float.toString(diff));
}
});
mul.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
float num1 = Float.parseFloat(t1.getText().toString());
float num2 = Float.parseFloat(t2.getText().toString());
float multiply = num1*num2;
result.setText(Float.toString(multiply));
}
});
div.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
float num1 = Float.parseFloat(t1.getText().toString());
float num2 = Float.parseFloat(t2.getText().toString());
float divide = num1/num2;
result.setText(Float.toString(divide));
}
});
power1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
double num1 = Double.parseDouble(t1.getText().toString());
double num2 = Double.parseDouble(t2.getText().toString());
double powerr = Math.pow(num1,num2);
result.setText(Double.toString(powerr));
}
});
log1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
double num1 = Double.parseDouble(t1.getText().toString());
double logg = Math.log(num1);
result.setText(Double.toString(logg));
}
});

6
Vishwakarma Institute of Technology
Department of Engineering Sciences and Humanities (DESH)

sin1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
double num1 = Double.parseDouble(t1.getText().toString());
double num2 = Math.toRadians(num1);
double sinn = Math.sin(num2);
result.setText(Double.toString(sinn));
}
});
cos1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
double num1 = Double.parseDouble(t1.getText().toString());
double num2 = Math.toRadians(num1);
double coss = Math.cos(num2);
result.setText(Double.toString(coss));
}
});
tan1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
double num1 = Double.parseDouble(t1.getText().toString());
double num2 = Math.toRadians(num1);
double tann = Math.tan(num2);
result.setText(Double.toString(tann));
}
});
}
}

Output:

You might also like