Question
In this task you will implement a single-source file program called Character Cruncher , which will perform low-level operations on a piece of text entered
In this task you will implement a single-source file program called Character Cruncher, which will perform low-level operations on a piece of text entered by the user (details are given later). The program will work with an array of characters converted from user's original String-based input. The program could be used to assist someone solving a cryptic crossword, or adapted to be part of a word processor.
In this task it is left to you to decide how the program should be broken down into methods (as a rough guide, a Completed solution will have between 5 and 9 relatively small methods, including main). We will accept a broad range of solutions, not merely the sample solution we created when designing the task.
A description of the program and its functionality is given next, followed by a number of suggestions for how to implement some of the more advanced functionality.
Character Cruncher
The program should implement the following high-level algorithm:
Program: Character Cruncher
Steps: 1 Display the program's name
2 Prompt the user to enter a piece of text (any characters are allowed)
3 Convert that String to lower case and then an array of its individual characters
4 Do
4-1 | Display the array's current contents as a single piece of text, | prefixed by "Letters: "
4-2 | Display a menu of operations for interacting with that text
5 While the user has not selected quit
After step 2 the original line of text is no longer used, only the array of characters, but whenever it is displayed it will appear as a single piece of text. For example, if the user typed 'Hello World' then it will be displayed later (at line 4-1) as 'Letters: hello world'.
The available actions are:
- Display the text in reverse. Given the example above the program would display 'dlrow olleh'.
- Display the text in sorted order. This requires making a copy of the array, sorting the copy, then displaying it, but there are library functions that can help. Given the example above, this would display ' dehllloorw' (the system would consider the space character to belong before the letters).
- Replace all occurrences of one character with another, which will prompt the user to enter a character to be replaced and a replacement character (both of which it will convert to lower case). The program will then replace all occurrences of the character to be replaced in the array. For example, starting with the example above, if the user entered 'l' and 'p' then the text in the array will become 'heppo worpd'. (The program does not have to support replacing whitespace characters as the Scanner makes it difficult to read these on their own.)
- Replace within a range, which works like replace all but also asks the user for the start and end of the range to replace within. The start index is inclusive while the end is exclusive, and both can be zero-based (as in your code). For example, starting with the original example above, if the user entered 'l' and 'p' as the replacement and positions 3 and 9 as the range then the text in the array would become 'helpo world', because the second 'l' is at the included start position 3, but the final 'l' is at excluded end position 9.
The third and fourth operations modify the state of the array, which is why the program displays the text it contains after every user action.
Implementation advice
Converting a line of text into an array of characters
Review the documentation for Strings and you will find a useful method for obtaining an array of characters from a String. Do not write a loop to do this conversion.
Displaying the array as a single piece of text
Look at the documentation for the many versions of println and you'll find this task is actually very easy. Do not write any loop code to display the array of characters.
Displaying the array in reverse
There are no convenient library methods to achieve this, so it will be easier to write your own code to print() the individual characters starting at the end of the array (.length - 1) going backwards while the loop variable is greater than or equal to 0.
(If you really want to try to do it with library functions then look at the StringBuilder, but be aware it works with Strings, not char[], so you will need to create a String first.)
Displaying the array in sorted order
This will require you to sort the characters first, but the original array of characters should not be changed in the process. So you will need to:
- take a copy of the array, and then
- sort it, and then
- display that sorted array as a single piece of text.
Reading individual characters
The Scanner has no nextChar() method, so you need to combine next() (to read a single-character 'word' from the user) with charAt(0) to extract the first (and only) character from that String.
Replace all and replacing all within a range
These are most easily implemented yourself using for loops, but if you find a library method that assists then you can use it.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Here is a possible implementation of the CharacterCruncher program import javautilScanner import javautilArrays public class CharacterCruncher public ...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