This practical demonstrates various fundamental programming concepts using Kotlin. It covers variables, data types, type conversion, user input, control flow, functions, recursion, arrays, ArrayList, classes, constructors, operator overloading, and matrix operations.
The practical is designed to provide hands-on understanding of basic and intermediate Kotlin programming concepts.
The objectives of this practical are:
- To understand different Kotlin data types and variables.
- To perform type conversions in Kotlin.
- To accept and display student information.
- To use conditional statements and
whenexpressions. - To create and use user-defined functions.
- To understand recursion through factorial calculation.
- To perform different operations on arrays.
- To find the maximum value from an
ArrayList. - To understand classes and constructors.
- To implement operator overloading.
- To perform matrix addition, subtraction, and multiplication.
- To customize object output using
toString().
- Programming Language: Kotlin
- JDK: 8 or later
- IDE: IntelliJ IDEA / Android Studio
- File Extension:
.kt
Create and display variables using different Kotlin data types.
The program demonstrates:
IntDoubleFloatLongShortByteCharBooleanString
val integerValue: Int = 100
val doubleValue: Double = 25.50
val floatValue: Float = 15.5f
val longValue: Long = 100000L
val shortValue: Short = 10
val byteValue: Byte = 5
val charValue: Char = 'A'
val booleanValue: Boolean = true
val stringValue: String = "Kotlin"The values are displayed using println().
Kotlin provides functions for converting values from one data type to another.
The program demonstrates:
- Integer to Double
- String to Integer
- String to Double
val number: Int = 10
val doubleNumber = number.toDouble()
val stringNumber = "20"
val integerNumber = stringNumber.toInt()
val stringDouble = "25.5"
val doubleValue = stringDouble.toDouble()The program accepts student information from the user and displays the entered details.
- Student Name
- Enrollment Number
- Branch
- Semester
- College Name
print("Enter Student Name: ")
val name = readLine()
print("Enter Enrollment Number: ")
val enrollmentNo = readLine()
print("Enter Branch: ")
val branch = readLine()The entered information is then displayed using println().
The program checks whether a given number is odd or even.
The condition is implemented using the modulo operator %.
println(
if (number % 2 == 0)
"Even Number"
else
"Odd Number"
)- If
number % 2 == 0, the number is Even. - Otherwise, the number is Odd.
A Kotlin when expression is used to display the month name based on the month number entered by the user.
val month = readLine()!!.toInt()
when (month) {
1 -> println("January")
2 -> println("February")
3 -> println("March")
4 -> println("April")
5 -> println("May")
6 -> println("June")
7 -> println("July")
8 -> println("August")
9 -> println("September")
10 -> println("October")
11 -> println("November")
12 -> println("December")
else -> println("Invalid Month")
}A user-defined function is created to perform arithmetic operations on two numbers.
The following operations are performed:
- Addition
- Subtraction
- Multiplication
- Division
fun arithmeticOperations(a: Double, b: Double) {
println("Addition = ${a + b}")
println("Subtraction = ${a - b}")
println("Multiplication = ${a * b}")
println("Division = ${a / b}")
}Functions make the program more organized, reusable, and easier to maintain.
The factorial of a number is calculated using recursion.
n! = n Γ (n-1) Γ (n-2) Γ ... Γ 1
For example:
5! = 5 Γ 4 Γ 3 Γ 2 Γ 1
5! = 120
fun factorial(n: Int): Long {
return if (n <= 1)
1
else
n * factorial(n - 1)
}The function calls itself until it reaches the base condition.
This section demonstrates different array operations in Kotlin.
The following functions are demonstrated:
Arrays.deepToString()contentDeepToString()IntArray.joinToString()
println(Arrays.deepToString(array))
println(array.contentDeepToString())
println(intArray.joinToString())Different loop and range operations are used:
rangedownTountil
for (i in 0..4) {
println(array[i])
}
for (i in 4 downTo 0) {
println(array[i])
}
for (i in 0 until array.size) {
println(array[i])
}An integer array is sorted manually using a sorting algorithm such as Bubble Sort.
Example:
Original Array:
5 2 8 1 3
Sorted Array:
1 2 3 5 8
Kotlin's built-in sorting functions can also be used.
array.sort()An ArrayList of integers is created, and the maximum number is found.
val numbers = arrayListOf(10, 25, 5, 40, 15)
val maximum = numbers.maxOrNull()
println("Maximum Number = $maximum")The program identifies the largest value stored in the ArrayList.
This section demonstrates the creation of classes and constructors in Kotlin.
A Car class is created with the following properties:
- Type
- Model
- Price
- Owner
- Miles Driven
The Car class implements functions to:
- Get car information
- Get original car price
- Calculate current car price
- Display car information
class Car(
val type: String,
val model: String,
val price: Double,
val owner: String,
val milesDriven: Double
) {
fun getCarInformation() {
println("Type: $type")
println("Model: $model")
println("Owner: $owner")
println("Miles Driven: $milesDriven")
}
fun getOriginalCarPrice(): Double {
return price
}
fun getCurrentCarPrice(): Double {
return price
}
fun displayCarInformation() {
getCarInformation()
println("Original Price: $price")
}
}This demonstrates how properties, constructors, and member functions work together inside a class.
Operator overloading allows operators such as +, -, and * to be given specific behavior for user-defined classes.
In Kotlin, operator functions are declared using the operator keyword.
operator fun plus(other: Matrix): MatrixThis allows two Matrix objects to be added using:
matrix1 + matrix2A Matrix class is created to perform:
- Matrix Addition
- Matrix Subtraction
- Matrix Multiplication
For two matrices A and B:
A + B
Each corresponding element is added.
A - B
Each corresponding element is subtracted.
Matrix multiplication follows the standard mathematical matrix multiplication rule.
The toString() function is overloaded in the Matrix class to provide customized matrix output.
override fun toString(): String {
return data.joinToString("\n") {
it.joinToString(" ")
}
}This allows a matrix object to be printed in a readable format using:
println(matrix)If all concepts are implemented in a single Kotlin file, the project can be organized as:
Kotlin-Practical-1/
β
βββ README.md
βββ Practical1.kt
Contains all programs from:
1.1 Variables and Data Types
1.2 Type Conversion
1.3 Student Information
1.4 Odd or Even
1.5 Month Name
1.6 User-Defined Function
1.7 Factorial Using Recursion
1.8 Array Operations
1.9 Maximum from ArrayList
1.10 Car Class and Constructor
1.11 Operator Overloading and Matrix Operations
Before running the program, install:
- JDK 8 or higher
- Kotlin
- IntelliJ IDEA or Android Studio
- Open IntelliJ IDEA.
- Create a new Kotlin project.
- Create a Kotlin file named
Practical1.kt. - Add the program code.
- Click the Run button.
- Follow the instructions shown in the console.
- Open Android Studio.
- Create/open a Kotlin project.
- Add
Practical1.kt. - Enter the required code.
- Run the program.
Compile the Kotlin program:
kotlinc Practical1.kt -include-runtime -d Practical1.jarRun the program:
java -jar Practical1.jar===== 1.1 DATA TYPES =====
Integer: 100
Double: 25.5
Float: 15.5
Long: 100000
Short: 10
Byte: 5
Char: A
Boolean: true
String: Kotlin
===== 1.2 TYPE CONVERSION =====
Integer to Double: 100.0
String to Integer: 200
String to Double: 25.5
===== 1.3 STUDENT INFORMATION =====
Enter Name: Yash
Enter Enrollment No: 12345
Enter Branch: Computer Engineering
Student Name: Yash
Enrollment No: 12345
Branch: Computer Engineering
===== 1.4 ODD OR EVEN =====
Enter Number: 10
10 is Even
===== 1.5 MONTH NAME =====
Enter Month Number: 8
Month: August
===== 1.6 ARITHMETIC OPERATIONS =====
Addition: 30
Subtraction: 10
Multiplication: 200
Division: 2
===== 1.7 FACTORIAL =====
Enter Number: 5
Factorial: 120
===== 1.8 ARRAY OPERATIONS =====
Original Array: 5 2 8 1 3
Sorted Array: 1 2 3 5 8
===== 1.9 ARRAYLIST MAXIMUM =====
ArrayList: [10, 25, 5, 40, 15]
Maximum Number: 40
===== 1.10 CAR INFORMATION =====
Type: Sedan
Model: XYZ
Price: 800000
Owner: Yash
Miles Driven: 25000
===== 1.11 MATRIX OPERATIONS =====
Matrix Addition:
6 8
10 12
Matrix Subtraction:
-4 -4
-4 -4
Matrix Multiplication:
19 22
43 50
After completing this practical, the student will be able to:
- Declare and use different Kotlin variables and data types.
- Convert values between different data types.
- Accept input from users.
- Use
if-elseandwhenexpressions. - Create and call user-defined functions.
- Understand recursive functions.
- Work with arrays and
ArrayList. - Use loops and ranges such as
..,until, anddownTo. - Sort arrays manually and using built-in functions.
- Create classes and constructors.
- Define properties and member functions.
- Understand operator overloading.
- Implement matrix operations.
- Override
toString()for customized object representation.
This practical covers important Kotlin programming concepts from basic variables and input handling to object-oriented programming and operator overloading. By completing all the sections, students gain practical experience with data types, type conversion, control flow, functions, recursion, arrays, collections, classes, constructors, operator overloading, and matrix operations in Kotlin.