File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Scrambling Words Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Scrambling Words" Watch "Scrambling Words" New topic

Scrambling Words

Shyam Prasad Murarka
Ranch Hand

Joined: May 02, 2005
Posts: 209
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 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:

With Best Regards,
Shyam Prasad Murarka
Henry Wong

Joined: Sep 28, 2004
Posts: 18120

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.


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
K Riaz
Ranch Hand

Joined: Jan 08, 2005
Posts: 375
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.

[ May 08, 2005: Message edited by: Kashif Riaz ]
Shyam Prasad Murarka
Ranch Hand

Joined: May 02, 2005
Posts: 209
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.
I agree. Here's the link:
subject: Scrambling Words
Similar Threads
palindrome checker, tried to debug, keeps looping why?
Punction Correct (Sentances)
Finding anagrams in a dictionary file using pre-fixes
Shuffle a string
Place A Word In A 2d Array