Java代写:KIT101_A9_DivideAndConquer

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
2
3
4
5
6
7
8
9
Resources: Introductory Programming Notes:
07 Methods in Self-contained Programs
08 Making Decisions
09 Repeating Actions with Loops
10 Managing Collections with Arrays
13 Functional Decomposition
Online Mini-lectures:
Making your own methods: ...in self-contained programs
Arrays: Writing methods to work with arrays

 Submission Details

1
2
3
4
Upload the following to the MyLO submission folder for this task:
 The source code for your program
 A screenshot of the program during execution
 An image (photo or scan) of the structure chart describing your program

 Assessment Criteria

1
2
3
4
5
6
7
8
9
A 🏁 Completed submission will:
Implement all required functionality, including the use of an array of Strings for the data
Import only those additional classes that it actually uses
Have little code in main() (likely less than 10 lines of code, but also more than just one)
Exhibit good use of functional decomposition, with a number of small methods to solve different parts of the
task
Show the connections between methods in your program in the structure chart
Make use of String methods wherever suitable
Followtheunit’scodingstyleforlayout variablenames(useofcase) andcommenting(includingyournameat
KIT101 Programming Fundamentals CL
1
2
3
Follow the units coding style for layout, variable names (use of case), and commenting (including your name at
the top)
Compile and run, with the screenshot showing that it works

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
2
3
4
5
6
7
8
9
10
Steps:
1 Display the program's name
2 Prompt the user to enter a sentence (which may include punctuation)
3 Convert that String into an array of the individual words it contains,
removing common punctuation (e.g., .,?!) in the process
4 Do
4-1 | Display the array's contents as a sentence (that is, as words separated
| by spaces, ending with a full stop), prefixed by "Text: "
4-2 | Display a menu of operations for editing the sentence
5 until the user selects quit

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
2
3
4
5
6
displayed sentence would be An edited sentence for an example line. (Most swaps will make a sentence
grammatically incorrect, but that’s the user’s problem, not yours.)
Shufe , which will swap random pairs of positions in the text length-of-sentence / 2 times (this number is
arbitrary, but should be enough to make the new arrangement quite different from the original).
For example, the program would perform three swaps on the sentence above as 7/2 is 3 when performing
integer division.

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
2
3
String original = "This is a sentence."
String[] result = original.split(" ");
//result is now the array ["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
2
3
4
5
String original = "What, he said, an over-punctuated sentence!"
String[] result = original.split("[ ,.!?\"]+"); //matches one or more of the
charcters inside []
//result is now the array ["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
2
 Note: Because split() returns a new array of the correct size for the data you do not need to allocate
space for the array using new String[].
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

Task: View this topic

Last Visited 28 May, 2021 04:
Activity Details