Java program to check Vowels and Consonents

Here is a program to check weather the inputted character is a vowel or a consonent. It also uses the switch case mechanism to determine the vowel property of the character. Function Math.random() is used to generate an arbitary character and then checks weather the caharacter is a vowel or consonent.

/**
 * VowelsNConsonents.java
 * A program to implement the usage of the switch case in java lang…
 * @author jo
 * @author [jintu.jacob@gmail.com]
 *
 * It uses a method – Math.random(). generates a value between 0 and 1, you need
 * only multiply it by the upper bound of the range of numbers you want to
 * produce (26 for the letters in the alphabet) and add an offset to establish
 * the lower bound.
 *  
 */

public class VowelsNConsonents {
    public static void main(String[] args) {
        double x = Math.random() * 26;
        char c = (char)( x + ‘a’);
        /**
         * Math.random( ) produces a double, so the value 26 is converted to a
         * double to perform the multiplication, which also produces a double.
         * This means that ‘a’ must be converted to a double to perform the
         * addition. The double result is turned back into a char with a cast.
         */

   
            System.out.println("Math.random()*26  >>  "+ x );
        System.out.print(c + ": ");
        switch(c) {
            case ‘a’:
            case ‘e’:
            case ‘i’:
            case ‘o’:
            case ‘u’: System.out.println("vowel");
                        break;
            case ‘y’:
            case ‘w’: System.out.println("Sometimes a vowel");
                        break;
            default: System.out.println("consonant");
        }
    }
}
About these ads

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s