9.1PP Divide and Conquer
KIT101 Programming Fundamentals
PP Task 9.1 Divide and Conquer
Overview
1 | Purpose: Apply your functional decomposition skills to a new task with familiar elements. |
1 | Task: Implement a sentence manipulation program consisting of a number of relatively small methods. |
1 | Learning Outcomes: 2 3 4 5 |
1 | Time: Aim to complete this task before the start of Week 10. |
1 | Resources: Introductory Programming Notes: |
Submission Details
1 | Upload the following to the MyLO submission folder for this task: |
Assessment Criteria
1 | A 🏁 Completed submission will: |
KIT101 Programming Fundamentals CL
1 | Follow the units coding style for layout, variable names (use of case), and commenting (including your name at |
Instructions
In this task you will implement a single-source le program called Sentence Editor , which will manipulate the
arrangement of words in a sentence entered by the user (details are given later). 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 7 relatively small methods, including main). As your program will work with the user-entered
sentence as an array of words, it will also make use of methods from the String class to save implementation effort.
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 trickier functionality.
Sentence Editor
The program should implement the following high-level algorithm:
1 | Program: Sentence Editor |
1 | Steps: |
After step 3, the original sentence is no longer used, only the array of the words extracted from it. Assuming that the
user originally entered the sentence ‘An example sentence, for an edited line.’ this would be displayed as:
1 | Text: An example sentence for an edited line. |
The available operations are:
Display the sentence as a question (that is, ending with? instead of. and without showing the ‘Text:’ prex)
Display the sentence as an exclamation (like the previous option but using! instead of .)
Find-replace , which will prompt the user to enter a word to nd and a replacement word. The program will
then replace all occurrences of the _nd_ word in the array (doing case-insensitive matches) and display the
number of replacements made.
For example, starting with the sentence above, if the user entered ‘an’ and ‘the’ then the displayed sentence
would become ‘the example sentence for the edited line’ and the program would display that there were 2
replacements. (The program does not have to restore sentence capitalisation.)
Swap , which will prompt the user to enter two positions within the text and then swap the words at those
positions. The prompt should indicate the valid range of positions, which must be 1 to the length of the text (so
remember to subtract 1 from what the user enters), but it can assume that the user will enter valid values.
For example, starting with the sentence given above, if the user entered the positions 2 and 6 then the new
displayedsentencewouldbe‘Aneditedsentenceforanexampleline’(Mostswapswillmakeasentence
1 | displayed sentence would be An edited sentence for an example line. (Most swaps will make a sentence |
The last three operations modify the state of the array, which is why the program displays the sentence it represents
after each user action.
Implementation advice
Converting the sentence into an array of words
Converting the sentence into an array of Strings is easier than it may at rst appear. You should not write a loop to
split the sentence.
Strings have a split() method, which will break the String into an array of substrings separated by characters you
specify. To split a String at each space character you can do the following:
1 | String original = "This is a sentence." |
The argument to split() is actually a regular expression , which is a pattern that can match a variety of String values.
The pattern “ “ matches a single space character. In order to prevent any commas, full stops and some other
punctuation characters from being left attached to the words in the sentence you can specify these as a set by
placing them inside square brackets [ ], followed by a + to say ‘match 1 or more of these characters’. For example,
1 | String original = "What, he said, an over-punctuated sentence!" |
In that example it matches any of space, comma, full stop, exclamation mark, question mark, or double quote (which
must be given as \”), appearing one or more times in sequence. You can adjust the set of punctuation characters
however you think appropriate.
1 | Note: Because split() returns a new array of the correct size for the data you do not need to allocate |
Reconstrucing the sentence for display
Although you’ve written code to do something similar before, we strongly suggest you use the class method
String.join() to convert the array of words to a displayable String, with individual words separated by a single
space. (The rst parameter is the separator to use between array elements, while the second CharSequence…
parameter actually matches a String[] variable.)
1 | Download Print |