[SOLVED] INST2002 Programming 2

$25

File Name: INST2002_Programming_2.zip
File Size: 207.24 KB

5/5 - (1 vote)

Important Notice

INST2002 Programming 2: Support Sheet for Online Assessment 2

Set by: Luke Dickens

Available from 27th November, 2015 Due at noon on 8th December, 2015

This assessment forms part of your degree assessment. It must be done entirely on your own from start to finish:

  • You must not collaborate or work with other students at any stage.
  • You must not send or show other students your answers.
  • You must not ask other students for help, or ask to see their answers. As well as being against regulations, this is unfair to the other student concerned, since it may lead to them being accused of plagiarism.
  • You must not seek help from friends, relatives, online discussion groups other than the moodle forum for INST2002
  • If you think any of the description of the task below is ambiguous or unclear, please post to the moodle forum, explaining what your concerns are, or raise it in person with your lecturer, Luke Dickens, or a INST2002 lab demonstrator.
  • If you are unsure of any of the above points, please post your concern to the moodle forum.

    Finally, if there is any reason you do not think you can complete this assessment in the alloted time, you should either make a formal request for an extension with your home department, or discuss your reasons with the INST2002 lecturer, Luke Dickens at [email protected].

1 A Chat Room with a Profanity Filter

Imagine that you are asked to write code to support a collection of simple text based online chat-rooms, with a configurable profanity filter. The chat-rooms consist of users posting short text messages (posts), which are visible to others in the chat-room alongside the name of the user who posted it. When reading these posts, some users will want profanities to be filtered out, while some will not. For simplicity, we will assume that the list of profanities

1

are the same for all users and in all chat-rooms. However, it should be possible to add and remove words from the profanity filter.

For example consider the following sequence of messages in a chat room whose name is VictorianGents.

post order

0

1 2

3 4 5 6

username

"Raffles""Bunny""Raffles"
"Dave""Raffles""Dave""Raffles"

text

"Hello Bunny, are you there?""Here I am, Raffles.""Excellent Bunny.Have you seen Davethe nincompoop?"
"How dare you call me a nincompoop, yourapscallion.""Oh tish and pish Dave.Quit yourblithering, you twit."
"Zounds!You knave!Ill not sufferyour insults further.""Gadzooks!Was it something I saidBunny?"

Table 1: An example chat history for the VictorianGents chat-room.

Now, imagine that a technician has added the following terms to the profanity filter: blither, blithering, gadzooks, knave, pish, nincompoop, rapscallion, tish, twit, and twits.

Without the profanity filter switched on, the chat history for the VictorianGents chat room would display to screen as shown in Figure 1:

Chat Room VictorianGents:
Raffles: Hello Bunny , are you there?
Bunny: Here I am, Raffles.
Raffles: Excellent Bunny. Have you seen Dave the nincompoop? Dave: How dare you call me a nincompoop, you rapscallion. Raffles: Oh tish and pish Dave. Quit your blithering, you twit. Dave: Zounds! You knave! Ill not suffer your insults further. Raffles: Gadzooks! Was it something I said Bunny?

Figure 1: Unfiltered chat history for VictorianGents displayed to screen

If the same chat history were again displayed to screen, this time with the profanity filter switched on, then it would appear as in Figure 2.

Note: All parts of the original text that match a word stored in the profanity filter has had each character replaced with an asterisk, *, in the filtered text.

Note: The word blithering in Figure 1 has been replaced in Figure 2, with **********. This is the case even though the shorter word blither is also in the list.

2

Chat Room VictorianGents [filtered]:
Raffles: Hello Bunny , are you there?
Bunny: Here I am, Raffles.
Raffles: Excellent Bunny. Have you seen Dave the **********? Dave: How dare you call me a **********, you ***********. Raffles: Oh **** and **** Dave. Quit your **********, you ****. Dave: ******! You *****! Ill not suffer your insults further. Raffles: ********! Was it something I said Bunny?

Figure 2: Filtered chat history for VictorianGents displayed to screen

Note: If there is a choice between filtering one of two words, e.g. either blither or blithering, then the longer profanity should be replaced with asterisks. If two equally long profanity words overlap, then either word may be replaced with asterisks.

Note: The profanity filter should be case-insensitive, meaning that it should filter words whether they appear in uppercase, lowercase or a mixture of cases, e.g. if blithering is a profanity word, then it should filter blithering, BLITHERING, Blithering, and so on.

Note: For simplicity, the profanity filter should filter any sequence of characters match- ing a profanity, even if that word is contained within another word, e.g. if tish is a profanity word then the text, I am British, should be filtered to be I am Bri****. In a real world profanity filter, this may not be the preferred behaviour.

Finally, it should be possible to look at the history of just one user, with or without the filter applied. For instance, if we wanted to see just Raffles history filtered and printed to screen then it would appear as in Figure 3.

Figure 3: Filtered chat history for VictorianGents displayed to screen

2 Your Task

In the code file Problem.java given to you, there are four classes: a public class Problem, and three package visible classes Post, ProfanityFilter, and ChatRoom. The class Problem has been written for you to test the code, and you do not need to edit this class. However, you may choose to edit this class to test your changes as you go. The class Post has also been written for you. Post is a very simple class that stores the user-name and text of a chat-room post, and can convert these two pieces of information to a String. You must

Chat Room VictorianGents [filtered]:
Raffles: Hello Bunny , are you there?
Raffles: Excellent Bunny. Have you seen Dave the **********? Raffles: Oh **** and **** Dave. Quit your **********, you ****. Raffles: ********! Was it something I said Bunny?

3

not edit the class Post. You will need to edit the other two classes, ProfanityFilter and ChatRoom. The required changes are described below.

2.1 The ProfanityFilter class

A ProfanityFilter object contains a collection of Strings each of which is a profanity, i.e. a word that should be filtered out. It also provides methods for filtering out these profanities in text. You should implement the following in ProfanityFilter.

  • One or more fields to the ProfanityFilter class to store the Strings representing the profanity words.
  • A constructor, which takes no arguments and constructs an empty ProfanityFilter, i.e. a ProfanityFilter with 0 profanity words contained in it.
  • An addProfanity method, which takes a String as input, and includes it as a profanity word.
  • A removeProfanity method, which takes a String as input, and removes it from the collection of a profanity words if it is present. Otherwise, it should do nothing.
  • A getProfanities method, which takes no arguments and returns a String array of all the profanities stored in the ProfanityFilter.
  • A sortProfanitiesByLength method, which takes no arguments and sorts the pro- fanities by length, with the longest words appearing first and the shortest appearing last. Two profanity words of the same length can appear in either order. After a call to sortProfanitiesByLength, the getProfanities method should return the profanities in length order.
  • A static method filterWordInText, which takes two Strings as arguments: profanity a word which should be replaced with a dummy string of asterisks; and text the text in which profanity will be replaced. The method should return a string of filtered text, i.e. text with every occurance of profanity replaced by a String of asterisks

    of the same length.

    Forexample,iftextistheStringQuit your blithering, you twit.andprofanity is the String blithering, then filterWordInText should return Quit your **********, you twit.. You may find it helpful to use the method createDummyString for filterWordInText.

    Note: This method can be implemented with loops, or recursion. You will get extra credit if you use recursion.

  • A method filterText, which takes a String called text as argument and returns a String which is a copy of the input text with all stored profanity words replaced by asterisks.

    Note: You should filter for the longest profanity words first.

4

2.2 The ChatRoom class

A ChatRoom object contains a collection of Posts in the order in which they were posted. ChatRoom objects provide methods to get histories of filtered and unfiltered posts.

All ChatRoom objects have access to the same ProfanityFilter object, which they
can use to filter out bad-language. This ProfanityFilter can be accessed by the static getProfanityFilter method, and set by the static setProfanityFilter method. Both
these methods are provided for you. Each ChatRoom also has two instance fields: the roomName of type String, which is the name of the chat-room; and posts of type ArrayList<Post>, which collects all messages posted to the chat-room. There are some other methods provided
for you too, see the code for details.

3

You will need to implement the following methods in ChatRoom:

A constructor, which takes a single argument roomName of type String and stores this as the chat-room name. This constructor should create a ChatRoom with 0 posts in its history.

A getRoomName method, which is a simple getter method for the roomName field.

An addPost method, which takes two Strings as input: author the author of the post; and text the text of the post. This method should store this information as a Post.

A getHistory method, which takes no arguments and returns a String array, con- taining the unfiltered string representations of each post to the chat-room, in the order they were added (by addPost).

A getFilteredHistory method, which takes no arguments and returns a String array, containing the filtered string representations of each post to the chat-room, in the order they were added.

Note: Filtered posts are those that have been filtered by the ProfanityFilter. These should filter both the author name and text of the post.

A getAuthorHistory method, which takes a String argument author, and returns a String array, containing the unfiltered string representations of each post to the chat-room by author. Again, posts should be displayed in the order they were added.

A getFilteredAuthorHistory method, which takes a String argument author, and returns a String array, containing the filtered string representations of each post to the chat-room by author. Again, posts should be displayed in the order they were added.

Note: Filtered posts are those that have been filtered by the ProfanityFilter. These should filter both the author name and text of the post.

Some Advice

You have been provided with signatures for all the methods described above in the code. You should not change any of these signatures. In particular, for the methods provided in the code:

5

Do not change the return type.
Do not change the visibility, e.g. public.
Do not change the number or type of the input arguments. Do not change whether a method is static or not.

You may write your own additional methods, and you may add fields to the ProfanityFilter and ChatRoom classes. In some cases, it may be easier to solve the problem if you define helper methods, and you will be given credit if these are used appropriately.

You may find some of the standard Java library helpful to solve this problem. For instance, the String class and its methods: length, toLowerCase, substring (2 overloaded methods). getChar, and equals. You may also find the following methods in ArrayList helpful: size, toArray, get, set, and remove. Make sure that you read up on how to use these methods in the Java documentation.

Also, it is sometimes better to use an array and sometimes it is better to use an ArrayList. You must decide when one is better than the other. You can always convert from one to the other when needed.

4 A Local Copy of the Code

If you go to moodle, you can access a local copy of the code. This is the same code that will appear in the text box when you finally log in to TestDome. You can experiment with this local copy to test your ideas, before you log in to TestDome. Try compiling this local code to start with, then running it to see what happens. At this point, try to make changes in line with the description above, and recompile and run regularly to test your changes.

There is more than one way to solve this problem, and so long as you can pass all the tests on TestDome you will be given a minimum of 80% overall. Additional marks will be given for coding style and comments, up to a maximum of 20%. This includes, but is not limited to: good names for variables, appropriate use of control flow (e.g. for loops and if statements), meaningful and appropriate comments, minimising duplication of code, appropriate helper methods, and consistent formatting of code.

6

Reviews

There are no reviews yet.

Only logged in customers who have purchased this product may leave a review.

Shopping Cart
[SOLVED] INST2002 Programming 2
$25