Computer Science, asked by abhinavjain1307, 11 months ago

Design a class to overload a function print () as follows:

(a) void print (int x, int y)- to print the odd numbers between the x and y.

(b) void print (char ch1, char ch2) - to print all the consonants between the 2 characters chl & ch2.

(c) void print (char ch, int n)- to print the character ch. n number of times in the given format:

Example: If ch = '$' and n= 4
Output:
$$$$
$$$​

Answers

Answered by Anonymous
21

Answer:

Method 1:

This method will take value when the user will create object and will call each method separately.

import java.util.*;

class overload

{ \\class starts

void print(int x, int y )

{ \\first void method starts

System.out.println("Odd Numbers between " +x+ " and " +y+ " are " );

for(int i = x, i<=y; i++)

{ \\for loop starts

if(i/2!=0) \\ condition checking whether the number is odd or not

System.out.println(i);\\printing odd numbers between x and y

} \\for loop ends

} \\first void method ends

void print(char ch1, char ch2)

{ \\second void method starts

System.out.println(" Consonants present between " +ch1+ " and " +ch2+ " are: " );

if(ch2>='a'&&ch2<='z')

{ \\if condition begins

for(int j=65;j<=90;j++)

{ \\for loop starts

char ch3 = (char)j;\\conversion of integer to character

if(ch3!='A' && ch3 !='E' && ch3 !='I' && ch3 !='O' && ch3 !='U' )\\checking whether capital letters are not vowels

System.out.println(ch3);\\printing capital letter constants

} \\for loop ends

} \\if condition ends

if(ch2>='A' && ch2<='Z')

{ \\if condition begins

for(int j=97;j<=122;j++)

{ \\for loop begins

char ch3 = (char)j;\\conversion of integer to character

if(ch3 != 'a' && ch3!='e' && ch3 !='i' && ch3 !='o' && ch3!='u')\\ checking whether small letters are not vowels

System.out.println(ch3);\\printing small letter constants

} \\for loop ends

} \\if condition ends

} \\second void method ends

void print( char ch , int n )

{ \\third method starts

for(int i=n; i>=1 ; i--)

{ \\for Loop starts

System.out.print("* "); \\code printing stars

} \\for loop ends

System.out.println(); \\ code changing lines after printing stars in one line

} \\third method ends

public static void main ( String args [] )

{ \\main function or main method starts

Overload ob= new Overload(); \\creating object for calling methods.

ob.print(); \\calling first method

ob.print(); \\calling second method

ob.print(); \\calling third method

} \\main method ends

} \\program ends

Method 2:

This method will take input on the output window and will print all the methods in order of their calling.

import java.util.*;

class overload

{ \\class starts

void print(int x, int y )

{ \\first void method starts

System.out.println("Odd Numbers between " +x+ " and " +y+ " are " );

for(int i = x, i<=y; i++)

{ \\for loop starts

if(i/2!=0) \\condition checking whether the number is odd or not

System.out.println(i);\\printing odd numbers between x and y

} \\for loop ends

} \\first void method ends

void print(char ch1, char ch2)

{ \\second void method starts

System.out.println(" Consonants present between " +ch1+ " and " +ch2+ " are: " );

if(ch2>='A'&&ch2<='Z')

{ \\if condition starts

for(int j=65;j<=90;j++)

{ \\for loop starts

char ch3 = (char)j;

if(ch3!='A' && ch3 !='E' && ch3 !='I' && ch3 !='O' && ch3 !='U' ) \\checking capital letters are not vowels

System.out.println(ch3);\\printing capital constant letters

} \\for loop ends

} \\if condition ends

if(ch2>='a' && ch2<='z')

{ \\if condition starts

for(int j=97;j<=122;j++)

{ \\for loop starts

char ch3 = (char)j; \\character conversion from integer

if(ch3 != 'a' && ch3!='e' && ch3 !='i' && ch3 !='o' && ch3!='u') \\checking small letters are not vowels

System.out.println(ch3); \\printing small constant letters

} \\for loop ends

} \\if condition ends

} \\second void method ends

void print( char ch , int n )

{ \\third method starts

for(int i=n; i>=1 ; i--)

{ \\for Loop starts

System.out.print("* "); \\code printing stars

} \\for loop ends

System.out.println(); \\ code changing lines after printing stars in one line

} \\third method ends

public static void main ( String args [] )

{ \\main function or main method starts

int a,b,num; \\variable declaration

char ch,chr1,chr2; \\variable declaration

Scanner sc = new Scanner(System.in);

\\creating object taking inputs

System.out.println(" Please enter two numbers to print odd numbers in between the two numbers");

a=sc.nextInt(); \\taking number as input

b=sc.nextInt(); \\taking number as input

System.out.println("Please enter two characters to print constants between the two characters");

ch= sc.next.charAt(0);\\taking character as input

chr1= sc.next.charAt(0); \\taking character as input

System.out.println("Please enter a character and a natural number to print the character ");

chr2 = sc.next.charAt(0); \\taking character as input

num = sc.nextInt(); \\taking number as input

Overload ob = new Overload(); \\creating object for method calling

ob.print(a,b); \\calling first method

ob.print(ch,chr1); \\calling second method

ob.print(chr2,num); \\calling third method

} \\main method ends

} \\program ends

Similar questions