Practice Lab 2 MAD
Practice Lab 2 MAD
MAHESHWARI
KOTLIN PROGRAMS
CODE:
fun main() {
if (char.isWhitespace()) continue
frequencyMap[char] = frequencyMap.getOrDefault(char, 0) + 1
println("Character frequencies:")
println("$char: $count")
OUTPUT:
EXPLANATION:
Download the Kotlin 2.1.0 from github and extract it in the folder.Copy the path and store into the
program files of windows.Now check for Kotlin by kotlinc command in command prompt.Write the code
for frequency of characters and save it as .kt file.Compile by cmd: kotlinc charfreq.kt -include-runtime -
d charfreq.jar and after run by cmd:java -jar charfreq.jar. Now program will give output.
CODE:
fun main() {
if (it.isUpperCase()) it.lowercaseChar()
else it.uppercaseChar()
}.joinToString("")
OUTPUT:
3. Write a Kotlin program to implement a basic currency converter. The program should:
1. Allow the user to select the source currency and the target currency (e.g., USD, EUR,INR).
2. Accept the amount in the source currency as input.
3. Convert the amount to the target currency based on predefined exchange rates.
4. Display the converted amount.
CODE:
fun main() {
return
return
}
val conversionRate = exchangeRates[sourceCurrency]!![targetCurrency]!!
OUTPUT:
4. Implement Guess the number Game. The program randomly generates a number between 1
and 100. The player has to guess the number, and the program provides hints like”too high”
or “too low”. The game ends when the player guesses the correct number.
CODE:
import kotlin.random.Random
fun main() {
var attempts = 0
println("I'm thinking of a number between 1 and 100. Can you guess it?")
do {
guess = readLine()?.toIntOrNull()
if (guess == null || guess < 1 || guess > 100) {
continue
attempts++
when {
OUTPUT:
5.Implement simple Login page using Kotlin.
MainActivity.xml
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="16dp">
<EditText
android:id="@+id/editTextUsername"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Username"
android:inputType="text" />
<EditText
android:id="@+id/editTextPassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Password"
android:inputType="textPassword"
android:layout_marginTop="8dp" />
<Button
android:id="@+id/buttonLogin"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Login"
android:layout_marginTop="16dp" />
</LinearLayout>
-----------
MainActivity.kt
package com.example.loginpage
import android.os.Bundle
import android.widget.Button
import android.widget.EditText
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
loginButton.setOnClickListener {
if (username.isEmpty() || password.isEmpty()) {
} else {