Exercicis Debug

Sumari

  1. Pas a pas
  2. IsAdult
  3. TeamColors
  4. Factorial
  5. Math
  6. HowManyDays
  7. DebugFor

Pas a pas

IsAdult

Tenim la següent implementació amb errors de la funció isAdult amb els seus testos
Debugueu el programa per trobar els errors.
El cas que falla és el

fun isAdult(
        todayYear: Int,
        todayMonth: Int,
        todayDay: Int,
        birthYear: Int,
        birthMonth: Int,
        birthDay: Int
): Boolean {
    val isAdult = todayYear > birthYear + 18
    val isDifficultYear = todayYear == birthYear + 18
    val isAdultForMonth = isDifficultYear && todayMonth >= birthMonth
    val isDifficultMonth = isDifficultYear && todayMonth == birthMonth
    val isAdultForDay = isDifficultMonth && todayDay > birthDay
    return isAdult || isAdultForMonth || isAdultForDay
}

El següent programa fa unes proves i ens indica quin cas falla

fun main() {
    if(!isAdult(2018, 10, 17, 1985, 1, 1)) {
        println("Ha fallat: isAdult(2018, 10, 17, 1985, 1, 1)")
    }
    if(isAdult(2018, 10, 17, 2015, 1, 1)){
        println("Ha fallat isAdult(2018, 10, 17, 2015, 1, 1))")
    }
    if(!isAdult(2018, 10, 17, 2000, 1, 1)) {
        println("Ha fallat: isAdult(2018, 10, 17, 2000, 1, 1)")
    }
    if(isAdult(2018, 10, 17, 2000, 12, 1)){
        println("Ha fallat isAdult(2018, 10, 17, 2000, 12, 1))")
    }
    if(!isAdult(2018, 10, 17, 2000, 10, 1)) {
        println("Ha fallat: isAdult(2018, 10, 17, 2000, 10, 1)")
    }
    if(!isAdult(2018, 10, 17, 2000, 10, 17)) {
        println("Ha fallat: isAdult(2018, 10, 17, 2000, 10, 17)")
    }
    if(isAdult(2018, 10, 17, 2000, 10, 30)){
        println("Ha fallat isAdult(2018, 10, 17, 2000, 10, 30))")
    }
}

TeamColors

Tenim la següent funció que té errors.
Crea un main i debuga-la fins a solucionar-ne els errors.

/**
 * Given two colors returns the name of the team represented by the colors.
 * The correct values are:
 * Team1: white, green
 * Team2: white, blue,
 * Team3: white, brown
 * Team4: red, blue
 * Team5: red, black
 * Team6: green, red
 */
fun teamForColors(color1: String, color2: String): String {
    if (color1 == "white") {
        if (color2 == "green") return "Team1"
        if (color2 == "blue") return "Team2"
        if (color2 == "bowrn") return "Team3"
    }
    if (color1 == "red") {
        if (color2 == "blue") {
            return "Team3"
        }
        if (color2 == "Black") {
            return "Team5"
        }
        if (color2 == "green") {
            return "Team6"
        }
    }
    return "ERROR"
}

Factorial

Tenim la següent funció que té errors.
Crea un main i debuga-la fins a solucionar-ne els errors.

fun factorial(number: Int): Int {
    var result = 1
    for (i in 0..number) {
        result *= i
    }
    return number
}

Math

Dona't el següent codi, digues

import kotlin.math.sqrt

fun main() {
    var result = 0.0
    for (i in 0..9999) {
        if (result > 100) {
            result = sqrt(result)
        }
        if (result < 0) {
            result += result * result
        }
        result += 20.2
    }
    print("el resultat és $result")
}

HowManyDays

Tenim la següent funció que conté errors. Debuga'n l'error.

/**
 * Ens retorna el numero de dies del mes (segons l'any)
 */
fun howManyDays(month: Int, year: Int): Int {
    return when (month) {
        4, 6, 9, 11 -> 30
        2 -> {
            if (isLeapYear(year)) 29 else 28
            else -> 31
        }
    }
}

fun isLeapYear(year: Int): Boolean {
    var isLeapYear: Boolean
    if (year % 4 == 0) {
        if (year % 100 != 0) {
            isLeapYear= true
        } else if (year % 400 == 0) {
            isLeapYear= false
        } else {
            isLeapYear= false
        }
    } else {
        isLeapYear = false
    }
    return isLeapYear
}

DebugFor

Tenim el següent codi que ens dona problemes. Podries debugar-lo i dir-nos que passa?

fun main(args: Array<String>) {
    val numero1 = 548745184
    val numero2 = 25145
    var result = 0
    for (i in 0 until numero2) {
        result += numero1
    }
    println("la multiplicació de $numero1 i $numero2 es $result")
}