17. Design a class overloading and a function display() as follows:
(i) void display(String str, int p) with one String argument and one integer argument.
It displays all the uppercase characters if 'p' is 1 (one) otherwise, it displays all the
lowercase characters.
(ii) void display(String str, char chr) with one String argument and one character
argument. It displays all the vowels if chr is 'v otherwise, it displays all the
alphabets. please give the correct answer!!
Answers
Answer:
What is the role of the keyword void in declaring functions?
The keyword 'void' signifies that the function doesn't return a value to the calling function
Answer:
import java.util.Scanner;
public class Overloading
{
void display(String str, int p) {
int len = str.length();
for (int i = 0; i < len; i++) {
char ch = str.charAt(i);
if (p == 1 && Character.isUpperCase(ch)) {
System.out.println(ch);
}
else if (p != 1 && Character.isLowerCase(ch)) {
System.out.println(ch);
}
}
}
void display(String str, char chr) {
int len = str.length();
for (int i = 0; i < len; i++) {
char ch = str.charAt(i);
ch = Character.toUpperCase(ch);
if (chr != 'v' && Character.isLetter(str.charAt(i)))
System.out.println(str.charAt(i));
else if (ch == 'A' ||
ch == 'E' ||
ch == 'I' ||
ch == 'O' ||
ch == 'U') {
System.out.println(str.charAt(i));
}
}
}
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.print("Enter string: ");
String s = in.nextLine();
Overloading obj = new Overloading();
System.out.println("p=1");
obj.display(s, 1);
System.out.println("\np!=1");
obj.display(s, 0);
System.out.println("\nchr='v'");
obj.display(s, 'v');
System.out.println("\nchr!='v'");
obj.display(s, 'u');
}
}
Explanation: