DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Newsletter
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • The Window Closes: Why I Switched to Kotlin Before the Codebase Got Too Big
  • Testing Java and Spring Applications With Kotlin: Low-Risk to Adoption
  • Why Kotlin or Java Is the Wrong Question For Enterprises
  • Parallel Kafka Batch Processing With Kotlin Coroutines in Spring Boot

Trending

  • Should Developers Switch From SQLite to DuckDB?
  • When a Coding Agent Fixes the Test Instead of the Bug
  • Part 4: Multi-Agent Orchestration — Pairing Goose With Quarkus Flow State Machines
  • Stop Writing Database Migration Scripts: Declarative Schema Sync for Java

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see it.

  1. DZone
  2. Coding
  3. Languages
  4. Rolling Dice in Kotlin

Rolling Dice in Kotlin

A small tutorial on creating a die rolling API in Kotlin, the hottest new language on the JVM.

By 
Nicolas Fränkel
·
Jun. 22, 16 · Tutorial
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
10.8K Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in

A little more than 2 years ago, I wrote a post on how you could create a die-rolling API in Scala.

As I’m more and more interested in Kotlin, let’s do that in Kotlin.

At the root of the hierarchy lies the Rollable interface:

interface Rollable<T> {
    fun roll(): T
}

The base class is the Die:

open class Die(val sides: Int): Rollable<Int> {

    init {
        val random = new SecureRandom()
    }

    override fun roll() = random.nextInt(sides)
}

Now let’s create some objects:

object d2: Die(2)
object d3: Die(3)
object d4: Die(4)
object d6: Die(6)
object d10: Die(10)
object d12: Die(12)
object d20: Die(20)

Finally, in order to make code using Die instances testable, let’s change the class to inject the Random instead:

open class Die(val sides: Int, private val random: Random = SecureRandom()): Rollable<Int> {
    override fun roll() = random.nextInt(sides)
}

Note that the random property is private, so that only the class itself can use it, there won’t even be a getter.

The coolest thing about that is that I hacked the above code in 15 minutes in the plane. I love Kotlin :-)

Kotlin (programming language)

Published at DZone with permission of Nicolas Fränkel. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • The Window Closes: Why I Switched to Kotlin Before the Codebase Got Too Big
  • Testing Java and Spring Applications With Kotlin: Low-Risk to Adoption
  • Why Kotlin or Java Is the Wrong Question For Enterprises
  • Parallel Kafka Batch Processing With Kotlin Coroutines in Spring Boot

Partner Resources

×

Comments

Loading comments

The comments didn't load

Please try again in a moment.

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook