Computer Applications
Design a class to overload a method Number() as follows:
(i) void Number(int num, int d) — To count and display the frequency of a digit in a number.
Example:
num = 2565685
d = 5
Frequency of digit 5 = 3
(ii) void Number(int n1) — To find and display the sum of even digits of a number.
Example:
n1 = 29865
Sum of even digits = 2 + 8 + 6 = 16
Write a main method to create an object and invoke the above methods.
Java
User Defined Methods
77 Likes
Answer
public class Overload
{
void Number(int num, int d) {
int f = 0;
while (num != 0) {
int x = num % 10;
if (x == d) {
f++;
}
num /= 10;
}
System.out.println("Frequency of digit " + d + " = " + f);
}
void Number(int n1) {
int s = 0;
while (n1 != 0) {
int x = n1 % 10;
if (x % 2 == 0) {
s = s + x;
}
n1 /= 10;
}
System.out.println("Sum of even digits = " + s);
}
public static void main(String args[]) {
Overload obj = new Overload();
obj.Number(2565685, 5);
obj.Number(29865);
}
}Output

Answered By
34 Likes
Related Questions
Define a class to overload the method perform as follows:
double perform (double r, double h) — to calculate and return the value of curved surface area of cone
void perform (int r, int c) — Use NESTED FOR LOOP to generate the following format
r = 4, c = 5
output
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5void perform (int m, int n, char ch) — to print the quotient of the division of m and n if ch is Q else print the remainder of the division of m and n if ch is R
Assertion (A): Static method can access static and instance variables.
Reason (R): Static variable can be accessed only by static method.
- Assertion and Reason both are correct.
- Assertion is true and Reason is false.
- Assertion is false and Reason is true.
- Assertion and Reason both are false.