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

Calculator Android Application: Jyoti Tiwari

This document summarizes a student's project to develop a scientific calculator application for Android using Kotlin. It includes a declaration, acknowledgments, abstract, and introductions to Kotlin programming language concepts like variables, data types, and strings. The student developed the app to fulfill their bachelor's degree requirements and received guidance from their department head and project supervisor.

Uploaded by

Ashok Kumar
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
44 views

Calculator Android Application: Jyoti Tiwari

This document summarizes a student's project to develop a scientific calculator application for Android using Kotlin. It includes a declaration, acknowledgments, abstract, and introductions to Kotlin programming language concepts like variables, data types, and strings. The student developed the app to fulfill their bachelor's degree requirements and received guidance from their department head and project supervisor.

Uploaded by

Ashok Kumar
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 45

Calculator Android Application

Submitted in partial fulfillment of the requirements for the award of degree of

BACHELOR OF TECHNOLOGY
IN
INFORMATION TECHNOLOGY

Submitted By:
Jyoti Tiwari
Roll No: 18362

Submitted To:
Er. Praveen Mishra
HOD (IT)

SESSION -17th April 2021

DEPARTMENT OF INFORMATION TECHNOLOGY,


INSTITUTE OF ENGINEERING AND TECHNOLOGY,
DR. RAMMANOHAR LOHIA AWADH UNIVERSITY
AYODHYA U.P.
DECLARATION
I hereby declare that the training report entitled “Android Application for Calculator” submitted
by me to Institute of Engineering & Technology; Dr. Rammanohar Lohiya Avadh University
Ayodhya U.P. in partial fulfillment of the requirement for the award of the degree of Bachelor of
Technology in the Department of Information Technology is a record of bonfide project work
carried out by me under the guidance of Er. Praveen Mishra. I further declare that the work
reported in this project has not been submitted and will not be submitted, either in part or in full,
for the award of any other degree or diploma in this institute or any other institute or university.

Signature of Candidate
(Jyoti Tiwari)
ACKNOWLEDGMENT
I am highly grateful to the Er. Praveen Mishra, HOD IT, Institute of Engineering and
Technology, Dr. RML Avadh University, Faizabad, for providing this opportunity to carry out
the Winter Training On Android Application with KOTLIN.
I would like to expresses my gratitude to other faculty members of Information Technology
department of IET, Dr. RML Avadh University, Faizabad, for providing academic inputs,
guidance & encouragement throughout this period.
The author would like to express a deep sense of gratitude and thank Dr.
Rama Pati Mishra Director of the institute, without whose permission, wise counsel and able
guidance, it would have not been possible to carry out my project in this manner.
The help rendered by Mr. Vishal Bhatt, Supervisor for Project is greatly acknowledged.
Finally, I express my indebtedness to all who have directly or indirectly contributed to the successful
completion of my project.

Jyoti Tiwari
CERTIFICATE

TO WHOM IT MAY CONCERN

I hereby certify that Jyoti Tiwari Roll No. 18362 student of Institute of Engineering and
Technology, Dr. RML Avadh University, Faizabad, has undergone Mini Project from Feburary
to March at our organization to fulfill the requirements for the award of degree of B.Tech.(IT
Branch). She worked on Calculator project during this period under the supervision of Er.
Praveen Mishra (HOD Of Information Technology) During his tenure with us we found him
sincere and hard working. We wish him a great success in the future.

Dated: 17/04/2021 Project In-charge:


()

Countersigned by (Head of Department) Stamp


ABSTRACT
A Scientific Calculator is useful for situations where we need to calculate some complex things
like logs or trigonometry. In such cases, the normal calculator won’t be useful for us. So
therefore, we are here to develop a Scientific Calculator.Scientific Calculator Free is a powerful
and comprehensive calculator that can help you solve both simple and advanced math operations.
... The best thing about this app is that its interface is designed to look like a real calculator,
making it intuitive to use.
INRODUCTION (ABOUT KOTLIN)
KOTLIN
History of Kotlin
Kotlin was developed by JetBrains team. A project was started in 2010 to develop the
language and officially, first released in February 2016. Kotlin was developed under the
Apache 2.0 license.

What is Kotlin?
Kotlin tutorial provides basic and advanced concepts of Kotlin programming language. Our
Kotlin tutorial is designed for beginners and professionals both.
Kotlin is a statically-typed, general-purpose programming language. It is widely used to develop
android applications.Kotlin is a general-purpose, statically typed, and open-source programming
language. It runs on JVM and can be used anywhere Java is used today. It can be used to develop
Android apps, server-side apps and much more.

Characteristic of Kotlin
Concise: Kotlin reduces writing the extra codes. This makes Kotlin more concise.

Null safety: Kotlin is null safety language. Kotlin aimed to eliminate the NullPointerException
(null reference) from the code.Interoperable.

Interoperable: Kotlin easily calls the Java code in a natural way as well as Kotlin code can be
used by Java.

Smart cast: It explicitly typecasts the immutable values and inserts the value in its safe cast
automatically.

Compilation Time: It has better performance and fast compilation time.

Tool-friendly: Kotlin programs are build using the command line as well as any of Java IDE.
Extension function: Kotlin supports extension functions and extension properties which
means it helps to extend the functionality of classes without touching their code.

Uses of Kotlin:
Kotlin is an Android-compatible language that is concise, expressive, and designed to be type-
and null-safe. It works with the Java language seamlessly, so it makes it easy for developers who
love the Java language to keep using it but also incrementally add Kotlin code and leverage
Kotlin libraries.

Applications of Kotlin:
Here are the 14 Android Apps that are made using Kotlin for Android.
Pinterest
Postmates
Evernote
Corda
Coursera
Uber
Shadowsocks

Variable in Kotlin
Variable refers to a memory location. It is used to store data. The data of variable can be changed
and reused depending on condition or on information passed to the program.All variables in PHP
are denoted with a leading dollar sign ($).
Variable Declaration
Kotlin variable is declared using keyword var and val.

var (Mutable variable): We can change the value of variable declared using var keyword later
in the program.
val (Immutable variable): We cannot change the value of variable which is declared using val
keyword.
Data types in Kotlin
Data type (basic type) refers to type and size of data associated with variables and functions.
Data type is used for declaration of memory location of variable which determines the features of
data.

In Kotlin, everything is an object, which means we can call member function and properties on
any variable.
Kotlin built in data type are categorized as following different categories:
Number
Character
Boolean
Array
String
Number types of data are those which hold only number type data variables. It is further
categorized into different Integer and Floating point.

DataType Bit Width (Size) Data Range


Byte 8 bit -128 to 127
Short 16 bit -32768 to 32767
Int 32 bit -2,147,483,648 to 2,147,483,647
Long 64 bit -9,223,372,036,854,775,808 to +9,223,372,036,854,775,807
Float 32 bit 1.40129846432481707e-45 to 3.40282346638528860e+38
Double 64 bit 4.94065645841246544e-324to1.79769313486231570e+308

Character (Char) Data Type:


DataType Bit Width(Size) Data Range
Char 4 bit -128 to 127

Boolean Data Types


Boolean data is represented using the type Boolean. It contains values either true or false.

Data Type Bit Width (Size) Data Value


Boolean 1 bit true or false

Array:Arrays in Kotlin are represented by the Array class. Arrays are created using library
function arrayOf() and Array() constructor. Array has get (), set() function, size property as well
as some other useful member functions.
Creating Array using library function arrayOf()
The arrayOf() function creates array of wrapper types. The item value are passed inside
arrayOf() function like arrayOf(1,2,3) which creates an array[1,2,3].
The elements of array are accessed through their index values (array[index]). Array index are
start from zero.

Creating Array using Array() constructor


Creating array using Array() constructor takes two arguments in Array() constructor:

First argument as a size of array, and


Second argument as the function, which is used to initialize and return the value of array element
given its index.

String:
String in Kotlin is represented by String class. String is immutable, which means we cannot
change the elements in String.

Types of String
String are categorize into two types.
These are:

1. Escaped String: Escape String is declared within double quote (" ") and may contain escape
characters like '\n', '\t', '\b' etc.

2. Raw String: Row String is declared within triple quote (""" """). It provides facility to
declare String in new lines and contain multiple lines. Row String cannot contain any escape
character.

Kotlin Type Conversion


Type conversion is a process in which one data type variable is converted into another data type.
In Kotlin, implicit conversion of smaller data type into larger data type is not supported (as it
supports in java). For example Int cannot be assigned into Long or Double.
However in Kotlin, conversion is done by explicit in which smaller data type is converted into
larger data type and vice-versa. This is done by using helper function.

The list of helper functions used for numeric conversion in Kotlin is given below:
• toByte()
• toShort()
• toInt()
• toLong()
• toFloat()
• toDouble()
• toChar()

Kotlin Operator
Operators are special characters which perform operation on operands (values or variable).There
are various kind of operators available in Kotlin.

• Arithmetic operator
• Relation operator
• Assignment operator
• Unary operator
• Bitwise operation
• Logical operator
• Arithmetic Operator
Arithmetic operators are used to perform basic mathematical operations such as addition (+),
subtraction (-), multiplication (*), division (/) etc.

• Operator Description Expression Translate to


• + Addition a+b a.plus(b)
• - Subtraction a-b a.minus(b)
• * Multiply a*b a.times(b)
• / Division a/b a.div(b)
• % Modulus a%b a.rem(b)

Relation Operator:
Relation operator shows the relation and compares between operands. Following are the different
relational operators:

Operator Description Expression Translate to


• > greater than a>b a.compateTo(b)>0
• < Less than a<b a.compateTo(b)<0
• >= greater than or equal to a>=b a.compateTo(b)>=0
• <= less than or equal to a<=b a?.equals(b)?:(b===null)
• == is equal to a==b a?.equals(b)?:(b===null)
• != not equal to a!=b !(a?.equals(b)?:(b===null))

Assignment operator
Assignment operator "=" is used to assign a value to another variable. The assignment of value
takes from right to left.

Operator Description Expression Convert to


• += add and assign a+=b a.plusAssign(b)
• -= subtract and assign a-=b a.minusAssign(b)
• *= multiply and assign a*=b a.timesAssign(b)
• /= divide and assign a/=b a.divAssign(b)
• %= mod and assign a%=b a.remAssign(b)

Unary Operator

Unary operator is used with only single operand. Following are some unary operator given
below.

Operator Description Expression Convert to


• + unary plus +a a.unaryPlus()
• - unary minus -a a.unaryMinus()
• ++ increment by 1++a a.inc()
• -- decrement by 1--a a.dec()
• ! not !a a.not()

Logical Operator
Logical operators are used to check conditions between operands. List of logical operators are
given below.

Operator Description Expression Convert to


• && return true if all expression are true (a>b) && (a>c) (a>b)and (a<c)
• || return true if any expression are true (a>b) || (a>c) (a>b) or(a>c)
• ! return complement of expression !a a.not()

Bitwise Operation
In Kotlin, there is not any special bitwise operator. Bitwise operation is done using named
function.
Named Function Description Expression
• shl (bits) signed shift left a.shl(b)
• shr (bits) signed shift right a.shr(b)
• ushr (bits) unsigned shift right a.ushr(b)
• and (bits) bitwise and a.and(b)
• or (bits) bitwise or a.or(b)
• xor (bits) bitwise xor a.xor(b)
• inv() bitwise inverse a.inv()

Kotlin Standard Input/Output


Kotlin standard input output operations are performed to flow byte stream from input device
(keyboard) to main memory and from main memory to output device (screen).

Kotlin Output
Kotlin output operation is performed using the standard methods print() and println().

Difference between print() and println() methods:

print(): print() method is used to print values provided inside the method "()"
.
println(): println() method is used to print values provided inside the method "()" and moves
cursor to the beginning of next line.

Kotlin Input

Kotlin has standard library function readLine() which is used for reads line of string input from
standard input stream. It returns the line read or null.

Kotlin Comment
Comments are the statements that are used for documentation purpose. Comments are ignored by
compiler so that don't execute. We can also used it for providing information about the line of
code. There are two types of comments in Kotlin.

• Single line comment.


• Multi line comment.
Single line comment
Single line comment is used for commenting single line of statement. It is done by using '//'
(double slash).
Multi line comment
Multi line comment is used for commenting multiple line of statement. It is done by using /* */
(start with slash strict and end with star slash)

Kotlin if Expression
In Kotlin, if is an expression is which returns a value. It is used for control the flow of program
structure.
There is various type of if expression in Kotlin.
• if-else expression
• if-else if-else ladder expression
• nested if expression

Traditional if Statement
Syntax of traditional if statement

if(condation){
//code statement
}

Syntax of traditional if else statement


if(condation){
//code statement
}
else{
//code statement
}

Kotlin if-else Expression


As if is an expression it is not used as standalone, it is used with if-else expression and the result
of an if-else expression is assign into a variable.

Syntax of if-else expression

val returnValue = if (condation) {


//code statement
} else {
// code statement
}
println(returnValue)

Syntax of if-else expression

val returnValue = if (condation) {


//code statement
} else {
// code statement
}
println(returnValue)

Kotlin if-else if-else Ladder Expression


Let's see an example of if-else if-else ladder expression.

fun main(args: Array<String>) {


val num = 10
val result = if (num > 0){
"$num is positive"
}else if(num < 0){
"$num is negative"
}else{
"$num is zero"
}
println(result)
}

Kotlin Nested if Expression


Let's see an example of nested if expression.

fun main(args: Array<String>) {


val num1 = 25
val num2 = 20
val num3 = 30
val result = if (num1 > num2){
val max = if(num1 > num3){
num1
}else{
num3
}
"body of if "+max
}else if(num2 > num3){
"body of else if"+num2
}else{
"body of else "+num3
}
println("$result")
}

Kotlin when Expression


Kotlin, when expression is a conditional expression which returns the value. Kotlin, when
expression is replacement of switch statement. Kotlin, when expression works as a switch
statement of other language (Java, C++, C).

Using when as an Expression


Let's see a simple example of when expression.

fun main(args: Array<String>){


var number = 4
var numberProvided = when(number) {
1 -> "One"
2 -> "Two"
3 -> "Three"
4 -> "Four"
5 -> "Five"
else -> "invalid number"
}
println("You provide $numberProvided")
}
Using when Without Expression
It is not mandatory to use when as an expression, it can be used as normally as it used in other
language.
For Example

fun main(args: Array<String>){

var number = 4
when(number) {
1 -> println("One")
2 -> println("Two")
3 -> println("Three")
4 -> println("Four")
5 -> println("Five")
else -> println("invalid number")
}

Multiple Statement of when Using Braces


We can use multiple statement enclosed within block of condition.
For Example

fun main(args: Array<String>){


var number = 1
when(number) {
1 -> {
println("Monday")
println("First day of the week")
}
7 -> println("Sunday")
else -> println("Other days")
}
}

Multiple branches of when


We can use multiple branches of condition separated with a comma. It is used, when we need to
run a same logic for multiple choices.

fun main(args: Array<String>){


var number = 8
when(number) {
3, 4, 5, 6 ->
println("It is summer season")
7, 8, 9 ->
println("It is rainy season")
10, 11 ->
println("It is autumn season")
12, 1, 2 ->
println("It is winter season")
else -> println("invalid input")
}
}

Using when in the range


The when expression also check the ranges of input provided in when condition. A range is
created using .. (double dot) operator. The in operator is used to check if a value belongs to a
range.

For Example:

fun main(args: Array<String>){


var number = 7
when(number) {
in 1..5 -> println("Input is provided in the range 1 to 5")
in 6..10 -> println("Input is provided in the range 6 to 10")
else -> println("none of the above")
}
}

Kotlin for Loop:


Kotlin for loop is used to iterate a part of program several times. It iterates through arrays,
ranges, collections, or anything that provides for iterate. Kotlin for loop is equivalent to the
foreach loop in languages like C#.

Syntax of for loop in Kotlin:


VDO.AI
for (item in collection){
//body of loop
}
Iterate through array:
Let's see a simple example of iterating the elements of array.
fun main(args : Array<String>) {
val marks = arrayOf(80,85,60,90,70)
for(item in marks){
println(item)
}
}

If the body of for loop contains only one single line of statement, it is not necessary to enclose
within curly braces {}.

fun main(args : Array<String>) {


val marks = arrayOf(80,85,60,90,70)
for(item in marks)
println(item)
}
The elements of an array are iterated on the basis of indices (index) of array. For example:

fun main(args : Array<String>) {

val marks = arrayOf(80,85,60,90,70)


for(item in marks.indices)
println("marks[$item]: "+ marks[item])
}

Iterate through range


Let's see an example of iterating the elements of range.

fun main(args : Array<String>) {

print("for (i in 1..5) print(i) = ")


for (i in 1..5) print(i)
println()
print("for (i in 5..1) print(i) = ")
for (i in 5..1) print(i) // prints nothing
println()
print("for (i in 5 downTo 1) print(i) = ")
for (i in 5 downTo 1) print(i)
println()
print("for (i in 5 downTo 2) print(i) = ")
for (i in 5 downTo 2) print(i)
println()
print("for (i in 1..5 step 2) print(i) = ")
for (i in 1..5 step 2) print(i)
println()
print("for (i in 5 downTo 1 step 2) print(i) = ")
for (i in 5 downTo 1 step 2) print(i)
}

Kotlin while Loop


The while loop is used to iterate a part of program several time. Loop executed the block of code
until the condition has true. Kotlin while loop is similar to Java while loop.

Syntax

while(condition){
//body of loop
}

Example of while Loop


Let's see a simple example of while loop printing value from 1 to 5.
fun main(args: Array<String>){
var i = 1
while (i<=5){
println(i)
i++
}
}

Kotlin Infinite while Loop


The while loop executes a block of code to infinite times, if while condition remain true.

For example:

fun main(args: Array<String>){


while (true){
println("infinite loop")
}
}
Kotlin do-while Loop
The do-while loop is similar to while loop except one key difference. A do-while loop first
execute the body of do block after that it check the condition of while.

As a do block of do-while loop executed first before checking the condition, do-while loop
execute at least once even the condition within while is false. The while statement of do-while
loop end with ";" (semicolon).

Syntax:
do{
//body of do block
}
while(condition);
Example of do -while loop

Let's see a simple example of do-while loop printing value 1 to 5.

fun main(args: Array<String>){


var i = 1
do {
println(i)
i++
}
while (i<=5);
}

Example of do -while loop even condition of while if false

In this example do-while loop execute at once time even the condition of while is false.

fun main(args: Array<String>){


var i = 6
do {
println(i)
i++
}
while (i<=5);
}

Kotlin Return and Jump:


There are three jump expressions in Kotlin. These jump expressions are used for control the flow
of program execution.
These jump structures are:
• break
• continue
• return

Break Expression
A break expression is used for terminate the nearest enclosing loop. It is almost used with if-else
condition.
For Example:
fun main(args: Array<String>) {
for (i in 1..5) {
if (i == 3) {
break
}
println(i)
}
}
In the above example, when the value of i became equal to 3 and satisfy the if condition(i==3)
than the break expression execute and terminate for loop.

Kotlin Labeled break Expression

Labeled is the form of identifier followed by the @ sign, for example abc@, test@. To make an
expression as label, we just put a label in front of expression.

Kotlin labeled break expression is used to terminate the specific loop. This is done by using
break expression with @ sign followed by label name (break@loop).
Kotlin labeled break example

fun main(args: Array<String>) {


loop@ for (i in 1..3) {
for (j in 1..3) {
println("i = $i and j = $j")
if (i == 2)
break@loop
}
}
}

Kotlin continue Jump Structure


Kotlin, continue statement is used to repeat the loop. It continues the current flow of the program
and skips the remaining code at specified condition.
The continue statement within a nested loop only affects the inner loop.

For example
fun main(args: Array<String>) {
for (i in 1..3) {
println("i = $i")
if (j == 2) {
continue
}
println("this is below if")
}
}

Kotlin Labeled continue Expression


Labeled is the form of identifier followed by the @ sign, for example abc@, test@. To make an
expression as label, we just put a label in front of expression.

Kotlin, labeled continue expression is used for repetition of specific loop (labeled loop). This is
done by using continue expression with @ sign followed by label name (continue@labelname).

Kotlin labeled continue example

fun main(args: Array<String>) {


labelname@ for (i in 1..3) {
for (j in 1..3) {
println("i = $i and j = $j")
if (i == 2) {
continue@labelname
}
println("this is below if")
}
}
}

Kotlin Function
Function is a group of inter related block of code which performs a specific task. Function is
used to break a program into different sub module. It makes reusability of code and makes
program more manageable.

In Kotlin, functions are declared using fun keyword. There are two types of functions depending
on whether it is available in standard library or defined by user.

• Standard library function


• User defined function

Standard Library Function:


Kotlin Standard library function is built-in library functions which are implicitly present in
library and available for use.

For example

fun main(args: Array<String>){


var number = 25
var result = Math.sqrt(number.toDouble())
print("Square root of $number is $result")
}

Here, sqrt() is a library function which returns square root of a number (Double value).
print() library function which prints a message to standard output stream
.
User defined Function:
User defined function is a function which is created by user. User defined function takes the
parameter(s), perform an action and return the result of that action as a value.
Kotlin functions are declared using the fun keyword.
For example:
fun functionName(){
// body of function
}

We have to call the function to run codes inside the body of the function.
functionName()

Kotlin simple function example:


fun main(args: Array<String>){
sum()
print("code after sum")
}
fun sum(){
var num1 =5
var num2 = 6
println("sum = "+(num1+num2))
}

Kotlin Parameterize Function and Return Value


Functions are also takes parameter as arguments and return value. Kotlin functions are defined
using Pascal notation, i.e. name:type (name of parameter and its type). Parameters in function are
separated using commas.

If a function does not returns any value than its return type is Unit. It is optional to specify the
return type of function definition which does not returns any value.

fun functionName(number1: Int, number2: Int){


.. .. ..
}
.. .. ..
functionName(value1, value2)
.. .. ..

Kotlin parameterize function example


fun main(args: Array<String>){
val result = sum(5, 6)
print(result)
}
fun sum(number1: Int, number2:Int): Int{
val add = number1+number2
return add
}

Kotlin Recursion Function


Recursion function is a function which calls itself continuously. This technique is called
recursion.

Syntax:
fun functionName(){
.. .. ..
functionName() //calling same function
}

Kotlin recursion function example 1: Finite times


Let's see an example of recursion function printing count.

var count = 0
fun rec(){
count++;
if(count<=5){
println("hello "+count);
rec();
}
}
fun main(args: Array<String>) {
rec();
}

Kotlin recursion function example 2: Factorial Number


Let's see an example of recursion function calculating factorial of number.

fun main(args: Array<String>) {


val number = 5
val result: Long
result = factorial(number)
println("Factorial of $number = $result")
}

fun factorial(n: Int): Long {


return if(n == 1){
n.toLong()
}
else{
n*factorial(n-1)
}
}

Kotlin Tail Recursion


Before we will discuss about the tail recursion, let's try to make an example which calculate sum
of nth (100000 larger number) using general (normal) recursion.

General Recursion
Let's see an example of calculating sum of nth (100000 larger number) using general (normal)
recursion.

fun main(args: Array<String>) {


var result = recursiveSum(100000)
println(result)
}
fun recursiveSum(n: Long) : Long {
return if (n <= 1) {
n
} else {
n + recursiveSum(n - 1)
}
}
The above example throws an exception of "java.lang.StackOverflowError". This is because the
compiler is unable to call large number of recursive function call.

Tail Recursion
Tail recursion is a recursion which performs the calculation first, then makes the recursive call.
The result of current step is passed into the next recursive call.

Tail recursion follows one rule for implementation. This rule is as follow:

The recursive call must be the last call of the method. To declare a recursion as tail recursion we
need to use tailrec modifier before the recursive function.

Kotlin Default and Named Argument:

Kotlin Default Argument:


Kotlin provides a facility to assign default argument (parameter) in a function definition.

If a function is called without passing any argument than default argument are used as parameter
of function definition. And when a function is called using argument, than the passing argument
is used as parameter in function definition.

Kotlin Named Argument

Before we will discuss about the named parameter, let's do some modify in the above program.

Named Argument
To solve the above problem a named argument is used.

A named argument is an argument in which we define the name of argument in the function call.
The name defined to argument of function call checks the name in the function definition and
assign to it.

Kotlin Lambda Function


Lambda is a function which has no name. Lambda is defined with a curly braces {} which takes
variable as a parameter (if any) and body of function. The body of function is written after
variable (if any) followed by -> operator.

Syntax of lambda:
{ variable -> body_of_function}

Before we talk about lambda, let's see a simple example of addition of two numbers using
normal function.

Normal function: addition of two numbers


In this example, we create a function addNumber() passing two arguments (a,b) calling from the
main function.

fun main(args: Array<String>){


addNumber(5,10)
}
fun addNumber(a: Int, b: Int){
val add = a + b
println(add)
}

Scientific Calculator Android App Project

Learn how to create a scientific calculator app in android studio. This is a good android project
for beginners, as it will increase your ability to apply logic while you’re coding.

What is Android?

Android is a mobile operating system. It is an open-source framework and is based on Linux.


The Android framework helps us to develop advanced and user-friendly applications. The
applications can be built using Java and Kotlin. The Android operating system has then gone
through numerous releases by fixing bugs as well as adding additional features which make our
life more comfortable and easy.

Future Scope:
Android is the leading mobile OS in the market. As we know the demand for Android phones is
growing exponentially, the number of Android phone users has grown in abundance over the last
decade. It is running on many devices, not only smartphones but also on tablets television sets,
books, readers, etc.
Due to this exponential growth android has become the hottest technology for technocrats.
Freshers, as well as experienced professionals, are looking forward to boost their profile with this
ever-growing technology.

Category: Android, Mobile computing


Programming Language: Java
Tools & Libraries: NA
IDE: Android Studio
Front End: XML, Java
Back End: NA
Prerequisites: Java, XML, Android studio
Intended Audience: Education, Developers, Android developers
Proficiency: Beginner

Project Details
A Scientific Calculator is useful for situations where we need to calculate some complex things
like logs or trigonometry. In such cases, the normal calculator won’t be useful for us. So
therefore, we are here to develop a Scientific Calculator.

This calculator will have the following

• First, it will have a screen to display the user inputs and by default, it will display a “0”.
To make this we will use Text View.
• Then there will be another screen that shows the result and the operator. It will be a
TextView too.
• Then we’ll have the keys, that will have numbers as well as the operators on it. We’ll use
Buttons for this.
• We’ll use a Linear layout for this so that we can arrange the buttons in the desired
manner.

To develop this application there are certain things that you need to know beforehand. So let us
see its requirements and the platform that we’ll use for this project. Let us begin with the tool
that we’ll use for our Scientific Calculator. The latest Version of Android Studio will be chosen
as the Application Development Platform for it. So, you must be fond of Android Studio and
have good hands on it.

Android Studio is the best and most appropriate IDE for Android app development today. But, in
order to work on Android Studio, there are some concepts that you must be fond of. So, you
should have good hands on the following things to work on Android Studio

• Object Oriented Programming Concepts


• Java Programming
• eXtensible Markup Language Basics (XML Basics)
• Kotlin (Optional- Either Java or Kotlin will be enough)

Description of the project:


This project will have the following files that you can interact with to make the project work

activity_main.xml: This file describes the whole user interface.

MainActivity.java: This file manages the working of this system. Main java file is important as
it is the only file that enables the execution of the application.

Resource folder: This folder will have all the resources that are useful for this project.
Following are the list of resource files that are used in our project-
Drawable: It has the layout and designing of the application components.
Colors: It has all the colors that are used in the project.
Style: Here the styling of the text or components is done.
String: This file defines all the strings that are necessary for the project.

Steps involved in developing the scientific calculator project:


To implement a scientific calculator app project in Android Studio you can follow the following
steps:
OUTPUT:-
Coding:-

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>


<RelativeLayout
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"
android:orientation="vertical"
android:weightSum="9"
tools:context=".MainActivity">

<TextView
android:id="@+id/input_user"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="2"
android:background="@drawable/bgcolor2"
android:gravity="center|end"
android:hint="0"
android:maxLength="14"
android:padding="10dp"
android:textColor="#000000"
android:textColorHint="#000000"
android:textSize="50sp" />

<TextView
android:id="@+id/sign_user"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:gravity="center|end"
android:padding="10dp"
android:textColor="#000000"
android:textColorHint="#000000"
android:background="@drawable/bgcolor"
android:textSize="32sp" />

<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="@drawable/background_btn1"
android:orientation="horizontal"
android:weightSum="5">

<Button
style="@style/Widget.AppCompat.Button.Borderless"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:onClick="btn_log"
android:text="log"
android:textAllCaps="false"
android:textColor="@color/WhiteText"
android:textSize="30sp" />

<Button
style="@style/Widget.AppCompat.Button.Borderless"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:onClick="btn_ln"
android:text="ln"
android:textAllCaps="false"
android:background="@drawable/background_btn1"
android:textColor="@color/WhiteText"
android:textSize="30sp" />

<Button
style="@style/Widget.AppCompat.Button.Borderless"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:onClick="btn_power"
android:background="@drawable/background_btn1"
android:text="xⁿ"
android:textAllCaps="false"
android:textColor="@color/WhiteText"
android:textSize="30sp" />

<Button
style="@style/Widget.AppCompat.Button.Borderless"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:onClick="btn_root"
android:text="√"
android:background="@drawable/background_btn1"
android:textAllCaps="false"
android:textColor="@color/WhiteText"
android:textSize="30sp" />

<Button
style="@style/Widget.AppCompat.Button.Borderless"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:onClick="btn_factorial"
android:background="@drawable/background_btn1"
android:text="!"
android:textAllCaps="false"
android:textColor="@color/WhiteText"
android:textSize="30sp" />

</LinearLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"

android:background="@drawable/background_btn1"
android:gravity="center"
android:orientation="horizontal"
android:weightSum="5">

<Button
style="@style/Widget.AppCompat.Button.Borderless"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="2"
android:onClick="btn_sin"
android:text="sin"
android:textAllCaps="false"
android:background="@drawable/background_btn1"
android:textColor="@color/WhiteText"
android:textSize="30sp" />

<Button
style="@style/Widget.AppCompat.Button.Borderless"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:onClick="btn_cos"
android:text="cos"
android:textAllCaps="false"
android:textColor="@color/WhiteText"
android:background="@drawable/background_btn1"
android:textSize="30sp" />

<Button
style="@style/Widget.AppCompat.Button.Borderless"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="2"
android:onClick="btn_tan"
android:text="tan"
android:textAllCaps="false"
android:background="@drawable/background_btn1"
android:textColor="@color/WhiteText"
android:textSize="30sp" />

</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal"
android:weightSum="5">

<Button
style="@style/Widget.AppCompat.Button.Borderless"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@drawable/background_btn1"
android:onClick="btn_7"
android:text="7"
android:textColor="@color/WhiteText"
android:textSize="32sp" />

<Button
style="@style/Widget.AppCompat.Button.Borderless"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@drawable/background_btn1"
android:onClick="btn_8"
android:text="8"
android:textColor="@color/WhiteText"
android:textSize="32sp" />

<Button
style="@style/Widget.AppCompat.Button.Borderless"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@drawable/background_btn1"
android:onClick="btn_9"
android:text="9"
android:textColor="@color/WhiteText"
android:textSize="32sp" />

<Button
style="@style/Widget.AppCompat.Button.Borderless"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@drawable/background_btn2"
android:onClick="btn_add"
android:text="+"
android:textColor="@color/WhiteText"
android:textSize="32sp" />

<ImageButton
style="@style/Widget.AppCompat.Button.Borderless"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@drawable/background_btn2"
android:onClick="btn_delete"
android:src="@drawable/ic_backspace_black_24dp" />
</LinearLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal"
android:weightSum="5">

<Button
style="@style/Widget.AppCompat.Button.Borderless"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@drawable/background_btn1"
android:onClick="btn_4"
android:text="4"
android:textColor="@color/WhiteText"
android:textSize="32sp" />

<Button
style="@style/Widget.AppCompat.Button.Borderless"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@drawable/background_btn1"
android:onClick="btn_5"
android:text="5"
android:textColor="@color/WhiteText"
android:textSize="32sp" />

<Button
style="@style/Widget.AppCompat.Button.Borderless"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@drawable/background_btn1"
android:onClick="btn_6"
android:text="6"
android:textColor="@color/WhiteText"
android:textSize="32sp" />

<Button
style="@style/Widget.AppCompat.Button.Borderless"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@drawable/background_btn2"
android:onClick="btn_subtract"
android:text="-"
android:textColor="@color/WhiteText"
android:textSize="32sp" />

<Button
style="@style/Widget.AppCompat.Button.Borderless"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@drawable/background_btn2"
android:onClick="btn_clear"
android:text="C"
android:textColor="@color/WhiteText"
android:textSize="30sp" />

</LinearLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="2"
android:orientation="horizontal"
android:weightSum="5">

<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="4"
android:orientation="vertical"
android:weightSum="2">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal"
android:weightSum="4">

<Button
style="@style/Widget.AppCompat.Button.Borderless"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@drawable/background_btn1"
android:onClick="btn_1"
android:text="1"
android:textColor="@color/WhiteText"
android:textSize="32sp" />

<Button
style="@style/Widget.AppCompat.Button.Borderless"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@drawable/background_btn1"
android:onClick="btn_2"
android:text="2"
android:textColor="@color/WhiteText"
android:textSize="32sp" />

<Button
style="@style/Widget.AppCompat.Button.Borderless"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@drawable/background_btn1"
android:onClick="btn_3"
android:text="3"
android:textColor="@color/WhiteText"
android:textSize="32sp" />

<Button
style="@style/Widget.AppCompat.Button.Borderless"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@drawable/background_btn2"
android:onClick="btn_multiply"
android:text="×"
android:textColor="@color/WhiteText"
android:textSize="32sp" />

</LinearLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal"
android:weightSum="4">

<View
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0"
android:background="@drawable/background_btn1" />

<Button
style="@style/Widget.AppCompat.Button.Borderless"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="2"
android:background="@drawable/background_btn1"
android:onClick="btn_0"
android:text="0"
android:textColor="@color/WhiteText"
android:textSize="32sp" />

<Button
style="@style/Widget.AppCompat.Button.Borderless"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@drawable/background_btn1"
android:onClick="btn_dot"
android:text="."
android:textColor="@color/WhiteText"
android:textSize="32sp" />

<Button
style="@style/Widget.AppCompat.Button.Borderless"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@drawable/background_btn2"
android:onClick="btn_divide"
android:text="÷"
android:textColor="@color/WhiteText"
android:textSize="32sp" />

</LinearLayout>

</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical"
android:weightSum="1">

<Button
style="@style/Widget.AppCompat.Button.Borderless"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="@drawable/background_btn2"
android:onClick="btn_equal"
android:text="="
android:textColor="@color/WhiteText"
android:textSize="32sp" />

</LinearLayout>

</LinearLayout>

</RelativeLayout>

MainActivity.java
package com.dataflair.scientificcalc;

import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

TextView user_input, sign_Box;

Double num1, num2, answer;


String sign, val_1, val_2;
boolean has_Dot;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

user_input = (TextView) findViewById(R.id.input_user);


sign_Box = (TextView) findViewById(R.id.sign_user);

has_Dot = false;

@SuppressLint("SetTextI18n")
public void btn_0(View view) {
user_input.setText(user_input.getText() + "0");
}
@SuppressLint("SetTextI18n")
public void btn_1(View view) {
user_input.setText(user_input.getText() + "1");
}

@SuppressLint("SetTextI18n")
public void btn_2(View view) {
user_input.setText(user_input.getText() + "2");
}

@SuppressLint("SetTextI18n")
public void btn_3(View view) {
user_input.setText(user_input.getText() + "3");
}

@SuppressLint("SetTextI18n")
public void btn_4(View view) {
user_input.setText(user_input.getText() + "4");
}

@SuppressLint("SetTextI18n")
public void btn_5(View view) {
user_input.setText(user_input.getText() + "5");
}

@SuppressLint("SetTextI18n")
public void btn_6(View view) {
user_input.setText(user_input.getText() + "6");
}

@SuppressLint("SetTextI18n")
public void btn_7(View view) {
user_input.setText(user_input.getText() + "7");
}

@SuppressLint("SetTextI18n")
public void btn_8(View view) {
user_input.setText(user_input.getText() + "8");
}

@SuppressLint("SetTextI18n")
public void btn_9(View view) {
user_input.setText(user_input.getText() + "9");
}

@SuppressLint("SetTextI18n")
public void btn_dot(View view) {
if (!has_Dot) {
if (user_input.getText().equals("")) {

user_input.setText("0.");
} else {

user_input.setText(user_input.getText() + ".");
}

has_Dot = true;
}

}
public void btn_add(View view) {
sign = "+";
val_1 = user_input.getText().toString();
user_input.setText(null);
sign_Box.setText("+");
has_Dot = false;
}

public void btn_subtract(View view) {


sign = "-";
val_1 = user_input.getText().toString();
user_input.setText(null);
sign_Box.setText("-");
has_Dot = false;
}

public void btn_multiply(View view) {


sign = "*";
val_1 = user_input.getText().toString();
user_input.setText(null);
sign_Box.setText("×");
has_Dot = false;
}

public void btn_divide(View view) {


sign = "/";
val_1 = user_input.getText().toString();
user_input.setText(null);
sign_Box.setText("÷");
has_Dot = false;
}

@SuppressLint("SetTextI18n")
public void btn_log(View view) {
sign = "log";
user_input.setText(null);
sign_Box.setText("log");
has_Dot = false;
}

@SuppressLint("SetTextI18n")
public void btn_ln(View view) {
sign = "ln";
user_input.setText(null);
sign_Box.setText("ln");
has_Dot = false;
}

public void btn_power(View view) {


sign = "power";
val_1 = user_input.getText().toString();
user_input.setText(null);
has_Dot = false;
sign_Box.setText("xⁿ");
}

public void btn_factorial(View view) {


sign = "factorial";
user_input.setText(null);
has_Dot = false;
sign_Box.setText("!");
}

@SuppressLint("SetTextI18n")
public void btn_sin(View view) {
sign = "sin";
user_input.setText(null);
has_Dot = false;
sign_Box.setText("sin");
}

@SuppressLint("SetTextI18n")
public void btn_cos(View view) {
sign = "cos";
user_input.setText(null);
has_Dot = false;
sign_Box.setText("cos");
}

@SuppressLint("SetTextI18n")
public void btn_tan(View view) {
sign = "tan";
user_input.setText(null);
has_Dot = false;
sign_Box.setText("tan");
}

public void btn_root(View view) {


sign = "root";
user_input.setText(null);
has_Dot = false;
sign_Box.setText("√");
}

@SuppressLint("SetTextI18n")
public void btn_equal(View view) {
if (sign == null) {
sign_Box.setText("Error!");
} else if (user_input.getText().equals("")) {
sign_Box.setText("Error!");
} else if ((sign.equals("+") || sign.equals("-") || sign.equals("*") ||
sign.equals("/")) && val_1.equals("")) {
sign_Box.setText("Error!");
} else {
switch (sign) {
default:
break;
case "log":
val_1 = user_input.getText().toString();
num1 = Double.parseDouble(val_1);
user_input.setText(Math.log10(num1) + "");
sign = null;
sign_Box.setText(null);
break;
case "ln":
val_1 = user_input.getText().toString();
num1 = Double.parseDouble(val_1);
user_input.setText(Math.log(num1) + "");
sign = null;
sign_Box.setText(null);
break;
case "power":
num1 = Double.parseDouble((val_1));
val_2 = user_input.getText().toString();
num2 = Double.parseDouble(val_2);
user_input.setText(Math.pow(num1, num2) + "");
sign = null;
sign_Box.setText(null);
break;
case "root":
val_1 = user_input.getText().toString();
num1 = Double.parseDouble((val_1));
user_input.setText(Math.sqrt(num1) + "");
sign = null;
sign_Box.setText(null);
break;
case "factorial":
val_1 = user_input.getText().toString();
num1 = Double.parseDouble((val_1));
int i = Integer.parseInt(val_1) - 1;

while (i > 0) {
num1 = num1 * i;
i--;
}

user_input.setText(num1 + "");
sign = null;
sign_Box.setText(null);
break;
case "sin":
val_1 = user_input.getText().toString();
num1 = Double.parseDouble((val_1));
user_input.setText(Math.sin(num1) + "");
sign = null;
sign_Box.setText(null);
break;
case "cos":
val_1 = user_input.getText().toString();
num1 = Double.parseDouble((val_1));
user_input.setText(Math.cos(num1) + "");
sign = null;
sign_Box.setText(null);
break;
case "tan":
val_1 = user_input.getText().toString();
num1 = Double.parseDouble((val_1));
user_input.setText(Math.tan(num1) + "");
sign = null;
sign_Box.setText(null);
break;
case "+":
val_2 = user_input.getText().toString();
num1 = Double.parseDouble(val_1);
num2 = Double.parseDouble(val_2);
answer = num1 + num2;
user_input.setText(answer + "");
sign = null;
sign_Box.setText(null);
break;
case "-":
val_2 = user_input.getText().toString();
num1 = Double.parseDouble(val_1);
num2 = Double.parseDouble(val_2);
answer = num1 - num2;
user_input.setText(answer + "");
sign = null;
sign_Box.setText(null);
break;
case "*":
val_2 = user_input.getText().toString();
num1 = Double.parseDouble(val_1);
num2 = Double.parseDouble(val_2);
answer = num1 * num2;
user_input.setText(answer + "");
sign = null;
sign_Box.setText(null);
break;
case "/":
val_2 = user_input.getText().toString();
num1 = Double.parseDouble(val_1);
num2 = Double.parseDouble(val_2);
answer = num1 / num2;
user_input.setText(answer + "");
sign = null;
sign_Box.setText(null);
break;
}

}
}

public void btn_delete(View view) {


if (user_input.getText().equals("")) {
user_input.setText(null);
} else {
int len = user_input.getText().length();
String s = user_input.getText().toString();
if (s.charAt(len - 1) == '.') {
has_Dot = false;
user_input.setText(user_input.getText().subSequence(0,
user_input.getText().length() - 1));

} else {
user_input.setText(user_input.getText().subSequence(0,
user_input.getText().length() - 1));
}
}
}

public void btn_clear(View view) {

user_input.setText(null);
sign_Box.setText(null);
val_1 = null;
val_2 = null;
sign = null;
has_Dot = false;
}

}
SOFTWARE AND HARDWARE REQUIREMENT USED

Software Requirement:
Kotlin (Optional- Either Java or Kotlin will be enough)
eXtensible Markup Language Basics (XML Basics)
Java Programming
Object Oriented Programming Concepts
Windows/Mac/Linux operating system
Android Studio

Hardware Requirement:
External Android for checking progress of application after debugging

Conclusion
The system that has been presented is mainly used in largescale e-learning. In our rapid changing world,
we find it difficult to overcome the learning challenges and possible achievements.
To bridge this gap, the concepts such as e-learning, e- teaching, e-teachers has become the part of
effective education.

REFERENCES

You might also like