0% found this document useful (0 votes)
7 views4 pages

Advanced Kotlin Tasks

Uploaded by

miqbal
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)
7 views4 pages

Advanced Kotlin Tasks

Uploaded by

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

Clone the following project and set up IntelliJ IDEA:

https://github.com/MarcinMoskala/advanced-kotlin-workshop-tasks

Task 1. Implement sendMessage function in MessageService on MessageService.kt


With parameters:
● to of type String and default value "all"
● title of type String and default value ""
● content of type String and default value ""
Without result type (or returning Unit)

Function should get emails using findEmailAddresses


Then for each email, it should send emails using messageSender function sendEmail
For iterate over emails, use

for(email in emails) {
// …
}

Task 2. Implement functions in StudentController. This is how this API should behave:
● When asked for a student (getStudent), the user should be loaded from the
database by id and returned.
● When asked for a student that does not exist (getStudent), the ApiError should be
thrown with code 400 and message "No user with id {id}".
● When asked for a student (getStudent), the analytics counter is bumped.
● When asked for students (getStudents), all students are returned and they are
sorted by surname

A few hints:
● In Kotlin, there is no new keyword. Just skip it.
● null result means that the result cannot be produced. If the return type is nullable,
be prepared.
● Use listOf to create a list with a concrete set of elements:

val list = listOf(1)

● You can add another list or an element using a plus:

val list2 = list + 1 + list

● You can use for-loop to iterate over a list:

for (elem in list2) {


print(elem)
}
Task 3. Chean-up in Tree.

Task 4. Nullability

Implement function sendMessageToClient in the file basics/Task.kt:

fun sendMessageToClient(client: Client?, message: String?, mailer:


Mailer) {
// TODO
}

It should send an email if the client, personal info, email, and message are not null.

Task 5. Implement class Businessman and Student on basics/Person.kt

● They both should implement Person


● They both can buy alcohol only if over 21
● Businessman says hello by “Good morning”, Student by “Hi”.
● Businessman cheers by “Hello, my name is ${his name}, nice to see you ${cheered
person name}”, Student by “Hey ${cheered person name}, I am $${his name}”.

Tests in basics/PersonTest.

Task 6. Make a factores that can be used this way:

val pizza1 = Pizza.hawaiian()


val pizza2 = PizzaFactory.hawaiian()

They should all produce a pizza with 1 cheese, ham and pineapple the same as this one:
Pizza(cheese = 1, pineapple = 1, ham = 1)

Task 7. Implement the extension functions Int.r(), Pair<Int, Int>.r() to support


the following manner of creating rational numbers:
● 1.r()
● Pair(1, 2).r()

Extra Task. Define the following extension functions in basics/TreeOperations.kt:


● count - that counts the number of leaves.
● countAll - that counts the number of leaves and nodes.
● height - that returns the height of this tree, that is the number of elements between
root and the furthest leaf, including root and this leaf.
● biggest - that finds the biggest element kept by the leaf. This should be defined
only for trees whose type parameter is comparable (implements Comparable<T>).
● sum - that calculates the sum of elements in the Tree<Int>.
● contains - that checks if a value elem is in any leaf on this tree.
● plus - that creates a node with tree1 and tree2 as leaves.
All these functions should use recurrence, smart casting, and when structure. Make these
functions generic.

sealed class Tree<T> {


override fun toString(): String = when (this) {
is Leaf -> value.toString()
is Node -> "($left, $right)"
}
}

data class Leaf<T>(val value: T) : Tree<T>()


data class Node<T>(val left: Tree<T>, val right: Tree<T>) :
Tree<T>()

Task 8. Functional

Implement functions in functional/Functional.kt. All functions should behave like


FunctionsClassic.
● Specify function types on AnonymousFunctionalTypeSpecified and
LambdaFunctionalTypeSpecified.
● Implement AnonymousFunctionalTypeSpecified functions using annonymus
functions with variable type specified.
● Implement AnonymousFunctionalTypeInferred functions using annonymus
functions with parameter types specified.
● Implement LambdaFunctionalTypeSpecified functions using lambda
expressions with variable type specified.
● Implement LambdaFunctionalTypeInferred functions using lambda
expressions with parameter types specified.
● Implement FunctionReferenceFunctionalTypeInferred functions using
member function references.
● Implement BoundedFunctionReferenceFunctionalTypeInferred functions
using bounded function references to the object classic.

Task 9. On the file collections/Examples.kt you see how forEach, flatMap, and
filter are implemented. Implement map.

Task 10. Add function plusAt at collections/PlusAt.kt to insert an element into a


specific index on an immutable list.

Task 11. Implement makePassingStudentsListText method to display a list of students


who have got more than 15 points in the semester and a result of at least 50. Display in
alphabetical order (surname then name), in the format: “{name} {surname}, {result}”
Task 12. Implement makeBestStudentsList method to display the best 10 students so
they can get an internship. You should compare them by their result (higher is better). To get
an internship, students need to have got at least 30 points in the semester and a result of at
least 80. The best student gets $5000, the next 3 get $3000 and the next 6 get $1000.
Display students in alphabetical order (compare first surname then name) in the format
“{name} {surname}, ${internship size}”

Task 13: In extra/HtmlDsl.kt, add 4 functions to make it possible to define a HTML


table using the following builder:

table {
tr {
td { +"A" }
td { +"B" }
}
tr {
td { +"C" }
td { +"D" }
}
}

Task 14. Change how Response, Success, and ErrorResponse are defined in
generics/Response.kt to allow the use presented in the same file.

Task 15. Implement NotNull.

Make a delegate, that behaves like lateinit variable, but:


1. Works for Int.
2. Works for all not-nullable types.
3. Works for nullable types.

Tests in LateinitTest.kt.

Extra Task. Implement Mutable lazy


Lazy delegate works only for read-only properties. Implement mutable lazy - lazy delegate
that we will be able to use to any property:

var game: Game? by mutableLazy { readGameFromSave() }

Task 16. Modify classes in the Java package to allow the following Java usage:

KotlinTopLevel.topLevelFunction("Some arg")
KotlinTopLevel.topLevelFunction()
KotlinClass.staticFunction();
KotlinPerson person = new KotlinPerson("Marcin");

Coroutines tasks are in a separated document.

You might also like