CALCULATE FEET AND INCHES TO CENTIMETRES
Create a method called calcFeetAndInchesToCentimeters.It needs to have two parameters feet and inches.You should validate the first parameter feet is >=0 and inches is >=0 and <=12 which return -1 from the method if either of the above is not true. If parameters are valid, then how many centimeters comprises the feet and inches passed to this method and return that value.Create a 2nd method of the same name but with only one parameter inches. validate that it is >=0, return -1 if false. But if its valid, then calculate how many feets are in inches and here is the tricky part call the other overloaded method passing the correct feet and inches calculated so that it can calculate correctly.
HINTS: Use 1inch = 2.54 cm and 1ft = 12inches.
Soln:
package com.cnc;
public class Main {
public static void main(String[] args) {
calcFeetAndInchesToCentimeters(3,1 );
calcFeetAndInchesToCentimeters(-1,3);
calcFeetAndInchesToCentimeters(100);
}
public static double calcFeetAndInchesToCentimeters(double feet,double inches){
if((feet<0) || (inches<0) && (inches>12)){
System.out.println("INVALID");
return -1;
}else{
double a = 30.48*feet;
a += 2.54*inches;
System.out.println(feet+" feet, = "+a + inches +" inches = "+a+" cm");
return a;
}
}
public static double calcFeetAndInchesToCentimeters(double inches){
if(inches<0){
return -1;
}
double feet = (int) inches/12;
double reaminingInches= (int) inches % 12;
System.out.println(inches +" inches is equal to "+ feet+" feet and "
+reaminingInches);
return calcFeetAndInchesToCentimeters(feet,inches);
}
}