Question

In: Computer Science

In java, write a program that asks the user to enter a character. Then pass the...

In java, write a program that asks the user to enter a character. Then pass the character to the following methods.

  1. isVowel() – returns true if the character is a vowel
  2. isConsonant() – returns true if the character is a consonant
  3. changeCase() – if the character is lower case then change it to upper case and if the character is in upper case then change it to lower case. (return type: char)

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);

Solutions

Expert Solution

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

  • Inside main() method isVowel(), isConsonant() methods are called to check the character is vowel or cosonant respectvely
  • changeCase() method is called to change case of the given character
  • isLower() method use a single if statement to compare the character with vowels a,e,i,o,u,A,E,I,O,U
  • If the given character is match with any one of the vowel,the method will returns true
  • isConsonant() check first the given character is vowel or not
  • if yes,return false
  • otherwise check if the character is any alphabet or not
  • if yes,means it is a consonant, then return true
  • In all other case,return false
  • changeCase() method checks if the given character is lower case or not
  • if yes,subtract 32 from the character to get corresponding upper case character
  • Otherwise checks if the given character is upper case or not
  • if yes,add 32 to the character to get corresponding lower case character

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


Related Solutions

Program should be written in Java a) Write a program that asks the user to enter...
Program should be written in Java a) Write a program that asks the user to enter the approximate current population of India. You should have the computer output a prompt and then YOU (as the user should enter the population.)  For testing purposes you may use the value of 1,382,000,000 from August 2020. Assume that the growth rate is 1.1% per year. Predict and print the predicted population for 2021 and 2022. The printout should include the year and the estimated...
Write a Java program that asks the user to enter an integer that is used to...
Write a Java program that asks the user to enter an integer that is used to set a limit that will generate the following four patterns of multiples of five using nested loops •Ascending multiples of five with ascending length triangle •Ascending multiples of five with descending length (inverted) triangle •Descending multiples of five with ascending length triangle •Descending multiples of five with descending length (inverted) triangle Use error checking to keep asking the user for a positive number until...
PYTHON: Write a program that asks the user to enter a 10-character telephone number in the...
PYTHON: Write a program that asks the user to enter a 10-character telephone number in the format XXX-XXX-XXXX. The application should display the telephone number with any alphabetic characters that appeared in the original translated to their numeric equivalent. For example, if the user enters 555-GET-FOOD, the application should display 555-438-3663. This is my code, but I cannot figure out where to go from here. #set new number new_number = "" #split number split_num = phone.split("-") for char in split_num[1:2]:...
Write a mips assembly language program that asks the user to enter an alphabetic character (either...
Write a mips assembly language program that asks the user to enter an alphabetic character (either lower or upper case)and change the case of the character from lower to upper and from upper to lower and display it.
Java Program 1. Write a program that asks the user: “Please enter a number (0 to...
Java Program 1. Write a program that asks the user: “Please enter a number (0 to exit)”. Your program shall accept integers from the user (positive or negative), however, if the user enters 0 then your program shall terminate immediately. After the loop is terminated, return the total sum of all the previous numbers the user entered. a. What is considered to be the body of the loop? b. What is considered the control variable? c. What is considered to...
Design, plan, test, and write a computer program in Java that asks the user to enter...
Design, plan, test, and write a computer program in Java that asks the user to enter 1 number and a String. You will display the first n characters of the string where n is the number entered. For example, if the user enters 3 and java then you will print jav. If they enter 5 and Halloween then you print Hallo. If the user enters a number less than 0 then set the number to 0. Assume the user will...
Write a java program which asks the user to enter name and age and calls the...
Write a java program which asks the user to enter name and age and calls the following methods: printName(): Takes name as parameter and prints it 20 times using a while loop. printAge(): Takes age as parameter and prints all the numbers from 1 up to age. Write a java program that will print first 10 multiples of 3 in a single line.
Instructions Write a Java program that asks the user t enter five test scores. The program...
Instructions Write a Java program that asks the user t enter five test scores. The program should display a letter grade for each score and the average test score. Write the following methods in the program: * calcAverage -- This method should accept five test scores as arguments and return the average of the scores. * determineGrade -- This method should accept a test score as an argument and return a letter grade for the score, based on the following...
Write a java simple document retrieval program that first asks the user to enter a single...
Write a java simple document retrieval program that first asks the user to enter a single term query, then goes through two docuements named doc1.txt and doc2.txt (provided with assignment7) to find which document is more relevant by calculating the frequency of the query term in each document. Output the conclusion to a file named asmnt7output.txt in the following format (as an example for query “java” and “problem”). The percentages in parenthese are respective supporting frequencies. java: doc1(6.37%) is more...
IN JAVA Write a complete program that asks the user to enter two real numbers from...
IN JAVA Write a complete program that asks the user to enter two real numbers from the console. If both numbers are positive print the product, if both numbers are negative print the quotient, otherwise print INVALID INPUT. Use a nested if; output should be to a dialog and console; use printf to format the console output (for real numbers specify the width and number of digits after the decimal). The output must be labeled. Follow Java conventions, indent your...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT