Skip to content

Instantly share code, notes, and snippets.

View travisdachi's full-sized avatar
🖖
Live long and prosper

Travis P travisdachi

🖖
Live long and prosper
View GitHub Profile
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".grader.LegendaryGraderActivity">
<TextView
interface Grader {
fun grade(score: Int): String
}
class TypicalGrader : Grader {
override fun grade(score: Int): String =
when (score) {
in 80..100 -> "A"
in 70..79 -> "B"
in 60..69 -> "C"
in 50..59 -> "D"
in 0..49 -> "F"
else -> "???"
}
class SuGrader(val satisfy: Int = 60) : Grader {
init {
if (satisfy < 0 || satisfy > 100)
throw IllegalArgumentException("A satisfy $satisfy is out of score range of 0-100")
}
override fun grade(score: Int) = when (score) {
in satisfy..100 -> "S"
in 0 until satisfy -> "U"
else -> "???"
import kotlinx.android.synthetic.main.activity_legendary_grader.*
class LegendaryGraderActivity : AppCompatActivity() {
var grader: Grader = TypicalGrader()
val satisfy = 50
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_legendary_grader)
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
android {
compileSdkVersion 24
buildToolsVersion "24.0.0"
defaultConfig {
applicationId "com.travispea.kotlinforandroid101"
public void attack(Target target) {
attack(target, new Fist());
}
public void attack(Target target, Weapon weapon) {
attack(target, weapon, 0.1f);
}
public void attack(Target target, Weapon weapon, float critical) {
//...
fun attack(target: Target, weapon: Weapon = Fist(), critical: Float = 0.1F) {
//...
}
// <paramName>: <paramType> [= <defaultValue>]
val target = getCurrentTarget()
attack(target)
attack(target, Sword())
attack(target, Bow(), 0.5F)
attack(target = target, critical = 0.9f)
public static String formatFancy(Date date) {
String myFancyFormat = ...
SimpleDateFormat f = new SimpleDateFormat(myFancyFormat);
return f.format(date);
}
String fancyDate = MyDateUtil.formatFancy(someDate);
fun Date.formatFancy(): String {
val myFancyFormat = ...
val f = SimpleDateFormat(myFancyFormat)
return f.format(this)
}
// fun <receiverType>.<functionName>(): <returnType>
someDate.formatFancy() //call within Kotlin
MyExtnsionsKt.formatFancy(someDate); //call from Java