Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Return in the method overloading

I'm new to Java and I'm learning it myself. I met a trouble when I try the method overloading. This is the code

 public static void main(String[] args) {
        calculateScore();
        calculateScore(500);
        calculateScore("Duy", 600);
        calcFeetAndInchesToCentimetres(100, 3.5);
        calcFeetAndInchesToCentimetres(100*12 + 3.5);
    }

 public static double calcFeetAndInchesToCentimetres(double feet, double inches) {
        if (feet >= 0 && inches >= 0 && inches <= 12) {
            double footToInches = feet * 12;
            double centimetres = (inches + footToInches) * 2.54;
            System.out.println("The value in centimetres is " + centimetres + " cm.");
            return centimetres;
        } else {
            return -1;
        }
    }

    public static double calcFeetAndInchesToCentimetres(double inches) {
        if (inches >= 0){
            double inchesToFeet = inches / 12;
            double inchesRemain = inches - (inchesToFeet * 12);
            calcFeetAndInchesToCentimetres(inchesToFeet, inchesRemain);
            return 0;
        } else {
            return -1;
        }

My problem is when I take the return 0 from the second method, the debugger says "missing return statement". Then I tried to put return calcFeetAndInchesToCentimetres(inches);, it works but the program runs for about many thousands times.

Then I put return 0 and everything is OK. But I don't understand why I can't put the return calcFeetAndInchesToCentimetres(inches); and why do I need a return statement when the method above (with 2 parameters) had it alredy. And if I want to have the value of centimetres converted when I execute the second method (with "inches" parameter only) what do I have to do?

One other thing I've realized that in this blockcode

double inchesToFeet = inches / 12;
        double inchesRemain = inches - (inchesToFeet * 12);
        calcFeetAndInchesToCentimetres(inchesToFeet, inchesRemain);

the inchesRemain will be 0? But the method works very well. When I change inchesToFeet = inches % 12, it just not shows anything. Why?

like image 648
Duy Nghia Tran Avatar asked Mar 08 '26 21:03

Duy Nghia Tran


2 Answers

It should just be:

public static double calcFeetAndInchesToCentimetres(double inches) {
    if (inches >= 0){
        double inchesToFeet = inches / 12;
        double inchesRemain = inches - (inchesToFeet * 12);
        return calcFeetAndInchesToCentimetres(inchesToFeet, inchesRemain);
    } else {
        return -1;
    }
}

You said you've tried return calcFeetAndInchesToCentimetres(inches); but that's just calling your method recursively and it recurses forever as there is no stopping condition.

like image 148
Dici Avatar answered Mar 11 '26 12:03

Dici


With method overloading, you have two different methods.

  1. calcFeetAndInchesToCentimetres that takes a single argument
  2. calcFeetAndInchesToCentimetres that takes two arguments

Now when you call calcFeetAndInchesToCentimetres(inches); you are calling the one that takes a single argument. If you call that from inside itself, it will continue calling itself infinite times. This is the error you are seeing.

If you replace that with return calcFeetAndInchesToCentimetres(inchesToFeet, inchesRemain); that will call the other method - the one that takes two arguments. That's what you actually want to do.

Fixed version:

public static double calcFeetAndInchesToCentimetres(double inches) {
    if (inches >= 0){
        double inchesToFeet = inches / 12;
        double inchesRemain = inches - (inchesToFeet * 12);
        return calcFeetAndInchesToCentimetres(inchesToFeet, inchesRemain);
    } else {
        return -1;
    }
}
like image 33
nhouser9 Avatar answered Mar 11 '26 12:03

nhouser9



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!