Computer Applications
Define a class to overload the function print as follows:
void print() - to print the following format
1 1 1 1
2 2 2 2
3 3 3 3
4 4 4 4
5 5 5 5
void print(int n) - To check whether the number is a lead number. A lead number is the one whose sum of even digits are equal to sum of odd digits.
e.g. 3669
odd digits sum = 3 + 9 = 12
even digits sum = 6 + 6 = 12
3669 is a lead number.
Java
User Defined Methods
ICSE 2023
93 Likes
Answer
import java.util.Scanner;
public class KboatMethodOverload
{
public void print()
{
for(int i = 1; i <= 5; i++)
{
for(int j = 1; j <= 4; j++)
{
System.out.print(i + " ");
}
System.out.println();
}
}
public void print(int n)
{
int d = 0;
int evenSum = 0;
int oddSum = 0;
while( n != 0)
{
d = n % 10;
if (d % 2 == 0)
evenSum += d;
else
oddSum += d;
n = n / 10;
}
if(evenSum == oddSum)
System.out.println("Lead number");
else
System.out.println("Not a lead number");
}
public static void main(String args[])
{
KboatMethodOverload obj = new KboatMethodOverload();
Scanner in = new Scanner(System.in);
System.out.println("Pattern: ");
obj.print();
System.out.print("Enter a number: ");
int num = in.nextInt();
obj.print(num);
}
}Variable Description Table
Program Explanation
Output

Answered By
40 Likes
Related Questions
Design a class with the following specifications:
Class name: Student
Member variables:
name — name of student
age — age of student
mks — marks obtained
stream — stream allocated(Declare the variables using appropriate data types)
Member methods:
void accept() — Accept name, age and marks using methods of Scanner class.
void allocation() — Allocate the stream as per following criteria:mks stream >= 300 Science and Computer >= 200 and < 300 Commerce and Computer >= 75 and < 200 Arts and Animation < 75 Try Again void print() – Display student name, age, mks and stream allocated.
Call all the above methods in main method using an object.
Define a class to accept 10 characters from a user. Using bubble sort technique arrange them in ascending order. Display the sorted array and original array.
Define a class to accept a String and print the number of digits, alphabets and special characters in the string.
Example:
S = "KAPILDEV@83"
Output:
Number of digits – 2
Number of Alphabets – 8
Number of Special characters – 1Define a class to accept values into an array of double data type of size 20. Accept a double value from user and search in the array using linear search method. If value is found display message "Found" with its position where it is present in the array. Otherwise display message "not found".