User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the Java section within the Software Development category of DaniWeb, a massive community of 455,965 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,684 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Java advertiser: Lunarpages Java Web Hosting
Views: 2129 | Replies: 8
Reply
Join Date: Nov 2007
Posts: 6
Reputation: xiaotu1990 is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
xiaotu1990 xiaotu1990 is offline Offline
Newbie Poster

Question StringUtil

  #1  
Nov 17th, 2007
i got stuck on this assignment...i have no idea how to write it.well..actually i don't know what it is asked for.. could anyone help me with this piece of code? appreciate it...


You will create a class that will perform several different functions on Strings that are sent to it. All of the methods you create will be static, and the class should work in a similar manner to the Math class. Only the four methods listed below should be public. Any methods that you write to help these methods should be private because they are only used internally within the class.

Create a method that receives a String and returns a String that is the exact reversal of the characters in the first String.

Create a method that receives a String and returns a boolean value of true if the String is a Palindrome and false if it is not. A word is a palindrome if it reads the same forwards and backwards. For example, the word level is a palindrome.

The idea of a palindrome can be extended to phrases or sentences if we ignore details like punctuation. Here are two familiar examples:

Madam, I'm Adam
A man, a plan, a canal: Panama

We can recognize these more elaborate examples as palindromes by considering the text that is obtained by removing all spaces and punctuation marks and converting all letters to their lower-case form.

Madam, I'm Adam ==> madamimadam
A man, a plan, a canal: Panama ==> amanaplanacanalpanama

If the "word" obtained from a phrase in this manner is a palindrome, then the phrase is a palindrome. Your method should ignore the case of the letters. A palindrome is determined by considering only alphabetic characters (a - z, A - Z) and numbers (0 - 9) as valid text.

Note: The World’s Longest Palindrome, created at 8:02 PM on the 20th of February (a palindromic time/date - 20:02 02/20 2002) is reported at http://www.norvig.com/palindrome.html

Use these sample phrases as inputs for your run outputs:

radar
J
Lewd did I live, & evil I did dwel.
I like Java
Straw? No, too stupid a fad, I put soot on warts.

Create a method that receives a String, converts the String to Pig Latin, and returns the new Pig Latinated word. There may be multiple words in your String, so you will need to have a recursive function that breaks down the String into single words and then reconstructs the sentence in Pig Latin. Here's how to translate the English word englishWord into the Pig Latin word pigLatinWord:

If there are no vowels in englishWord, then pigLatinWord is just englishWord + "ay". (There are ten vowels: 'a', 'e', 'i', 'o', and 'u', and their uppercase counterparts. ‘y’ is not considered to be a vowel for the purposes of this assignment, i.e. my becomes myay, why becomes whyay, etc.)
Else, if englishWord begins with a vowel, then pigLatinWord is just englishWord + "yay".
Otherwise (if englishWord has a vowel in it and yet doesn't start with a vowel), then pigLatinWord is end + start + "ay", where end and start are defined as follows:

Let start be all of englishWord up to (but not including) its first vowel.
Let end be all of englishWord from its first vowel on.
But, if englishWord is capitalized, then capitalize end and "uncapitalize" start.
"Astahay alay istavay, abybay." - The Piglatinator

Create a method that receives a String and returns the String converted into shorthand. The simplified shorthand form of a string is defined as follows:

replace these four words: "and" with "&", "to" with "2", "you" with "U", and "for" with "4".
remove all vowels ('a', 'e', 'i', 'o', 'u', whether lowercase or uppercase)

here is the basic structure i guess....

[ public class StringUtil {


/**
* This method reverse all the characters in the string
* @param str
* @return A string with all characters reversed
*/
public static String reverse(String str)
{
return "";
}

/**
* This method will check to see if a string is a palindrome by removing
* all punctuations and spacing
* @param str
* @return True is string is palindrome; false if not
*/
public static boolean palindrome(String str)
{
return false;
}

/**
* This method takes any phrase and makes into Pig Latin
* @param phrase
* @return The English phrase converted to Pig Latin
*/
public static String pigLatinator(String phrase)
{
return "";
}

/**
* This method will create a shorthand out of the phrase -
* - all vowels removed
* - and --> &; for --> 4; you --> U; to --> 2
* @param phrase
* @return The shorthand of the phrase
*/
public static String shortHand(String phrase)
{
return "";
}

}
]


the driver class
[import junit.framework.TestCase;


public class StringUtilTest extends TestCase {

public void testReverse()
{
String str = "Elmo";
String actual = StringUtil.reverse(str);
assertEquals("omlE", actual);
}

public void testPalindrome()
{
String str = "Madam, I'm Adam";
boolean test = StringUtil.palindrome(str);
assertTrue(test);
}

public void testPigLatinator()
{
String str = "Hasta la vista baby.";
String actual = StringUtil.pigLatinator(str);
assertEquals("Astahay alay istavay, abybay.", actual);
}

public void testShortHand()
{
String str = "What are the two of you here for.";
String actual = StringUtil.shortHand(str);
assertEquals("Wht R th 2 f U here 4.", actual);


}
}
]
Last edited by xiaotu1990 : Nov 17th, 2007 at 8:58 pm.
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Apr 2007
Location: US
Posts: 21
Reputation: rpk5000 is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
rpk5000 rpk5000 is offline Offline
Newbie Poster

Re: StringUtil

  #2  
Nov 17th, 2007
public static String reverse(String str)
{
  StringBuilder sb = new StringBuilder();
  sb.append(str);
  return sb.reverse().toString();
}

/**
* This method will check to see if a string is a palindrome by removing
* all punctuations and spacing
* @param str
* @return True is string is palindrome; false if not
*/
public static boolean palindrome(String str)
{
boolean palincheck=false;
  String strr;
  strr=reverse(str);
 strr= strr.replaceAll(".","");
str=str.replaceAll(".","");
  if (str==strr)
    palincheck=true;
return palincheck;
}

Thats a rough way to do the first one, it should work alright, may need some modification to work in whatever your teacher is using as a checker.

Also, the replace all needs to be expanded to include any other stuff you need removed, what it does it take whats before the comma and replace it with ""(nothing). So to get rid of spaces you'd use " ". Removing additional characters can be done in the same replace all statement.
Reply With Quote  
Join Date: Apr 2007
Location: US
Posts: 21
Reputation: rpk5000 is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
rpk5000 rpk5000 is offline Offline
Newbie Poster

Re: StringUtil

  #3  
Nov 18th, 2007
Oh, your going to need a str.toLowerCase() and strr.toLowerCase() as well before the check.
Reply With Quote  
Join Date: Nov 2007
Posts: 6
Reputation: xiaotu1990 is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
xiaotu1990 xiaotu1990 is offline Offline
Newbie Poster

Re: StringUtil

  #4  
Nov 18th, 2007
thank you~~~
Reply With Quote  
Join Date: Apr 2007
Location: US
Posts: 21
Reputation: rpk5000 is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
rpk5000 rpk5000 is offline Offline
Newbie Poster

Re: StringUtil

  #5  
Nov 18th, 2007
Use substring for the piglatin one to do the end/start part which is broken apart by the value. You can use charAt to determine where to find the substring.
Reply With Quote  
Join Date: Nov 2007
Posts: 6
Reputation: xiaotu1990 is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
xiaotu1990 xiaotu1990 is offline Offline
Newbie Poster

Re: StringUtil

  #6  
Nov 19th, 2007
what about the short hand one?
Originally Posted by rpk5000 View Post
Use substring for the piglatin one to do the end/start part which is broken apart by the value. You can use charAt to determine where to find the substring.
Reply With Quote  
Join Date: Nov 2007
Posts: 6
Reputation: xiaotu1990 is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
xiaotu1990 xiaotu1990 is offline Offline
Newbie Poster

Re: StringUtil

  #7  
Nov 19th, 2007
[/**
* This method takes any phrase and makes into Pig Latin
* @param phrase
* @return The English phrase converted to Pig Latin
*/
public static String pigLatinator(String phrase)
{
{
String translation = "";
int phraseLen = phrase.length();
for (int charIndex = 0; charIndex < phraseLen; )
{
int specialCharIndex = indexOf(phrase, phrase, charIndex);
if (specialCharIndex < 0)
{
specialCharIndex = phraseLen;
}
if (specialCharIndex == charIndex)
{
translation = translation + phrase.substring(charIndex, charIndex + 1);
charIndex++;
}
else
{
String engWord = phrase.substring(charIndex, specialCharIndex);
String pigWord = pigLatinator(engWord);
translation = translation + pigWord;
charIndex = specialCharIndex;}
}

return translation;
}
}]
what is wrong with this piece of code for piglatinatorT.T
Reply With Quote  
Join Date: Apr 2007
Location: US
Posts: 21
Reputation: rpk5000 is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
rpk5000 rpk5000 is offline Offline
Newbie Poster

Re: StringUtil

  #8  
Nov 19th, 2007
For shorthand use the replace all thing I showed you, just have multiple replace all statements.
Really sorry, but I don't have time to look at piglatinator right now, whens it due?
Reply With Quote  
Join Date: Nov 2007
Posts: 6
Reputation: xiaotu1990 is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
xiaotu1990 xiaotu1990 is offline Offline
Newbie Poster

Re: StringUtil

  #9  
Nov 19th, 2007
2 days later~but thank you anyway
Originally Posted by rpk5000 View Post
For shorthand use the replace all thing I showed you, just have multiple replace all statements.
Really sorry, but I don't have time to look at piglatinator right now, whens it due?
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb Java Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Other Threads in the Java Forum

All times are GMT -4. The time now is 9:07 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC