0% found this document useful (0 votes)
13 views17 pages

Lesson 1 - Lab 1.2

Uploaded by

Vũ Trịnh Văn
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views17 pages

Lesson 1 - Lab 1.2

Uploaded by

Vũ Trịnh Văn
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 17

ĐẠI HỌC BÁCH KHOA HÀ NỘI

TRƯỜNG CÔNG NGHỆ THÔNG TIN VÀ TRUYỀN THÔNG

BÁO CÁO THỰC HÀNH


PHÁT TRIỂN ỨNG DỤNG CHO THIẾT BỊ DI
ĐỘNG – IT 4785
LAB 1.2

Họ và tên : Trịnh Văn Vũ


Mã số sinh viên: 20215668
NỘI DUNG
1. Welcome..............................................................................................................1
1.1. Create a project in IntelliJ IDEA.....................................................................1
1.2. Open and execute code in Kotlin's REPL (Read-Eval-Print Loop) in IntelliJ
IDEA...................................................................................................................... 2
2. Learn about operators and types........................................................................3
2.1. Explore numeric operators............................................................................3
2.2. Practice using types.......................................................................................4
2.3. Learn the value of variable types..................................................................6
2.4. Learn about strings and characters...............................................................6
3. Compare conditions and booleans......................................................................8
4. Learn about nullability........................................................................................9
4.1. Learn about nullability..................................................................................9
4.2. Learn about the ? and ?: operators...............................................................9
5. Explore arrays, lists, and loops..........................................................................11
5.1. Make lists.....................................................................................................11
5.2. Create arrays................................................................................................11
5.3. Create loops.................................................................................................12
6. Summary............................................................................................................14
7. Learn more.........................................................................................................15
1. Welcome
1.1. Create a project in IntelliJ IDEA

1
2
1.2. Open and execute code in Kotlin's REPL (Read-Eval-Print Loop) in IntelliJ
IDEA

3
2. Learn about operators and types
2.1. Explore numeric operators
- Enter the following expressions in the REP then see the result

4
2.2. Practice using types
- Define a variable of type Int in the REPL.

- Create a new variable, then enter the variable name shown above, followed
by .to

- Select toByte() from the list, then print the variable

5
- Assign a Byte value to variables of different types.

- For the assignments that returned errors, try casting them instead

- Entering different numeric constants

2.3. Learn the value of variable types


- Define variables using val and var and then assign new values to them.

6
- Define some variables and specify the type explicitly.

2.4. Learn about strings and characters


- Create a string template

- Create a string template with an expression in it

7
3. Compare conditions and booleans
- Write an if/else statement

- Using a range in an if statement

- Write an if with multiple cases

- Try out a when statement

8
4. Learn about nullability
4.1. Learn about nullability
- Declare an Int and assign null to it.

- Use the question mark operator, ?, after the type to indicate that a variable can
be null. Declare an Int? and assign null to it.

4.2. Learn about the ? and ?: operators


- Write some code the longer way to check whether the fishFoodTreats variable is
not null. Then decrement that variable.

- Using the ? operator.

- You can also chain null tests with the ?: operator

9
- A point about null pointers

10
5. Explore arrays, lists, and loops
5.1. Make lists
- Declare a list using listOf and print it out

- Declare a list that can be changed using mutableListOf

5.2. Create arrays


- Declare an array of strings using arrayOf. Use the java.util.Arrays.toString() array
utility to print it out.

- An array declared with arrayOf doesn't have a type associated with the
elements, so you can mix types, which is helpful. Declare an array with different
types.

- You can also declare arrays with one type for all the elements. Declare an array
of integers using intArrayOf()

- Combine two arrays with the + operator

11
- Try out different combinations of nested arrays and lists.

- One nice feature of Kotlin is that you can initialize arrays with code instead of
initializing them to 0

5.3. Create loops


- Create an array. Use a for loop to iterate through the array and print the
elements.

- You can loop through the elements and the indexes at the same time

- You can specify ranges of numbers or of characters, alphabetically

12
- Kotlin has while loops, do...while loops, and ++ and -- operators. Kotlin also has
repeat loops.

13
6. Summary
Kotlin is very similar to other languages when it comes to the basics like
operators, lists, and loops, but there are some important differences.
The following features may be different in Kotlin than what you're used to in
other languages:
• Kotlin types can't be implicitly converted—use casting.
• Variables declared with val can only be assigned once.
• Kotlin variables are not nullable by default. Use ? to make variables nullable.
• With Kotlin, you can loop through the index and elements of an array at the
same time in a for loop.
The following Kotlin programming constructs are similar to those in other
languages:
• Arrays and lists can have a single type or mixed types.
• Arrays and lists can be nested.
• You can create loops with for, while, do/while, and repeat.
• The when statement is Kotlin's version of the switch statement, but when is
more flexible.

14
7. Learn more
• Explicit type conversion
• Defining variables
• String templates
• Nullable values
• Lists
• Arrays
• if, when, for, while
• ?: (Elvis) operator

15

You might also like