Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write overloaded Java methods that return the difference of two parameters. Your

ID: 3623120 • 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. Each row indicates the return type, parameter 1 and parameter 2 for a separate method call using the pattern <return type> method_name(parm1 type, parm2 type);

check photo to see final assignment ouput
Continue to properly document your source code.

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 -- feel free to ask questions.

Good luck,

Joe

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote