207k views
4 votes
Modify the CharacterInfo class shown in Figure 7-3 so that the tested character is retrieved from user input.

An example of the program is shown below:
Enter a character... A
The character is A
A is uppercase
A is not lowercase
After toLowerCase(), aChar is a
After toUpperCase(), aChar is A
A is a letter or digit
A is not whitespace

User HSBP
by
7.7k points

1 Answer

2 votes

Final answer:

To modify the CharacterInfo class, import the Scanner class for user input, use the Scanner to prompt for and read a character, and then apply various methods like Character.isUpperCase() to check the character's properties.

Step-by-step explanation:

To modify the CharacterInfo class to retrieve a character from user input, you need to ask the user to input a character and then use this character to perform various checks. Here's a step-by-step guide:

  1. Import the Scanner class from the java.util package to get user input.
  2. Create an instance of Scanner to read from the standard input stream.
  3. Prompt the user to enter a character.
  4. Use the Scanner's next() method to read a string and use charAt(0) to get the first character.
  5. Assign this character to the aChar variable in the CharacterInfo class.
  6. Perform the required checks on aChar (is it uppercase, lowercase, letter, digit, whitespace).
  7. Use methods such as Character.isUpperCase(), Character.isLowerCase(), Character.isLetterOrDigit(), and Character.isWhitespace() to perform these checks and print the results.
  8. Use Character.toLowerCase() and Character.toUpperCase() to change the case of the character and print out the results.

By incorporating these changes into your CharacterInfo class, the class will be able to handle dynamic input from the user and provide the necessary character information.

User Raghavsikaria
by
8.7k points