초보자를 위한 Kotlin 200제 #2

초보자를 위한 Kotlin 200제

#kotlin #computer #book

초보자를 위한 Kotlin 200제에 나와 있는 예제 중 실습한 내용을 남겨 놓는다.

t001.kt

fun main() {
    println("Hello World!")
}

t022.kt

fun main(args : Array<String>) : Unit
{
    val value : Int = if(10 > 5) {
        println("10은 5보다 크다.")
        10
    }
    else {
        println("10은 5보다 크지 않다.")
        5
    }

    println(value)
}

t029.kt

fun main(args: Array<String>): Unit {
    var x = 0
    var y = 0

    outer@ while(x <= 20) {
        y = 0
        while(y <= 20) {
            if(x + y == 15 && x - y == 5)
                break@outer
            y += 1
        }

        x += 1
    }

    println("x: $x, y: $y")
}

t048.kt

class Product {
    var name = ""
    var price = 0
}


fun main(args: Array<String>): Unit {
    var product: Product = createProduct()

    printProductInfo(product)
    processProduct(product)
    printProductInfo(product)
}

fun createProduct(): Product {
    val apple = Product()
    apple.name = "Apple"
    apple.price = 1000

    return apple
}

fun processProduct(product: Product): Unit {
    product.price += 500
}

fun printProductInfo(product: Product): Unit {
    println("이름:${product.name}")
    println("가격:${product.price}")
}

t055.kt

class Size(width: Int, height: Int) {
    val width = width
    val height: Int

    init {
        this.height = height
    }

    val area: Int

    init {
        area = width * height
    }
}

fun main(args: Array<String>) {
    val size = Size(10, 50)
    println(size.area)
}

t057.kt

class Time(val second: Int) {
    init {
        println("init 블록 실행 중")
    }

    constructor(minute: Int, second: Int):this(minute*60 + second) {
        println("보조생성자 1 실행 중")
    }

    constructor(hour: Int, minute: Int, second: Int):this(hour*60 + minute, second) {
        println("보조생성자 2 실행 중")
    }

    init {
        println("또 다른 init 블록 싱행 중")
    }
}

fun main(args: Array<String>) {
    println("${Time(15, 6).second} 초")
    println("${Time(6, 3, 17).second} 초")
}

2

t060.kt

class Person(var name: String, var birthday: String) {
    operator fun get(position: Int): String {
        return when(position) {
            0 -> name
            1 -> birthday
            else -> "Unknown"
        }
    }

    operator fun set(position: Int, value: String) {
        when(position) {
            0 -> name = value
            1 -> birthday = value
        }
    }
}

fun main(args: Array<String>) {
    val person = Person("Kotlin", "2016-02-15")
    println(person[0])
    println(person[1])
    println(person[-1])

    person[0] = "Java"
    println(person.name)
}

t092.kt

class Point(val x: Int, val y: Int)

class Rect {
    lateinit var pt: Point
    lateinit var pt2: Point

    val width: Int get() = pt2.x - pt.x
    val height: Int get() = pt2.y - pt.y
    val area: Int get() = width * height
}

fun main(args: Array<String>) {
    val rect = Rect()
    rect.pt = Point(3,3)
    rect.pt2 = Point(6,5)

    println("WIDTH:${rect.width}")
    println("HEIGHT:${rect.height}")
    println("AREA:${rect.area}")
}