Answered step by step
Verified Expert Solution
Question
1 Approved Answer
3. This programming assignment will implement a menu system which allows the user to: - Count the number of punctuation symbols in a String of
3. This programming assignment will implement a menu system which allows the user to: - Count the number of punctuation symbols in a String of text - Convert a String of characters to their binary values - Convert an 8-bit binary String to a character - Exit the program 4. The first three of the above options will be implemented in separate methods. Refer to the following information in implementing the methods described below. To count the number of punctuation marks in a corpus of text, perform the following steps: - Create integer variables named periods (.), questions (?), commas (.), exclamations (!), apostrophes ('), semicolons (;), and backslashes ( ). Initialize each to zero. - For each character in the String of text (from left to right): - Retrieve the character at the current index. - If the character is equal to a certain punctuation mark, increment its counter. - Print each counter variable along with a description followed by a new line. When the counter is 1, the singular version of the description should be used. Otherwise, the plural version should be used. Implementation notes: - The apostrophe and backslash literals must use an escape character. - Copying the apostrophes from this document will cause an error. To convert a single character to its 8-bit binary value, perform the following steps: - Store the character in an integer named n. - Create a String named binary, initially empty. - Repeat the following until n is 0 : - Calculate the remainder of n divided by 2. - Set n to the quotient of n divided by 2 (using integer division). - Prepend (i.e., concatenate on the left) the remainder to the binary String. - If binary has length less than 8 , prepend 0 's to binary until it has a length of 8 . To convert an 8-bit binary String to a character, perform the following steps: - Declare a variable result of type int and initialize it to 0 . - For each character in the input string (from left to right): - If the character is not 0 nor 1 , return the null character (i.e., 0 cast to a char). - If the character is 1 , calculate 2 to the power of the distance from the right end, and add the resulting value to result. (e.g., the bit 1 in the second from the right position - index 6 - contributes 276=2 ). - Cast the int value in result to a char and return it. 5. Write a public and static method in the class Main named printMenu. The method returns nothing, accepts no parameters, and prints the menu options as given with integer choices 1 through 4. Following the menu items, the method prints a prompt String to indicate user input is expected. 6. In the main method, write a loop that calls printMenu until the user decides to quit. Remember to include a dummy read after reading the integer. Write a switch statement to handle user inputs 1,2,3, and 4 . In the cases of 1,2 , and 3 , add stub-code which indicates the choice made. In the case of 4 , set a boolean variable to indicate the user has decided to quit. In all other cases, indicate an invalid menu selection has been made. When the loop ends due to user input, the program should reach the end of the main method and exit. Run and test this code before proceeding. 7. Write a public and static method in the class Main named countPunctuation. The method returns nothing and accepts a String parameter. The method implements the algorithm given above "To count the number of punctuation marks in a corpus of text." 8. In the main method's switch statement, include a prompt in case 1 for the user to enter a String with a few sentences. Call the method countPunctuation with the String from the user sent as an actual parameter. Run and test this code before proceeding. 9. Write a public and static method in the class Main named characterToBinary which accepts a char formal parameter and returns a String. The method implements the algorithm given above "To convert a single character to its 8-bit binary value" and returns the String named binary. 10. In the main method's switch statement, include a prompt in case 2 for the user to enter a String. Print the String following by a colon. On the same line print the binary values of each character by calling characterToBinary with each character from the String sent as an actual parameter. Separate each 8-bit value with a space. Print one new line after the last 8-bit binary value. Run and test this code before proceeding. 11. Write a public and static method in the class Main named binaryToCharacter which accepts a String formal parameter and returns a character. The method implements the algorithm given above "To convert an 8-bit binary String to a character" and returns the explicitly-casted char value of result. 12. In the main method's switch statement, include a prompt in case 3 for the user to enter an 8-bit binary String. Print an error message if the length is not 8. Otherwise, print the 8-bit binary String followed by a colon. On the same line print the value returned by binaryToCharacter when sent the binary String as an actual parameter. Print one new line after the returned value. Run and test this code. An example program execution is provided on the next page. Xxample Program Execution User input is indicated as bold text. Copying apostrophes from below will cause an error. Enter ' 1 ' to count punctuation symbols in a String. Enter ' 2 ' to convert characters to binary. Enter ' 3 ' to convert 8-bit binary to a character. Enter '4' to exit. >1 Enter a few sentences: You may talk o' gin and beer \When you're quartered safe out 'ere, An' you're sent to penny-fights an' Aldershot it; You entered 0 periods, 0 question marks, 1 comma, 0 exclamations marks, 6 apostrophes, 1 semicolon, and 2 backslashes. Enter ' 1 ' to count punctuation symbols in a String. Enter '2' to convert characters to binary. Enter ' 3 ' to convert 8-bit binary to a character. Enter '4' to exit. >2 Enter a few characters: abcd abcd: 01100001011000100110001101100100 Enter ' 1 ' to count punctuation symbols in a String. Enter '2' to convert characters to binary. Enter ' 3 ' to convert 8-bit binary to a character. Enter '4' to exit. >3 Enter an 8-bit binary value: 01010101 01010101: 'U' Enter ' 1 ' to count punctuation symbols in a String. Enter '2' to convert characters to binary. Enter ' 3 ' to convert 8-bit binary to a character. Enter '4' to exit. >5 Invalid menu choice selected. Enter ' 1 ' to count punctuation symbols in a String. Enter '2' to convert characters to binary. Enter ' 3 ' to convert 8-bit binary to a character. Enter '4' to exit. >4
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started