Win a copy of TDD for a Shopping Website LiveProject this week in the Testing forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Scrambling Words

 
Ranch Hand
Posts: 209
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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:

 
author
Posts: 23919
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 375
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
When people don’t understand what you are doing they call you crazy. But this tiny ad just doesn't care:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic