Home > Net >  In kotlin while overloading functions I keep getting a "Type mismatch" on some very basic
In kotlin while overloading functions I keep getting a "Type mismatch" on some very basic

Time:06-12

on the bottom function I keep getting Type mismatch: inferred type is Int but Double was expected I have done similar before by declaring the output as a double with an Int input. This is however my first time working with "math.PI" and "math.pow"

fun main(args: Array<String>) {
val   radius = 13;
    println(getArea(radius));
}
fun getArea(radius: Double): Double{
    val area = Math.pow(radius, radius) * Math.PI;
    return area;
}

fun getArea(radius: Int): Double{
    val area = Math.pow(radius, radius) * Math.PI;
    return area;
}

CodePudding user response:

I'll try to summarize the different contributed comments to formulate a hopefully complete answer to your question.

Incompatible type

First and foremost to your actual problem. the Math.pow function has the following signature, as can be seen in the documentation:

public static double pow(double a, double b)

You however attempt to invoke this function by passing two Integers into it, thus you receive the compilation error:

Type mismatch: inferred type is Int but Double was expected

Mistake in math formula

Assuming you aim to calculate the area of a circle, you have a mistake in your formula.

It should be A = r^2 * π but you got A = r^r * π.

Code reuse

If you want to provide the same function for differently typed parameters it might provide useful to implement it once and make use of the single implementation in the overloaded versions.

Keeping close to your original code, this might look something like the following.

fun main(args: Array<String>) {
    val radius = 13;
    println(getArea(radius));
}

fun getArea(radius: Double): Double {
    val area = Math.pow(radius, 2) * Math.PI;
    return area;
}

fun getArea(radius: Int): Double {
    return getArea(radius.toDouble())
}

Syntax improvements

Kotlin is a very expressive language, which allows you to simplify your code as follows. You do not need semicolons in Kotlin. For simple functions that are a single expression, Kotlin provides a neat shortened syntax. The standard library of Kotlin also provides basic math functionality such as a pow function and a π constant. So you don't need to rely on the Java/JVM specific functions, e.g. from Math.

import kotlin.math.PI
import kotlin.math.pow

fun getArea(radius: Double): Double = 
    radius.pow(2) * PI

fun getArea(radius: Int): Double = 
    getArea(radius.toDouble())

CodePudding user response:

Edited: I was focusing on the code so didn't pay attention to the math mistake sorry for that, as @duffymo said the area = raduis * raduis, so you need to change your syntax like this:

fun main(args: Array<String>) {
    val radius = 13;
    println(getArea(radius));
}
fun getArea(radius: Double): Double{
    val area = radius * radius * Math.PI;
    return area;
}

fun getArea(radius: Int): Double{
    val area = radius * radius * Math.PI;
    return area;
}
  • Related