Advertisements
Advertisements
प्रश्न
Write a program with overloaded methods to implement the following:
Overload a method Pack(), given that:
- void Pack(int a, int b) − to print the average of the arguments.
- void Pack(int n) − to print the cube of the arguments.
- void Pack(String s) − to print the last character of the argument.
कोड लेखन
Advertisements
उत्तर
import java.util.*;
class clOver
{
void pack(int a, int b)
{
double avg = (a + b)/2.0;
System.out.println("Average of the arguments " + a + " & " + b + " is " + avg);
}
void pack(int n)
{
System.out.println(n + "^3 = " + (n * n * n));
}
void pack(String s)
{
int len = s.length();
System.out.println("Last char of " + s + " is " + s.charAt(len - 1));
}
void main()
{
pack(25, 30);
pack(7);
pack("SIMPLE LIFE");
}
}
Output:
Average of the arguments 25 & 30 is 27.5
7^3 = 343
Last char of SIMPLE LIFE is E
shaalaa.com
या प्रश्नात किंवा उत्तरात काही त्रुटी आहे का?
