0

Okay, pretty sure I got my formula right... don't think I quite understand how to call on methods, howcome mine isn't working?

    fun main() {
    println("Enter Width of the triangle")
    readln()
    println("Enter Height of the triangle")
    ComputeMethods().hypotenuse(readln().toDouble())
}

 class ComputeMethods(){
fun hypotenuse(width: Int, height: Int) {
    val triangle = width.toDouble().pow(2) + height.toDouble().pow(2)
    val formula = "$triangle"
    println(formula)
    }
}
Gilli
  • 51
  • 8
  • Can you please be more specific about what's not working? – Egor Oct 18 '22 at 07:10
  • 1
    You are only passing one parameter to the function instead of two. You should probably store the result of the first `readln()` call in a variable and pass it to the function call as well. – Fabian Strathaus Oct 18 '22 at 07:12

2 Answers2

2

Your hypotenuse function has two arguments of type Int. You are only giving it one, and also of the wrong type (Double). Also, the result of your first readln is not stored anywhere. To solve it you can do:

fun main() {
    println("Enter Width of the triangle")
    val width: Int = readln().toInt()
    println("Enter Height of the triangle")
    val height: Int = readln().toInt()
    ComputeMethods().hypotenuse(width, height)
}

PS: to get the hypotenuse you would also need to take the square root of that result

Ivo
  • 18,659
  • 2
  • 23
  • 35
1

@Gilli, Here is the working code example for triangle formula, You have to pass input arguments in space separated format (2.0 3.0), You can interact with the link given https://pl.kotl.in/mq-8jgVRy in order to run the program on kotlin playground.

import kotlin.math.pow

fun main(args: Array<String>) {
    println("Enter Width of the triangle")
    val width = args[0].toDouble()
    println("Enter Height of the triangle")
    val height = args[1].toDouble()
    ComputeMethods().hypotenuse(width, height)
}

class ComputeMethods(){
fun hypotenuse(width: Double, height: Double) {
    val triangle = width.pow(2) + height.pow(2)
    val formula = "$triangle"
    println(formula)
    }
}
Shoaib Mushtaq
  • 595
  • 4
  • 17
  • 1
    Appreciate that mate, got mine working. I had the logic all wrong! I get it now... think I need to take a step away from the computer HAHA! Need a break. – Gilli Oct 18 '22 at 07:48