Posts

Generic Classes

Classes in Kotlin can have type parameters to create generic classes, just like in Java // File: Main.kt // Programmer: Engineer Nolverto Urias Obeso // Creation Date: 06/24/2023 // Email: nolvertou@gmail.com // Description: Using generic classes fun main () { val stringList: MyList<String> // Using generic Class val intList: MyList<Int> = MyList() intList.addItem( 1 ) println (intList.get( 1 )) } class MyIntList{ fun get (pos: Int) : Int{ return 0 } fun addItem (item: Int){} } class MyShortList{ fun get (pos: Int): Short{ return 0 } fun add (item: Short){} } // Generic Class class MyList< T >{ fun get (pos: T ): T { // TODO("implement") return pos } fun addItem (item: T ){} } Example 2: Main.kt import generics.Aquarium // File: Main.kt // Programmer: Engineer Nolverto Urias Obeso // Creation Date: 06/24/2023 // Email: nolvertou@gmail.c...

Practice Time: Extension Functions

  Practice Time It can be useful to know the weight of a book, for example, for shipping. The weight of a book can change because sometimes pages get torn, and the page count changes. While calculating the weight could be defined as a method, it’s more like a helper function. Besides, it would hurt a book's feelings to have a method that tears up its pages. Add a mutable property  pages  to  Book . Create an extension function on  Book  that returns the weight of a book as the page count multiplied by 1.5 grams. Create another extension,  tornPages() , that takes the number of torn pages as an argument and changes the page count of the book. Write a class  Puppy  with a method  playWithBook()  that takes a book as an argument, and removes a random number of pages from the book. Create a  puppy  and give it a  book  to play with, until there are no more pages. Main File // File: Main.kt // Programmer: ...

Extension Functions

 Kotlin provides a convenient syntax for declaring extension functions. These allow you to add functions to an existing class without having access to its source code. Extensions do not actualy modify the classes they extend. By defining an extension, you do not insert new members into the class. Extension functions are defined outside of the class they extend, so, they don't have access to private variables.  You can also define extension properties // File: Main.kt // Programmer: Engineer Nolverto Urias Obeso // Creation Date: 06/23/2023 // Email: nolvertou@gmail.com // Description: Using extension functions and properties fun main () { println ( "Does it have spaces" . hasSpaces ()) // Output: true val aquariumPlant = AquariumPlant( "Green" , 100 ) println (aquariumPlant. isRed ()) // Output: false val aquariumPlant2 = AquariumPlant( "Red" , 2 ) println (aquariumPlant2. isRed ()) // Output: true...

Practice Time: Collections

  Practice Time One book is rarely alone, and one author rarely writes just one book. Create a  Set  of book titles called  allBooks , for example, by William Shakespeare. Create a  Map  called  library  that associates the set of books,  allBooks , to the author. Use the collections function  any()  on  library  to see if any of the books are “Hamlet’ Create a  MutableMap  called  moreBooks , and add one title/author to it. Use  getOrPut()  to see whether a title is in the map, and if the title is not in the map, add it. // File: Main.kt // Programmer: Engineer Nolverto Urias Obeso // Creation Date: 06/21/2023 // Email: nolvertou@gmail.com // Description: Using MapList and SetList Collection fun main (args: Array<String>) { // TODO 1: Create a Set of book titles called allBooks, for example, by William Shakespeare. val allBooks = setOf ( "Daemon" , "Cr...

Collections: MapList

// File: Main.kt // Programmer: Engineer Nolverto Urias Obeso // Creation Date: 06/21/2023 // Email: nolvertou@gmail.com // Description: Using MapList Collection fun main () { val cures: Map<String , String> = mapOf ( "white spots" to "Ich" , "red sores" to "hole disease" ) println (cures[ "white spots" ]) // Output: Ich println (cures.get( "red sores" )) // Output: hole disease println (cures.getOrDefault( "bloating" , "sorry I don't know" )) // Output: sorry I don't know println (cures. getOrElse ( "bloating" ) { "No cure for this" } ) // Output: No cure for this val inventory: MutableMap<String , Int> = mutableMapOf ( "fishnet" to 1 ) println (inventory) // Output: {fishnet=1} inventory.put( "tank scrubber" , 4 ) println (inventory) // Output: {fishnet=1, tank scrubber=4} inventory.re...

Collections: MutableList operations

  // File: Main.kt // Programmer: Engineer Nolverto Urias Obeso // Creation Date: 06/21/2023 // Email: nolvertou@gmail.com // Description: Using MutableLists operations fun main () { val symptoms: MutableList<String> = mutableListOf ( "white spots" , "red spots" , "not eating" , "bloated" , "belly up" ) println (symptoms) // Output: [white spots, red spots, not eating, bloated, belly up] symptoms.add( "white fungus" ) println (symptoms) // Output: [white spots, red spots, not eating, bloated, belly up, white fungus] symptoms.remove( "white fungus" ) println (symptoms) // Output: [white spots, red spots, not eating, bloated, belly up] println (symptoms.contains( "red" )) // Output: false println (symptoms.contains( "red spots" )) // Output: true println (symptoms.subList( 3 , symptoms. size )) // O...

Collections: Lists and MutableLists

  // File: Main.kt // Programmer: Engineer Nolverto Urias Obeso // Creation Date: 06/21/2023 // Email: nolvertou@gmail.com // Description: Using Collections (lists and mutable lists) fun main () { val testList: List<Int> = listOf ( 11 , 12 , 13 , 14 , 15 , 16 , 17 , 18 , 19 , 20 ) println (testList) // Output: [11, 12, 13, 14, 15, 16, 17, 18, 19, 20] println ( reverseList (testList)) // Output: [20, 19, 18, 17, 16, 15, 14, 13, 12, 11] println ( reverseListAgain (testList)) // Output: [20, 19, 18, 17, 16, 15, 14, 13, 12, 11] println (testList. reversed ()) // Output: [20, 19, 18, 17, 16, 15, 14, 13, 12, 11] } fun reverseList (list: List<Int>): List<Int>{ val result: MutableList<Int> = mutableListOf <Int> () for (i in list. indices ){ result.add(list[list. size -i- 1 ]) } return result } fun reverseListAgain (list: List<Int>): ...