Advertisements
Advertisements
Question
Write a program with overloaded methods to implement the following:
Overload a method fnDiffer(), given that:
- Int fnDiffer(int a, int b) − to return the difference between the arguments.
- double fnDiffer (double p, double q) − to return the difference between the arguments.
- int fnDiffer (String s1, String s2) − to return the difference oflengths between the arguments.
Code Writing
Advertisements
Solution
import java.util.*;
class clOver
{
int fnDiffer(int a, int b)
{
int d = a - b;
d = (d < 0) ? (d * (-1)) : d;
return d;
}
double fnDiffer(double p, double q)
{
double d = p - q;
d = (d < 0) ? (d * (-1)) : d;
return d;
}
int fnDiffer(String s1, String s2)
{
int len1 = s1.length();
int len2 = s2.length();
int d = len1 - len2;
d = (d < 0) ? (d * (-1)) : d;
return d;
}
void main()
{
int R1 = fnDiffer(72, 75);
System.out.println("Difference between 72, 75 is" + R1);
double R2 = fnDiffer(24.68, 24.31);
System.out.println("Difference between 24.68, 24.31 is" + R2);
int R3 = fnDiffer("WEIRD", "WONDEROUS");
System.out.println("Length Difference is" + R3);
}
}
Output:
Difference between 72, 75 is 3
Difference between 24.68, 24.31 is 0.37
Length Difference is 4
shaalaa.com
Is there an error in this question or solution?
