Question

In: Computer Science

***Using Java Using the switch/case construct, write a program that takes a character from the user...

***Using Java

Using the switch/case construct, write a program that takes a character from the user and classifies it as a number (‘1’,’2’, ‘3’, …), a letter from the alphabet (‘A’, ‘a’, ‘B’, ‘b’, …, ‘Z’, ‘z’), an arithmetic operator (‘+’, ‘-‘, ‘/’, ‘*’, ‘%’), a comparison operator (‘<’, ‘>’, ‘=’), punctuation (‘!’, ‘?’, ‘,’, ‘,’, ‘:’, ‘;’), and all other characters as a Special Character, and informs the user of the same.

If the user entered the character A, the system should say: “You entered a letter!”

If the user entered the character (, the system should say: “You entered a special character!”

Solutions

Expert Solution

//CharacterClassifier.java
import java.util.Scanner;
public class CharacterClassifier {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter a character: ");
        char ch = scanner.next().charAt(0);
        switch (ch){
            case '0':
            case '1':
            case '2':
            case '3':
            case '4':
            case '5':
            case '6':
            case '7':
            case '8':
            case '9':
                System.out.println("You entered a number!");
                break;
            case 'A':
            case 'B':
            case 'C':
            case 'D':
            case 'E':
            case 'F':
            case 'G':
            case 'H':
            case 'I':
            case 'J':
            case 'K':
            case 'L':
            case 'M':
            case 'N':
            case 'O':
            case 'P':
            case 'Q':
            case 'R':
            case 'S':
            case 'T':
            case 'U':
            case 'V':
            case 'W':
            case 'X':
            case 'Y':
            case 'Z':
            case 'a':
            case 'b':
            case 'c':
            case 'd':
            case 'e':
            case 'f':
            case 'g':
            case 'h':
            case 'i':
            case 'j':
            case 'k':
            case 'l':
            case 'm':
            case 'n':
            case 'o':
            case 'p':
            case 'q':
            case 'r':
            case 's':
            case 't':
            case 'u':
            case 'v':
            case 'w':
            case 'x':
            case 'y':
            case 'z':
                System.out.println("You entered a letter!");
                break;
            case '+':
            case '/':
            case '*':
            case '%':
                System.out.println("You entered an arithmetic operator!");
                break;
            case '<':
            case '>':
            case '=':
                System.out.println("You entered a comparison operator!");
                break;
            case '!':
            case '?':
            case ',':
            case ':':
            case ';':
                System.out.println("You entered a punctuation !");
                break;
            default:
                System.out.println("You entered a special character!");
        }
    }
}


Related Solutions

Using c# , Write a program using a switch statement that takes one character value from...
Using c# , Write a program using a switch statement that takes one character value from the user and checks whether the entered value is an arithmetic operator (+, -, * , /) If not the program display a message that it not of the operators ( (+, -, * , /) .
USING THE SWITCH STATEMENT - Write a java program that asks the user to enter the...
USING THE SWITCH STATEMENT - Write a java program that asks the user to enter the number of their favorite month of the year – obviously, that would be 1 – 12. Write a switch statement that takes the number and converts it to the fully spelled out name [ex. 3 would be MARCH] . Be sure to build in error message to catch any invalid data entries such as 0 or 13 etc. Print out the number that was...
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. isVowel() – returns true if the character is a vowel isConsonant() – returns true if the character is a consonant 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...
Write a Java program that takes an array of 10 "Int" values from the user and...
Write a Java program that takes an array of 10 "Int" values from the user and determines if all the values are distinct or not. Return TRUE if all the values of the array are distinct and FALSE if otherwise.
8. write a program using switch-case statements that ask a user to enter a temperature value...
8. write a program using switch-case statements that ask a user to enter a temperature value in degrees Fahrenheit (In your program, use an integer to record the user’s entry. If the temperature falls between 0 and 96 make it print “Temperature below normal”. If the temperature is 97, 98, make it “Temperature normal”. If the temperature is between 100 and 150, print “You have a fever”. If the temperature is outside the ranges given above, display “Are you human”....
IN JAVA: Write a simple program that takes 5 inputs from the user and save them...
IN JAVA: Write a simple program that takes 5 inputs from the user and save them into a Text File. The inputs are Student Name, Student Address and student Date of Birth. Also write a simple program that reads and display the input from the first program text file.
write a java program that takes three numbers from the user and print the greatest number...
write a java program that takes three numbers from the user and print the greatest number (using if-statement). sample output: input the first number:35 input the second number:28 input the third number:87 the greatest number:87
Write a program using switch statement that asks user to enter the month number and then...
Write a program using switch statement that asks user to enter the month number and then it prints out the number of days for that month. For simplicity reasons have your program print 28 days for February no matter if it is a leap year or not. Your program should also handle any invalid month numbers that user could enter (hint use default for the switch). Use a while loop to allow user to test for different month entries till...
Write a program that will print out “W” using a character that a user provides. use...
Write a program that will print out “W” using a character that a user provides. use for loop statement. in java please
Write a Java program that uses printf, and takes user input to find the name of...
Write a Java program that uses printf, and takes user input to find the name of the File. FileCompare (20) Write a program that compares to files line by line, and counts the number of lines that are different. You can use file1.txt and file2.txt when testing your program, but you must ask for the file names in the input. main Interactively requests the names of the two files, after creating the Scanner for the keyboard. Creates a Scanner for...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT