मराठी

Write a program with overloaded methods to implement the following: Overload a method fnDiffer(), given that: (a) Int fnDiffer(int a, int b) − to return the difference between the arguments.

Advertisements
Advertisements

प्रश्न

Write a program with overloaded methods to implement the following:

Overload a method fnDiffer(), given that:

  1. Int fnDiffer(int a, int b) − to return the difference between the arguments.
  2. double fnDiffer (double p, double q) − to return the difference between the arguments.
  3. int fnDiffer (String s1, String s2) − to return the difference oflengths between the arguments.
कोड लेखन
Advertisements

उत्तर

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
  या प्रश्नात किंवा उत्तरात काही त्रुटी आहे का?
पाठ 11: Programs Overall - Method Overloading [पृष्ठ २५३]

APPEARS IN

रूपा पंडित Computer Applications [English] Class 10 ICSE
पाठ 11 Programs Overall
Method Overloading | Q 2. | पृष्ठ २५३
Share
Notifications

Englishहिंदीमराठी


      Forgot password?
Use app×