Write overloaded Java methods that return the difference of two parameters. Your
ID: 3638058 • Letter: W
Question
Write overloaded Java methods that return the difference of two parameters. Your methods should contain the logic to return the difference of the larger parameter – smaller parameter, regardless of parameter order. Provide enough test code to ensure your methods are logically correct. The following table displays the required method parameter variations.Parm 1 Type Parm 2 Type Return Type
int int int
double double double
int double double
double int double
Instructor hint:
Here is a code example I posted in a reply to another post. Some may find this helpful. In this example I have only two methods, not four as required for the assignment.
public class Test{
public static void main(String [] args){
System.out.println(doSomething(4, 5, 6));
System.out.println(doSomething(5.4, 7.6, 3.2, 4.12));
}
public static int doSomething(int int1, int int2, int int3){
return (int1 + int2 + int3);
}
public static double doSomething(double double1, double double2, double double3, double double4){
return (double1 * double2 - double3 * double4);
}
}
Explanation / Answer
Hi, Since you didn't post the table of "required method parameter variations" - I've included two overloaded methods (one for integers and one for doubles). I also don't see the photo to reference the desired assignment output, so I just made up my own. The calcDifference class contains the 2 overloaded methods and testDifference displays the resulting calls to those methods. Copy everything below this into the file calcDifference.java: public class calcDifference { // Difference of two integer values public int difference(int parm1, int parm2) { // Default to parm1 - parm2 int retValue = parm1 - parm2; // If parm2 > parm1, then set the difference to that instead if (parm2 > parm1) retValue = parm2 - parm1; return retValue; } // Difference of two "floating point" values public double difference(double parm1, double parm2) { // Default to parm1 - parm2 double retValue = parm1 - parm2; // If parm2 > parm1, then set the difference to that instead if (parm2 > parm1) retValue = parm2 - parm1; return retValue; } } Copy everything below this into the file testDifference.java: public class testDifference { public static void main(String[] args) { // Create an instance of our calcDifference class calcDifference calc = new calcDifference(); // Call the integer variation System.out.println("Difference of 5 and 2 is: " + calc.difference(5, 2)); System.out.println("Difference of 7 and 13 is: " + calc.difference(7, 13)); // Call the "floating point" variation System.out.println("Difference of 1.25 and 11.75 is: " + calc.difference(1.25, 11.75)); System.out.println("Difference of 21.5 and 14.25 is: " + calc.difference(21.5, 14.25)); } } Compile both files and run testDifference to see the output. Hope this helps..... :)
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.