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.
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
Related Questions
Invoking a method by passing the objects of a class is termed as:
- Call by reference
- Call by value
- Call by method
- Call by constructor
Assertion (A): An argument is a value that is passed to a method when it is called.
Reason (R): Variables which are declared in a method prototype to receive values are called actual parameters
- Both Assertion (A) and Reason (R) are true and Reason (R) is a correct explanation of Assertion (A)
- Both Assertion (A) and Reason (R) are true and Reason (R) is not a correct explanation of Assertion(A)
- Assertion (A) is true and Reason (R) is false
- Assertion (A) is false and Reason (R) is true
Consider the following program segment and answer the questions below:
class calculate { int a; double b; calculate() { a=0; b=0.0; } calculate(int x, double y) { a=x; b=y; } void sum() { System.out.println(a*b); }}Name the type of constructors used in the above program segment?
Design a class to overload a function series( ) as follows:
(a) void series (int x, int n) – To display the sum of the series given below:
x1 + x2 + x3 + ………. xn terms
(b) void series (int p) – To display the following series:
0, 7, 26, 63 ………. p terms
(c) void series () – To display the sum of the series given below:
1/2 + 1/3 + 1/4 + ………. 1/10