188k views
0 votes
Write a second convertToInches() with two double parameters, numFeet and numInches, that returns the total number of inches. Ex: convertToInches(4.0, 6.0) returns 54.0 (from 4.0 * 12 + 6.0).FOR JAVA PLEASEimport java.util.Scanner;public class FunctionOverloadToInches {public static double convertToInches(double numFeet) {return numFeet * 12.0;}/* Your solution goes here */public static void main (String [] args) {double totInches = 0.0;totInches = convertToInches(4.0, 6.0);System.out.println("4.0, 6.0 yields " + totInches);totInches = convertToInches(5.9);System.out.println("5.9 yields " + totInches);return;}}

1 Answer

2 votes

Answer:

public static double convertToInches(double numFeet, double numInches){

return (numFeet*12)+numInches;

}

Step-by-step explanation:

This technique of having more than one method/function having the same name but with different parameter list is called method overloading. When the method is called, the compiler differentiates between them by the supplied parameters. The complete program is given below

public class FunctionOverloadToInches {

public static void main(String[] args) {

double totInches = 0.0;

totInches = convertToInches(4.0, 6.0);

System.out.println("4.0, 6.0 yields " + totInches);

totInches = convertToInches(5.9);

System.out.println("5.9 yields " + totInches);

return;

}

public static double convertToInches(double numFeet)

{

return numFeet * 12.0;

}

/* Your solution goes here */

public static double convertToInches(double numFeet, double numInches){

return (numFeet*12)+numInches;

}

}

User SaviNuclear
by
7.9k points