Question

In: Computer Science

anwer in kotlin fun main() { val text = "Kotlin".getFirstAndLast() val firstChar = text["first"] val lastChar...

anwer in kotlin

fun main() {
val text = "Kotlin".getFirstAndLast()

val firstChar = text["first"]
val lastChar = text["last"]

// TODO 2
println()

}

// TODO 1
fun String.getFirstAndLast() = mapOf<String, Char>()

TODO 1:
Make the getFirstAndLast function the extension of the String class with the return type Map <String, Char>

TODO 2:
Add functions to print the values of the firstChar and lastChar variables in the console.

there no more information from this task

i dont know.
this is the full task

Solutions

Expert Solution

The question is complete and correct. To solve the question, the following two tasks have to be carried out in Kotlin:

1. To complete the function getFirstAndLast() which when invoked on a string, returns a map. This map has two strings (first and last) as the key which correspond to the first and last characters of the string on which the function is called. Note that the function is called using a string object ("Kotlin").

2. Once we receive the map, the next task is to show the results by printing them to the console. If the first part is done, then for the second part requires only two print statements.

The code to solve the complete question is below. This has been tested on online Kotlin compiler. If there is difficulty in executing this code then reinstalling Kotlin on your machine can solve the problem.

fun main() {
//Here the function getFirstAndLast() is called on String object "Kotlin"
val text = "Kotlin".getFirstAndLast()
  
//The following two statements extract the first and last character from the returned map (text)
   val firstChar = text["first"]
   val lastChar = text["last"]
  
//This solves second part by printing first and last characters on console
println(firstChar)
println(lastChar)
}


//This function is the solution of the first part
fun String.getFirstAndLast(): Map<String, Char>{
//this keyword refers to the currently calling object. String in our case.
//len stores the length of the string
val len = this.length
//In this statement we create a map which is the solution
val map = mapOf("first" to this.get(0), "last" to this.get(len-1))
//Finally the map is returned
return map
}


Related Solutions

fun main() {     val stringResult = getResult("Kotlin")     val intResult = getResult(100)     // TODO...
fun main() {     val stringResult = getResult("Kotlin")     val intResult = getResult(100)     // TODO 2     println() } // TODO 1 fun <T> getResult(args: T): Int {     return 0 } TODO 1: Create a new generics function with the following conditions: The function name is getResult. Has one type of parameter. If the attached argument is of type Int, then the value returned is the value of the argument multiplied by 5. If the attached argument is...
ANSWER WITH ML CODE PLZ. NOT C++ 1) fun main() = let val yy = [6,4,2,0];...
ANSWER WITH ML CODE PLZ. NOT C++ 1) fun main() = let val yy = [6,4,2,0]; in print (PolyML.makestring ( ***(Remove the first item from yy)****** )) end; 2) fun main() = let val yy = [6,4,2,0]; in print (PolyML.makestring ( ***(Make a list containing only the first item of yy )*** )) end; 3) fun main() = let val yy = [8,6,4,2,0]; in print (PolyML.makestring ( ***(Prepend 5 to the front of yy)*** )) end; 4)fun main() = let...
First create the text file given below. Then complete the main that is given. There are...
First create the text file given below. Then complete the main that is given. There are comments to help you. An output is also given Create this text file: data1.txt -59 -33 34 0 69 24 -22 58 62 -36 5 45 -19 -73 62 -5 95 42 ` Create a project and a Main class and copy the Main class and method given below. First declare the array below the comments that tell you to declare it. Then there...
// TODO 1 class Cat(private val name: String) { var sleep: Boolean = false fun toSleep()...
// TODO 1 class Cat(private val name: String) { var sleep: Boolean = false fun toSleep() { println() } } fun main() { // TODO 2 val gippy = Cat("") gippy.toSleep() gippy.sleep = true gippy.toSleep() } TODO 1: Complete the code in the Cat class with the following conditions: Create a getter setter function for the sleep property in which there is a function to print text: The getter / setter function is called Add code to the toSleep ()...
!) Vigenere cipher Encode the text manybooksoninformationsecurity using the Vigenere cipher with the keyword fun
!) Vigenere cipher Encode the text manybooksoninformationsecurity using the Vigenere cipher with the keyword fun
Which of the following is the first step in implementing Val IT? Multiple Choice Forming programs...
Which of the following is the first step in implementing Val IT? Multiple Choice Forming programs of IT investments Making specific IT investments Recognizing problems with prior IT investments Assessing the readiness to change None of these is the first step
Allow the main process to generate a text file containing the text of this assignment. The...
Allow the main process to generate a text file containing the text of this assignment. The main process will then create two other processes and pass to them the name of the file and two codes (code1 and code2) using a pipe() system call. The codes are two different alphabetic letters such as “a” and “k”. The child processes should search the file for the character in the interval received, compute their frequencies and return them through a separate pipe...
JavaScript Find and Replace Text Main text: a text area with 3 rows and 20 columns....
JavaScript Find and Replace Text Main text: a text area with 3 rows and 20 columns. The default text is “Hello testing”. The text area cannot be empty. • Find text: a text box for the user to key in text he/she wants to find. • Replacement text: a text box for the user to key in the replacement text. If the find text is empty, this element should be disabled (i.e. user cannot key in anything here). • Find...
Write a remove(E val) method for DoublyLinkedList class This method remove the first occurrence of the...
Write a remove(E val) method for DoublyLinkedList class This method remove the first occurrence of the node that contains the val. .
Write a remove(E val) method for CircularlyLinkedList class This method remove the first occurrence of the...
Write a remove(E val) method for CircularlyLinkedList class This method remove the first occurrence of the node that contains the val.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT