BSc(IT) Semester-5
503-Mobile Application Development with Kotlin
Practical Assignment-2

  1. Develop a kotlin program for class Employee with properties: code, name,
    designation , date of birth as DOB, date of joining as DOJ, basic salary, hra, da, dept.
    [Calculate da and hra accordingly:
    (1)If basic salary is less than 30000 then da=60%, hra=20%
    (2)If basic salary more than 30000 and less than 45000 then da=70%,
    hra=30%
    (3) If basic salary more than 45000 then da=85%, hra=40%]
    The behavior of Employee should include:
    a. Accept the input from the user.
    b. Display the data.
    c. To calculate Total salary.
    d. To search for the employees who work in ‘Sales’ Dept.
    e. Calculate the total experience of employees.
    f. Search the employees who have experience more than 5 years.
    1. Develop a kotlin program for class student with properties: rollno, name, marks of
      3 subjects, total, percentage and grade.
      The behavior of student should include:
      a. Accept the input from the user.
      b. Display the data.
      c. Calculate the total marks obtained, percentage and grade of each student.
      d. Display the student who secured 1st rank.

----------------2-----------------

//Design

<?xml version="1.0" encoding="utf-8"?>
<ScrollView 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"
tools:context=".MainActivity">
<androidx.appcompat.widget.LinearLayoutCompat
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/edtRollno"
android:hint="Enter roll no"
/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/edtName"
android:hint="Enter name"
/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/edtSubject1"
android:hint="Enter marks of first subject"
/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/edtSubject2"
android:hint="Enter marks of second subject"
/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/edtSubject3"
android:hint="Enter marks of third subject"
/>
<androidx.appcompat.widget.LinearLayoutCompat
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/btnAdd"
android:layout_weight="1"
android:text="Add"
/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/btnCalculate"
android:layout_weight="1"
android:text="Process"
/>
</androidx.appcompat.widget.LinearLayoutCompat>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/txtDisplay"
android:textSize="60px"
/>
</androidx.appcompat.widget.LinearLayoutCompat>
</ScrollView>

//Kotlin

package com.example.a2q2

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.widget.Button
import android.widget.EditText
import android.widget.TextView

class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val studentList = ArrayList<student>()
val iRollno = findViewById(R.id.edtRollno) as EditText
val stName = findViewById(R.id.edtName) as EditText
val iMark1 = findViewById(R.id.edtSubject1) as EditText
val iMark2 = findViewById(R.id.edtSubject2) as EditText
val iMark3 = findViewById(R.id.edtSubject3) as EditText
val btnProcess = findViewById(R.id.btnCalculate) as Button
val btnAdd = findViewById(R.id.btnAdd) as Button
val stDisplay = findViewById(R.id.txtDisplay) as TextView

⎗
✓
        btnAdd.setOnClickListener(View.OnClickListener {
            // Create a new student object and add it to the list
            val newStudent = student(
                iRollno.text.toString().toInt(),
                stName.text.toString(),
                iMark1.text.toString().toInt(),
                iMark2.text.toString().toInt(),
                iMark3.text.toString().toInt()
            )
            studentList.add(newStudent)
        })

        btnProcess.setOnClickListener(View.OnClickListener {
            val displayText = StringBuilder()

            for (i in 0 until studentList.size) {
                val currentStudent = studentList[i]
                currentStudent.calculateTotal()
                currentStudent.calculatePercentage()
                currentStudent.calculateGrade()

                displayText.append(currentStudent.toString()).append("\n\n")
            }

            stDisplay.text = displayText.toString()
        })
    }
}

open class student(
    val rollno: Int,
    val sname: String,
    val m1: Int,
    val m2: Int,
    val m3: Int
) {
    var total: Int = 0
    var percentage: Int = 0
    var grade: String = ""

    fun calculateTotal() {
        total = m1 + m2 + m3
    }

    fun calculatePercentage() {
        percentage = total / 3
    }

    fun calculateGrade() {
        grade = when {
            percentage >= 80 -> "A"
            percentage >= 60 -> "B"
            percentage >= 40 -> "C"
            else -> "Fail"
        }
    }

    override fun toString(): String {
        return "Roll No: $rollno\nName: $sname\nSubject 1: $m1\nSubject 2: $m2\nSubject 3: $m3\nTotal: $total\nPercentage: $percentage%\nGrade: $grade"
    }
}
Edit

Pub: 25 Mar 2023 04:00 UTC

Edit: 25 Aug 2023 05:23 UTC

Views: 142