Lab1
Lab1
(Kotlin Language)
Lab 1
Prepared By:
Eng.Marwa ELDeeb
Contact info:[email protected]
|Page1
Quick Recap
What is Kotlin?
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.
(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:
|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
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().
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)
}
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)
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)
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