Ravindra BagaleCourses & study guides Track your progress
AI CODING ASSISTANTS · CHAPTER 3 · LANGUAGES

How to Code in Kotlin Using AI

Use an AI assistant to write a Kotlin function, run it, and prove it with tests. Kotlin is the main language for Android apps and also runs on servers.

In short: say the Kotlin version, give the formula or rules and one example with the exact answer, ask for kotlin.test tests, and run them.

Why Kotlin with AI

WHY

Kotlin is Google's preferred language for Android. Many Android teams write Kotlin every day. AI speeds up the repeated parts, but money and math must be checked with tests.

What you need

WHAT
  • JDK 17 or 21: Kotlin runs on the JVM.
  • Kotlin: through Android Studio or IntelliJ (bundled), or the kotlinc command-line compiler.
  • kotlin.test: Kotlin's test library; works with JUnit in Gradle projects.
  • IDE + assistant: Android Studio or IntelliJ with Copilot, or VS Code with Claude Code.

How it flows

1 · Describe the function formula + example answer 2 · AI writes code read every line 3 · Run it check the example 4 · Ask for tests edge cases 5 · Run tests all pass = keep

Install and check

HOW
  1. Install IntelliJ IDEA or Android Studio (Kotlin is included), or the kotlinc compiler from the official Kotlin releases.
  2. Check: kotlinc -version (command line) or create a new Kotlin project in the IDE.
  3. In Gradle projects, add testImplementation(kotlin("test")) and run ./gradlew test.

Step 1 — the prompt

COPY THIS PROMPT

Write a Kotlin function emi(principal: Double, annualRatePercent: Double, months: Int): Long for a loan EMI using EMI = P × r × (1+r)^n / ((1+r)^n − 1), where r is the monthly rate. Round to the nearest rupee. Zero interest means principal / months. Use require() for bad input. Example: emi(500000.0, 9.0, 60) = 10379. Only return the code.

Step 2 — the code (tested here)

Emi.kt
// Monthly loan EMI. Built with an AI assistant, then tested.
import kotlin.math.pow
import kotlin.math.roundToLong

/** EMI = P * r * (1+r)^n / ((1+r)^n - 1), r = monthly rate. Returns rupees, rounded. */
fun emi(principal: Double, annualRatePercent: Double, months: Int): Long {
    require(principal > 0) { "principal must be positive" }
    require(months > 0) { "months must be positive" }
    require(annualRatePercent >= 0) { "rate cannot be negative" }
    if (annualRatePercent == 0.0) return (principal / months).roundToLong()
    val r = annualRatePercent / 12 / 100
    val f = (1 + r).pow(months)
    return (principal * r * f / (f - 1)).roundToLong()
}

fun main() {
    println(emi(500_000.0, 9.0, 60))  // 10379
}
kotlinc Emi.kt -include-runtime -d emi.jar
java -jar emi.jar
# -> 10379

Step 3 — ask for tests, then run them

FOLLOW-UP PROMPT

Write kotlin.test checks for emi: 5 lakh at 9% for 60 months, zero interest, negative principal, and zero months.

EmiTest.kt
import kotlin.test.assertEquals
import kotlin.test.assertFailsWith

fun main() {
    assertEquals(10379, emi(500_000.0, 9.0, 60))   // 5 lakh, 9%, 5 years
    assertEquals(10000, emi(120_000.0, 0.0, 12))   // zero interest
    assertFailsWith<IllegalArgumentException> { emi(-1.0, 9.0, 12) }
    assertFailsWith<IllegalArgumentException> { emi(100_000.0, 9.0, 0) }
    println("4 checks passed")
}
# -> 4 checks passed
Why the zero-interest case matters: the EMI formula divides by ((1+r)^n − 1). With 0% interest that is 0, and the answer becomes NaN. AI often forgets this. The test proves our code handles it.

Settings and config

  1. Rules file: “Kotlin 2.x, null-safety, no !!, kotlin.test/JUnit, coroutines for async”.
  2. Turn on warnings as errors in CI: kotlin { compilerOptions { allWarningsAsErrors.set(true) } } in Gradle.
  3. Use the IDE's “Reformat Code” after accepting AI code.

Watch out (common AI mistakes in Kotlin)

  • The !! operator — it crashes on null; ask for safe calls (?.) or requireNotNull.
  • Double for money — fine for an EMI estimate; use BigDecimal for real accounting.
  • Old Android APIs — say “Jetpack Compose” or your minSdk.
  • Blocking calls on the main thread — ask for coroutines.

Quick quiz

1. What is emi(500000.0, 9.0, 60)?

10379 rupees.

2. Why test 0% interest?

The formula divides by zero there; code must handle it separately.

3. Which Kotlin operator often causes crashes?

!! (not-null assertion).

4. Which type should real accounting use?

BigDecimal.

Save tokens and money

SMART AI USE

Give the formula in the prompt; the AI does not need to search or guess. Ask for one function plus tests. Paste only the top lines of a Gradle error.

Save these cards

INTERVIEW CARDAI Coding

How do you check AI Kotlin code?

Run the example; write kotlin.test tests.
Test edge cases like zero and negative input.
Ravindra Bagale · ravindrabagale.com/ai-coding
COMPARISON CARDAI Coding

Kotlin vs Java with AI

Kotlin: null-safety catches AI null bugs at compile time.
Java: needs extra null checks and tests.
Ravindra Bagale · ravindrabagale.com/ai-coding
REVISION CARDAI Coding

Kotlin with AI in 3

Formula + example in the prompt.
require() for bad input.
Tests incl. 0% interest.
Ravindra Bagale · ravindrabagale.com/ai-coding