Skip to content

WA2. Kotlin and Android Studio

Question 1

Install android studio on your machine and share screenshots of each step of installation.

  • To be honest, I had Android Studio installed on my machine before this assignment. I use MacOS laptop. However, I reinstalled for the sake of this assignment. I will share the screenshots of the installation process below:

  • Visited the official website of Android Studio: https://developer.android.com/studio?utm_source=android-studio and clicked the download where they asked me to accept their terms and conditions.

  • Installed the downloaded file.
  • Opened the installed Android Studio and created a new project, and my first kotlin script within that project.

Question 2

Write a program in Kotlin using android studio to create two arrays - one with the first names and the other with the last names corresponding to the first names in array one. Then create a third array that will contain the full names which are obtained from these two arrays by concatenating the first and last names. You can use the below set of names to test your program. You need to share your program and screenshot of your output printing all three arrays - one containing all first names, the second containing last names, and the third containing full names

  • The code is below:
val firstNames = listOf<String>(
    "James",
    "Joseph",
    "Art",
    "Len",
    "Don",
    "Sima",
    "Mitsue",
    "Leo",
    "Sage",
    "Krish",
    "Minna",
    "Abe",
    "Kyle",
    "Graciela",
    "Cammi",
    "Matt",
    "Mel",
    "Glady",
    "Yukee",
);

val lastNames = listOf<String>(
    "Bhatt",
    "Darakjy",
    "Veere",
    "Paprocki",
    "Foller",
    "Morasca",
    "Toll",
    "Dilli",
    "Wiezer",
    "Marrier",
    "Amigo",
    "Maclead",
    "Caldarera",
    "Roota",
    "Albares",
    "Poquette",
    "Garufi",
    "Rim",
    "Whobrey",
);

val fullNames = mutableListOf<String>()

for (i in 0.. firstNames.size -1) {
    val fName = firstNames.get(i);
    val lName = lastNames.get(i);
    fullNames.add(i, "$fName $lName");
}

println("First Names: ");
for (fn in firstNames) {
    print("$fn, ");
}
println("\n");

println("Last Names: ");
for (ln in lastNames) {
    print("$ln, ");
}
println("\n");

println("Full Names: ");
for (fln in fullNames) {
    print("$fln, ");
}
println("\n");
  • The output is below:


Question 3

  • Explain types of collection (Set, List, Map) in Kotlin using examples

List

  • Ordered collection of elements that allows duplicates.
  • Immutable: val l = listOf(1,2,3) or val l = listOf<Int>(1,2,3), you can not add or remove elements from this list.
  • Mutable: can be created using functions like: mutableListOf, arrayListOF, and ArrayList.
  • Advantages: Immutability, Type safety, Convenience (to store collection of fixed size that is not changing).
  • Disadvantages: Immutability, overhead, limited functionality.
  • Examples:
var l = listOf<Int>(1,2,3)
l.get(1) // get element at index 1
l[1] // get element at index 1
l.first() // get first element
l.last() // get last element
for (i in l) {} // iterate over elements
var it = l.listIterator() // get iterator
while (it.hasNext()) { } // iterate over elements using iterator
for (i in 0 until l.size) {} // iterate over elements using indices
l.sorted() // gives sorted list in ascending order
l.sortedDescending() // gives sorted list in descending order
l.contains(1) // check if list contains element `1`
l.containsAll(listOf(1,2)) // check if list contains all elements in the list

Set

  • Unordered collection of elements that does not allow duplicates.
  • Immutable: val s = setOf(1,2,3) or val s = setOf<Int>(1,2,3), you can not add or remove elements from this set.
  • Mutable: can be created using functions like: mutableSetOf, hashSetOf, and HashSet.
  • Examples:
var s = setOf<Int>(1,2,3)
s.elementAt(1) // get element at index 1
s.indexOf(2) // get index of element `2`
s.lastIndexOf(2) // get last index of element `2`
s.first() // get first element
s.last() // get last element
s.count() // get number of elements
s.max() // get maximum element
s.min() // get minimum element
s.sum() // get sum of elements
s.average() // get average of elements
s.contains(1) // check if set contains element `1`
s.containsAll(listOf(1,2)) // check if set contains all elements in the list
s.isEmpty() // check if set is empty, empty sets are equal to each other `setOf<Int>() == setOf<Int>()`

Map

  • Collection of k**ey-value pairs** that does not allow duplicate keys.
  • Immutable: val m = mapOf(1 to "one", 2 to "two", 3 to "three") or val m = mapOf<Int, String>(1 to "one", 2 to "two", 3 to "three"), you can not add or remove elements from this map.
  • Mutable: can be created using functions like: mutableMapOf, hashMapOf, and HashMap.
  • Examples:
val m = mutableMapOf(1 to "one", 2 to "two", 3 to "three");
m.get(1) // get value for key `1`
m[1] // get value for key `1`
m.keys // get set of keys
m.values // get collection of values
m.entries // get set of key-value pairs
m.containsKey(1) // check if map contains key `1`
m.containsValue("one") // check if map contains value `one`
```

---

References