0% found this document useful (0 votes)
20 views8 pages

Lab 2

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views8 pages

Lab 2

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Mobile Programming

(Kotlin Language)

Lab 2

Prepared By:

Eng.Marwa ELDeeb

Contact info:[email protected]

|Page1
Quick Recap

Important notes:

 A string contains a collection of characters surrounded by double quotes.


 If you want to create a String without assigning the value (and assign the
value later), you must specify the type while declaring the variable:

This works fine:

var name: String

name = "John"

println(name)

This will generate an error:

var name

name = "John"

println(name)

 Use [ ] to access the characters in a string by referring to its index number.

var txt = "Hello World"

println(txt[0]) // first element (H)

println(txt[2]) // third element (l)

 plus() function to concatenate two strings.

var firstName = "John "

var lastName = "Doe"

|Page2
println(firstName.plus(lastName))

 Concatenation (+) used between strings to add them together to make a new
string

var firstName = "John"

var lastName = "Doe"

println(firstName + " " + lastName)

 length function: to get the length of a string.

var txt = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"

println("The length of the txt string is: " + txt.length)

 indexOf() function returns the index (the position) of the first occurrence of a
specified text in a string (including whitespace)

var txt = "Please locate where 'locate' occurs!"

println(txt.indexOf("locate")) // Outputs 7

println(txt.indexOf("s")) // Outputs 4

 if statement to specify a block of Kotlin code to be executed if a condition is


true.
 else if statement to specify a new condition if the first condition is false.
 Instead of writing many if..else expressions, you can use the when
expression.
 The when expression is similar to the switch statement in Java.
 you can also create ranges of values with ".."

|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.

fun myFunction(x: Int): Int {

return (x + 5)

fun main() {

var result = myFunction(3)

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() {

var result = myFunction(3, 5)

println(result)

Please write the following by using kotlin language

1. Check whether an alphabet is vowel or consonant using when statement

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() {

var str1: String = "language"


println (str1.indexOf("g"))
println (str1.indexOf("f"))
}

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 = ""

for (i in str.length - 1 downTo 0) {


reverse += str[i]
//println(reverse)
}
return (reverse)
}
fun main() {
print ("Please enter word to reverse: ")
var str = readLine()!!

var reverse = reverse(str)

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]

for (num in arr) {


if (largest < num)
largest = num
}

println("Largest element = " +largest)

|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)
}

7. Write program to calculates the sum of numbers entered by the


user until user enters 0.
fun main() {

var sum = 0
var number: Int

while (true) {
print("Enter a number: ")
number = readLine()!!.toInt()

if (number == 0)
break

sum += number
}

print("sum = "+sum)
}

8. Write function to Find Factorial of Given Numbe


fun factorial(num: Long): Long{
var factorial: Long = 1
var i = 1

while (i <= num) {


factorial *= i
i++
}
return factorial
}
fun main() {
print("Enter Number:")
val numb : Long = readLine()!!.toLong()
//factorial(numb)

|Page7
println("Factorial of "+ numb +" is " +factorial(numb))}

Task
9. Write program to check the string is a Palindrome or not.
10. fun main() {

println("Enter word to check Palindrome: ")


val originalString = readLine()!!
var reverseString = ""
var length = originalString.length

for (i in (length - 1) downTo 0) {


reverseString = reverseString + originalString[i]
}
if (originalString==reverseString) {
println("The given string is Palindrome")
} else {
println("The given string is NOT a Palindrome")
}}

11. write Program to Generate and Print Multiplication Table .

fun main() {

print("Enter the number: ")


var num: Int= readLine()!!.toInt()

println("Multiplication Table: \n")


for (i in 1..10) {
val product = num * i
println(""+num + "*"+ i +"=" +product)
}
}

|Page8

You might also like