ICSE Computer Science Java Programs | IT Developer
IT Developer

Java Programs - Solved 2020 ICSE Computer Science Paper



Share with a Friend

Solved 2020 ICSE Computer Science Paper

Class 10 - ICSE Computer Science Solved Papers

Method Overloading - ICSE 2020 Computer Science

Question 7
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 n) – to find and display the sum of even digits of a number.
Example:
n = 29865
Sum of even digits = 16


Write a main() method to create an object and invoke the above methods.

class Overload{ public void number(int num, int d){ int f = 0; if(num == 0 && d == 0) f = 1; for(int i = num; i != 0; i /= 10){ if(i % 10 == d) f++; } System.out.println("Frequency of digit " + d + " = " + f); } public void number(int n){ int sum = 0; for(int i = n; i != 0; i /= 10){ int d = i % 10; if(d % 2 == 0) sum += d; } System.out.println("Sum of even digits = " + sum); } public static void main(String[] args){ Overload obj = new Overload(); obj.number(2565685, 5); obj.number(29865); } }

Output

 
 OUTPUT : 
Frequency of digit 5 = 3
Sum of even digits = 16