In: Computer Science
In java, write a program that asks the user to enter a character. Then pass the character to the following methods.
Example output is given below:
Enter a character
a
a is a vowel
a is not a consonant
a is equivalent to A
test case: e, E, d, D
Note: Java represents character using Unicode encoding and ‘A’ to ‘Z’ is represented by numbers 65 to 90 and ‘a’ to ‘z’ is represented by numbers 97 to 122. So to convert ‘A’ to ‘a’ you need to add (97-65) to A and then cast it to Character type since the addition will change its type to integer.
You can use the following code to read a character from console. When you call charAt(i) method for any String it returns the character at index i. The index of first character is 0, the index of second character is 1 and so on.
Scanner input = new Scanner(System.in);
char ch =input.next().charAt(0);
Source code of the program and its working are given below.Comments are also given along with the code for better understanding.Screen shot of the code and output are also attached.If find any difficulty, feel free to ask in comment section. Please do upvote the answer.Thank you.
Working of the program
Source code
//importing Scanner class to read user input
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
//declaring a character variable ch
char ch;
//creating Scanner object to read user input
Scanner input=new Scanner(System.in);
System.out.println("Enter the character: ");
//reading user input
ch=input.next().charAt(0);
//checking the character is a vowel by calling isVowel() method
if (isVowel(ch))
System.out.println(ch+" is a vowel");
else
System.out.println(ch+" is not a vowel");
//checking the character is a consonant by calling isConsonant() method
if (isConsonant(ch))
System.out.println(ch+" is a consonant");
else
System.out.println(ch+" is not a consonant");
//calling changeCase() method to change the case of character
System.out.println(ch+" is equivalent to "+changeCase(ch));
}
//isVowel() method definition
public static boolean isVowel(char ch)
{
//checking if the character is any one of the vowels a,e,i,o,u,A,E,I,O,U
if(ch=='a'|| ch=='e'||ch=='i'||ch=='o'||ch=='u'||
ch=='A'||ch=='E'||ch=='I'||ch=='O'||ch=='U')
return true;
else
return false;
}
//isConsonant() method definition
public static boolean isConsonant(char ch)
{
//checking if the character is any one of the vowels a,e,i,o,u,A,E,I,O,U
if(ch=='a'|| ch=='e'||ch=='i'||ch=='o'||ch=='u'||
ch=='A'||ch=='E'||ch=='I'||ch=='O'||ch=='U')
//if yes return false
return false;
//otherwise check if it is an alphabet
else if((ch>='a'&&ch<='z')||(ch>='A'&&ch<='Z'))
//if yes, means it is a consonant,then return true
return true;
//if none of the condition true,return false
else
return false;
}
//changeCase() method definition
public static char changeCase(char ch)
{
//if character is lower case then subtract 32 to get corresponding upper case
if(ch>='a'&&ch<='z')
ch=(char)(ch-32);
//if character is upper case then add 32 to get corresponding lower case
else if(ch>='A'&&ch<='Z')
ch=(char)(ch+32);
return ch;
}
}
Screen shot of the code


Screen shot of the output
