• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Scrambling Words

 
Shyam Prasad Murarka
Ranch Hand
Posts: 209
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear Readers,
I picked up this project from the net. I finished it but I wanted to know whether there is a better way of doing it. A more efficient and professional way. I know my logic is bad but I want to improve. Please don't worry about the other classes. Here are the requirements:

Write a class Scrambler.java which is once again a subclass of FileTransfer. Define a constructor for the Scrambler class using the keyword super, and write an overriding definition for the newWord method that scrambles the words in each line of text. Just remember that your program needs to scramble only the interior characters of each word, leaving the first and last characters untouched, so you'll have to separate out the leading character and the final character of each word before you scramble the interior characters. Also note that words with less than four characters can't be scrambled. Make sure that punctuation marks stay in the same place when you scramble words. For example, a period at the end of a sentence should not be processed as the last character in the word that precedes it.
Notes on using the Java API: You should make extensive use of the Java class library to implement these classes. For example, the String class and the StringBuffer class have methods that will help you a lot. The java.util.Collections class, has a shuffle method that scrambles a List (for example, an ArrayList). Therefore, a way to implement the scrambling function would be:

Put the characters to be scrambled into an ArrayList (as Character objects).
Call Collections.shuffle()
read the characters out of the ArrayList


It would be better if it is copied and pasted into your own editors. Its about 60 lines. Please don't think that this is my homework because it is NOT. I just want to improve my logic and style of writing code. Here is my code:

 
Henry Wong
author
Marshal
Pie
Posts: 21016
78
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


This section of code creates a lot of intermediate String objects.

Anyway, the assignment did mention that you consider using the StringBuffer class. It may be better to use the StringBuffer / StringBuilder class to build the result before converting it to a string.

Henry
 
K Riaz
Ranch Hand
Posts: 375
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Shyam Murarka:
I picked up this project from the net.


I've written my own version, but before I paste the code, can you post the URL of the original website from where you got the question so I can read it in full, as it sounds like a disguised assignment.

Thanks.
[ May 08, 2005: Message edited by: Kashif Riaz ]
 
Shyam Prasad Murarka
Ranch Hand
Posts: 209
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear Kashif,
I picked up this project from RentACoder. I usually go there to see if I can get hold of easy work so that I can practice. And please don't post your full code, if possible, please tell me where I have gone wrong and how I should improve it.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic