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?
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.
With method overloading, you have two different methods.
calcFeetAndInchesToCentimetres that takes a single argumentcalcFeetAndInchesToCentimetres that takes two argumentsNow 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;
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With