Lab 2
Lab 2
(Kotlin Language)
Lab 2
Prepared By:
Eng.Marwa ELDeeb
Contact info:[email protected]
|Page1
Quick Recap
Important notes:
name = "John"
println(name)
var name
name = "John"
println(name)
|Page2
println(firstName.plus(lastName))
Concatenation (+) used between strings to add them together to make a new
string
indexOf() function returns the index (the position) of the first occurrence of a
specified text in a string (including whitespace)
println(txt.indexOf("locate")) // Outputs 7
println(txt.indexOf("s")) // Outputs 4
|Page3
The while loop loops through a block of code as long as a specified condition
is true.
The do..while loop is a variant of the while loop. This loop will execute the
code block once, before checking if the condition is true, then it will repeat the
loop as long as the condition is true.
The break statement is used to jump out of a loop ” means exist from the
current loop”.
The continue statement breaks one iteration (in the loop), if a specified
condition occurs, and continues with the next iteration in the loop “means
leave the current iteration and starts a new iteration”.
use the arrayOf() function, and place the values in a comma-separated list
inside it.
To find out how many elements an array have, use the size property.
You can use the in operator to check if an element exists in an array.
the for loop is used to loop through arrays with the in operator.
With the for loop, you can also create ranges of values with ".."
You can also use the break and continue keywords in a range/for loop.
To create your own function, use the fun keyword, and write the name of the
function, followed by parentheses ().
We used functions to output a value. To return a value, use the return
keyword, and specify the return type after the function's parentheses.
return (x + 5)
fun main() {
println(result)
There is also a shorter syntax for returning values. You can use the = operator
instead of return without specifying the return type.
|Page4
fun myFunction(x: Int, y: Int) = x + y
fun main() {
println(result)
fun main() {
print("Please enter a charcter to check if it vowel or not: ")
var ch = readLine()!!
when(ch) {
"a", "e", "i", "o", "u" -> println(ch +" is vowel")
else -> println(ch+ " is consonant")
}
}
2. Use loop to allow user to enter a number and if it is even, print a message
“this is an even number”, then allow him to enter another number. This loop
will stop if the user enter an odd number.
fun main() {
var flag=true
while(flag){
print("Please enter a number to check if it's odd or even: ")
var num:Int = readLine()!!.toInt()
var msg= "is even number"
if(num%2==0){
println("the number you entered is even")
flag=true
}
else{
print("the number you entered is odd")
//break
flag=false
}
}
}
|Page5
3. Write a kotlin program to determine where the character (g) then where is
the character (f) are located inside the given string "languages"
fun main() {
4. Write a function named reverse that string as a parameter then return this
string in reversed order.
fun reverse(str: String): String {
var reverse = ""
println(reverse)
5. Write program that take array of int from user and return the largest number in this array.
fun main() {
print("Enter Array size: ")
val arraySize = readLine()!!.toInt()
println("Enter Array Elements")
val arr = Array<Int>(arraySize) { readLine()!!.toInt() }
//val numArray = intArrayOf(23, -34, 50, 33, 55, 43, 5, -66)
var largest = arr[0]
|Page6
6. Write program to calculates the sum of maximum of 6 positive numbers
entered by the user. If the user enters negative number or zero, it is
skipped from calculation
fun main() {
var number: Int
var sum = 0
for (i in 1..6) {
print("Enter an integer: ")
number = readLine()!!.toInt()
if (number <= 0)
continue
sum += number
}
println("sum = " + sum)
}
var sum = 0
var number: Int
while (true) {
print("Enter a number: ")
number = readLine()!!.toInt()
if (number == 0)
break
sum += number
}
print("sum = "+sum)
}
|Page7
println("Factorial of "+ numb +" is " +factorial(numb))}
Task
9. Write program to check the string is a Palindrome or not.
10. fun main() {
fun main() {
|Page8