Answer: The primary constructor is part of the class header. Unlike Java, you don't need to declare a constructor in the body of the class. Here's an example:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"app_name": "My App", | |
"example_text_view_key": "Hello, World!", | |
"settings_title": "Settings", | |
"language_title": "Language", | |
"select_language": "Select Language", | |
"display_name": "English", | |
"ok_button": "OK", | |
"cancel_button": "Cancel", | |
"home_feature": "Changed Home" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"app_name": "My App", | |
"example_text_view_key": "Hello, World!", | |
"settings_title": "Settings", | |
"language_title": "Language", | |
"select_language": "Select Language", | |
"display_name": "English", | |
"ok_button": "OK", | |
"cancel_button": "Cancel", | |
"home_feature": "Changed Home" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* ANY | |
*/ | |
fun Any.toJson(): String = GsonInitializer.toJson(this) | |
/** | |
* String to Inches | |
**/ | |
internal fun convertStringToInches(centimeters: String) : String = | |
(BigDecimal(centimeters.toDouble() * 0.393701).setScale(3, RpundingMode.HALF_EVEN)).toString() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
Usage for extensions. | |
Initialize Moshi in your Converter class | |
*/ | |
private val moshi: Moshi = Moshi.Builder() | |
.add(KotlinJsonAdapterFactory()) | |
.build() | |
/** | |
* [Moshi] extension to transform a [List] to Json |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun main() { | |
/* | |
cards = ['Jack', 8, 2, 6, 'King', 5, 3, 'Queen'] | |
<!-- Required Output = [2,3,5,6,8,'Jack','Queen','King'] | |
Q: Sort the array as per the rules of card game using a generic method. | |
* */ | |
val card1 = arrayListOf("Jack", 8, 2, 6, "King", 5, 3, "Queen") | |
val card2 = arrayListOf( |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Note: In this problem you must NOT generate any output on your own. Any such solution will be considered as being against the rules and its author will be disqualified. The output of your solution must be generated by the uneditable code provided for you in the solution template. | |
An important concept in Object-Oriented Programming is the open/closed principle, which means writing code that is open to extension but closed to modification. In other words, new functionality should be added by writing an extension for the existing code rather than modifying it and potentially breaking other code that uses it. This challenge simulates a real-life problem where the open/closed principle can and should be applied. | |
A Tree class implementing a rooted tree is provided in the editor. It has the following publicly available methods: | |
getValue(): Returns the value stored in the node. | |
getColor(): Returns the color of the node. | |
getDepth(): Returns the depth of the node. Recall that the depth of a node is the number of |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Dealing with TMDB genres? | |
* This will help you fix that | |
*/ | |
fun getGenre(ids:List<Int>):String{ | |
var genre="" | |
for(i in ids ){ | |
when (i) { | |
28 -> genre+="Action|" | |
12 -> genre+="Adventure|" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Get TAG for class name | |
val Any.TAG: String | |
get() { | |
return if (!javaClass.isAnonymousClass) { | |
val name = javaClass.simpleName | |
/** First 23 chars */ | |
if (name.length > 23 && Build.VERSION.SDK_INT < Build.VERSION_CODES.N) | |
name.substring(0, 23) else name | |
} else { | |
val name = javaClass.name |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Converts milliseconds to formatted mm:ss | |
* | |
* @param value, time in milliseconds. | |
*/ | |
@BindingAdapter("elapsedTime") | |
fun TextView.setElapsedTime(value: Long) { | |
val seconds = value / 1000 | |
text = if (seconds < 60) seconds.toString() | |
else DateUtils.formatElapsedTime(seconds) |
NewerOlder