0% found this document useful (0 votes)
15 views

Lab1

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)
15 views

Lab1

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

Mobile Programming

(Kotlin Language)

Lab 1

Prepared By:

Eng.Marwa ELDeeb

Contact info:[email protected]

|Page1
Quick Recap

What is Kotlin?

Kotlin is a modern, trending programming language that was released in 2016 by


JetBrains.

It has become very popular since it is compatible with Java (one of the most popular
programming languages out there), which means that Java code (and libraries) can
be used in Kotlin programs.

Kotlin is used for:

 Mobile applications (specially Android apps)


 Web development
 Server side applications
 Data science
 And much, much more!

Why Use Kotlin?

 Kotlin is fully compatible with Java


 Kotlin works on different platforms (Windows, Mac, Linux, Raspberry Pi, etc.)
 Kotlin is easy to learn, especially if you already know Java
 Kotlin is free to use

Is kotlin compiled language or an interpreted language?

Kotlin is both a compiled language and an interpreted language

(when targeting the JVM, Kotlin is compiled to JVM *.class files, which is a bytecode
format that can later be either interpreted by a JVM, or compiled to the machine code by
the JVM during the program run (JIT))

Important notes:

 To declare function we must use keyword fun.


 The main() function is used to execute code.
 Any code inside the main() function's curly brackets{} will be executed.
 The readLine() function is used to read a line of string in Kotlin (take input).
 The println() function is used to output/print text and insert a new line at the
end of the output.
 The print() function is similar to println() but The difference between them is:
print() does not insert a new line at the end of the output.
 Use // for short comments (Single-line comments).
 Use /* and ends with */ for long comments (Multi-line comments).
 To create a variable, use var or val, and assign a value to it with the equal sign
(=).

|Page2
 The difference between var and val is that variables declared with the var
keyword can be changed/modified, while val variables cannot.
 You can also use the + character to add a variable to another variable.
 For numeric values, the + character works as a mathematical operator.
 Data types in kotlin are divided into different groups:

1. Numbers
2. Characters

you cannot use ASCII values to display certain characters. The value
66 would output a "B" in Java, but will generate an error in Kotlin

Example

val myLetter: Char = 66

println(myLetter) // Error

3. Booleans
4. Strings
5. Arrays

 To convert data type to another type, you must use one of the following
functions: toByte(), toShort(), toInt(), toLong(), toFloat(), toDouble(), toChar() or
toString().

 Operators are used to perform operations on variables and values.

 Kotlin divides the operators into the following groups:

o Arithmetic operators
o Assignment operators
o Comparison operators
o Logical operators

|Page3
Please write the following by using kotlin language

1. Write a program in kotlin to print a welcome and hello text in two separate
lines.
fun main() {
println("Welcome")
println("Hello")

2. Create an integer variable, the integer variable should be named my_int and
should contain the number 30, then print out the value of my_int.

fun main() {
var my_int=30
// var my_int:Int=30

print(my_int)
}

3. Create a floating point variable, the floating point variable should be


named my_float and should contain the number 10.35, then print out the value
of my_float.
fun main() {
var my_float=10.35 // var my_float:Float=30
print(my_float)
}

4. Create variables str_a, char_b and bool_num which contain values "Kotlin",
‘A’, and true.
a) Print out the variables str_a, char_b and bool_num respectively in
separate lines.
fun main() {
var str_a="kotlin"
var char_b="A"
var bool_num= true
println(str_a)
println(char_b)
println(bool_num)
}

|Page4
5. Create three variables (a, b, and c) as double numbers. Assign a value of 9.123
to a, assign a value of 5.1 to b, and then make c equal to the sum of those two
variables. Print out the operation that you made with two those variables and
the contents of the c variable.
fun main() {
var a: Double=9.123
var b: Double=5.1
var c: Double =a+b
println("a+b="+ c)
}

6. Repeat prob5.0 but print out the content of the variable c as an integer.
fun main() {
var a: Double=9.123
var b: Double=5.1
var c: Double =a+b
println("a+b="+ c.toInt())
}

7. Create integers x and y and assign them values 98 and 60, then print out the
value of x and y simultaneously with one command.
fun main() {
var x: Int =98
var y:Int=60
println(""+x+" "+y)
}
or we can change the type of x and y to string.

8. Create integer variables named num1, num2, num3 and assign them values 3,
4 and 5, then calculate the output of the following Arithmetic Operations with
clarifying the calculation that you have made:
a) num1+num2
b) num3-num1
c) num1*num2+num3
d) num1*(num2+num3)
e) num3/num1
f) num3%num1
g) Decrement num2 by 1
fun main() {
var num1=3
var num2=4
var num3=5
var a=num1+num2
var b=num3-num1
var c=num1*num2+num3
var d=num1*(num2+num3)
var e=num3/num1
var f=num3%num1

|Page5
num2-=1
println(a)
println(b)
println(c)
println(d)
println(e)
println(f)

println(num2)

9. Build a basic calculator that can do the multiplication operation.


fun main() {

print("Please Enter first number: ")


var num1: Int= readLine()!!.toInt()
println()
println("Please Enter secound number: ")
var num2: Int= readLine()!!.toInt()
var multp=num1*num2
println("the result of multiplication is: "+ multp)

10.Create variables named x, y and z and assign them values 5, 8 and 12, then
calculate the result of the following comparisons and demonstrate the process
you are doing:
a) Is x equal to y?
b) Is y less than z?
c) Is x greater than z?
d) Is x less than or equal to y?
e) Is y not equal to x?
fun main() {

var x=5
var y=8
var z=12
println(x==y)
println(y<z)
println(x>z )
println(x<=y)
println(y!=x)

|Page6
11.Create variables named a, b, c and d and assign them values 2, 4, 7 and 9, then
calculate the result of the following logical Operations:
a) Is d greater than b and c not equal a?
b) Is b greater than d and a less than d?
c) Is d less than b or c not equal a?
d) Is b greater than d or a less than d?
e) Negates the value of (is a greater than or equal to d)
f) a >= 2 and (a/b) > 2

fun main() {
var a=2
var b=4
var c=7
var d=9
println(d>b && c!=a)
println(b>d && a<d)
println(d<b || c!=a)
println(b>d || a<d)
println(!(a>=d))
println(a >= 2 && a/b > 2)

12.Create variables named student_one, student_two and student_three and


assign them values Omar, omar and Omar, then calculate the output of the
following operations:
a) student_one!= student_two
b) student_two== student_three
fun main() {
var student_one="Omar"
var student_two="omar"
var student_three="Omar"
println(student_one!= student_two)
println(student_two == student_three)

Task
1) Create variables named x, y, z, and g and take the values of them from the
user(this values must be 7,2,20, and 4), then calculate the output of the following
Arithmetic Operations:
a) x+y*z/g
b) y**g
2) Build a basic calculator that can do the summation operation.

|Page7

You might also like