0% found this document useful (0 votes)
51 views3 pages

Alternative-9 1PP

Uploaded by

Reborned Link
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
51 views3 pages

Alternative-9 1PP

Uploaded by

Reborned Link
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

KIT101 Programming Fundamentals

PP Task 9.1 Divide and Conquer

Overview

Purpose: Apply your functional decomposition skills to a new task with familiar
elements.

Task: Implement a text editing program consisting of a number of relatively small


methods.

Learning Outcomes: 2  3 4 5

Time: Complete by required due date

Resources: Introductory Programming Notes:


05 Using Objects
07 Methods in Self-contained Programs
08 Making Decisions
09 Repeating Actions with Loops
10 Managing Collections with Arrays
14 Functional Decomposition
Online Mini-lectures:
Making your own methods:  …in self-contained programs
Arrays:  Writing methods to work with arrays

 Submission Details

Submit the following over email:


 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

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 (including parameters and return values) between methods in your
program in the structure chart
Make use of String and java.util.Arrays methods wherever suitable
Follow the unit’s coding style for layout, variable names (use of case), and commenting
(including your name at the top)
Compile and run
Be demonstrated and explained to the unit coordinator in a short interview
Instructions
In this task you will implement a single-source file program called Text Editor, 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.

Text Editor

The program should implement the following high-level algorithm:

Program: Text Editor

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 upper 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 "Text line: "
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 ‘Text line: hello world’.

The available actions are:


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).
Display the text in reverse. Given the example above the program would display ‘DLROW
OLLEH’.
Count a character, which displays the number of times a user-given character appears in the
array. This must be a case insensitive comparison, so if the user typed ‘o’ then the program
should report there are 2 occurrences.
Find and replace 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
upper 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’.

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 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.

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.)

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.

Counting occurrences of a character


You will need to write a loop for this. Remember to convert the user’s input to upper case first to
make the comparison easier.

Find and replace one character with another


This is most easily implemented yourself using a for loop, but if you find a library method that
assists then you can use it.

Ask us for help


We can give you additional advice if you tell us which part you’re stuck on. Just ask.

You might also like