| | |
StringUtil lab
![]() |
•
•
Join Date: Nov 2008
Posts: 1
Reputation:
Solved Threads: 0
I got most of the basics but i need help on getting all of the requirements right and EVERYTHING I NEED TO DO. Is there a way to replace certain chars without actually using str.replace();, especially punctuation?
This is the assignment
1.
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.
2.
Create a method that receives a String and returns a String that is the exact reversal of the characters in the first String.
3.
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.
4.
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:
1. 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.)
2. Else, if englishWord begins with a vowel, then pigLatinWord is just englishWord + "yay".
3. 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:
1. Let start be all of englishWord up to (but not including) its first vowel.
2. Let end be all of englishWord from its first vowel on.
3. But, if englishWord is capitalized, then capitalize end and "uncapitalize" start.
"Astahay alay istavay, abybay." - The Piglatinator
5.
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:
1. replace these four words: "and" with "&", "to" with "2", "you" with "U", and "for" with "4".
2. remove all vowels ('a', 'e', 'i', 'o', 'u', whether lowercase or uppercase)
this is my code so far
public class Palindrome {
public static String reverse(String str) {
if (str.length() == 1)
return (str.substring(0));
else
return (reverse(str.substring(1)) + str.charAt(0));
}
public boolean palindro(String str) {
str = str.toLowerCase();
str = (stripPunctuation(str));
if (str.equals(reverse(str)))
return true;
else
return false;
}
private static String stripPunctuation(String str) {
str.replace("[ @]", "");
str.replace(".", "");
str.replace(",", "");
str.replace("!", "");
str.replace("?", "");
str.replace("%", "");
return str;
}
public static String shorthand(String str) {
str = replaceWord(str);
str = stripVowels(str);
return str;
}
private static String stripVowels(String str){
str = str.replace("a", "");
str = str.replace("e", "");
str = str.replace("i", "");
str = str.replace("o", "");
str = str.replace("u", "");
return str;
}
private static String replaceWord(String word){
String replacement = (word.replace("for", "4"));
replacement = (word.replace("and", "&"));
replacement = (word.replace("to", "2"));
replacement = (word.replace("you", "u"));
return replacement;
}
public static String pigLatin(String sent) {
if (sent.indexOf(" ") == -1)
return pigLatin(sent);
else {
int index = sent.indexOf(" ");
String word = sent.substring(0, index);
return (piglatinated(word) + pigLatin(sent));
}
}
private static String piglatinated(String word) {
String one = word.substring(0, 1);
word.replace("", one);
word = (word + one + "ay");
return word;
}
}
//Driver
import java.util.Scanner;
public class PalindromeDriver {
public static void main(String[] args) {
Palindrome pal = new Palindrome();
Scanner scan = new Scanner(System.in);
System.out.println("Enter a word or a sentence");
String str = scan.nextLine();
System.out.println("Original; " + str);
System.out.println("In reverse: " + pal.reverse(str.toLowerCase()));
System.out.println("Is it a palindrome? " + pal.palindro(str));
System.out.println("In PigLatin: " + pal.pigLatin(str));
System.out.println("Shorthanded: " + pal.shorthand(str));
}
}
This is the assignment
1.
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.
2.
Create a method that receives a String and returns a String that is the exact reversal of the characters in the first String.
3.
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.
4.
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:
1. 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.)
2. Else, if englishWord begins with a vowel, then pigLatinWord is just englishWord + "yay".
3. 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:
1. Let start be all of englishWord up to (but not including) its first vowel.
2. Let end be all of englishWord from its first vowel on.
3. But, if englishWord is capitalized, then capitalize end and "uncapitalize" start.
"Astahay alay istavay, abybay." - The Piglatinator
5.
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:
1. replace these four words: "and" with "&", "to" with "2", "you" with "U", and "for" with "4".
2. remove all vowels ('a', 'e', 'i', 'o', 'u', whether lowercase or uppercase)
this is my code so far
public class Palindrome {
public static String reverse(String str) {
if (str.length() == 1)
return (str.substring(0));
else
return (reverse(str.substring(1)) + str.charAt(0));
}
public boolean palindro(String str) {
str = str.toLowerCase();
str = (stripPunctuation(str));
if (str.equals(reverse(str)))
return true;
else
return false;
}
private static String stripPunctuation(String str) {
str.replace("[ @]", "");
str.replace(".", "");
str.replace(",", "");
str.replace("!", "");
str.replace("?", "");
str.replace("%", "");
return str;
}
public static String shorthand(String str) {
str = replaceWord(str);
str = stripVowels(str);
return str;
}
private static String stripVowels(String str){
str = str.replace("a", "");
str = str.replace("e", "");
str = str.replace("i", "");
str = str.replace("o", "");
str = str.replace("u", "");
return str;
}
private static String replaceWord(String word){
String replacement = (word.replace("for", "4"));
replacement = (word.replace("and", "&"));
replacement = (word.replace("to", "2"));
replacement = (word.replace("you", "u"));
return replacement;
}
public static String pigLatin(String sent) {
if (sent.indexOf(" ") == -1)
return pigLatin(sent);
else {
int index = sent.indexOf(" ");
String word = sent.substring(0, index);
return (piglatinated(word) + pigLatin(sent));
}
}
private static String piglatinated(String word) {
String one = word.substring(0, 1);
word.replace("", one);
word = (word + one + "ay");
return word;
}
}
//Driver
import java.util.Scanner;
public class PalindromeDriver {
public static void main(String[] args) {
Palindrome pal = new Palindrome();
Scanner scan = new Scanner(System.in);
System.out.println("Enter a word or a sentence");
String str = scan.nextLine();
System.out.println("Original; " + str);
System.out.println("In reverse: " + pal.reverse(str.toLowerCase()));
System.out.println("Is it a palindrome? " + pal.palindro(str));
System.out.println("In PigLatin: " + pal.pigLatin(str));
System.out.println("Shorthanded: " + pal.shorthand(str));
}
}
Hello, lone_emu:
At first, to use your static methods, you don't have to create an object of class Palindrome. So this:
is not wanted here.
To replace chars in the string (not using replace function) you can convert your string to char array, replace the elements at specified indexes and the convert it back to string
At first, to use your static methods, you don't have to create an object of class Palindrome. So this:
java Syntax (Toggle Plain Text)
Palindrome pal = new Palindrome();
To replace chars in the string (not using replace function) you can convert your string to char array, replace the elements at specified indexes and the convert it back to string
So what if you can see the darkest side of me?
No one would ever change this animal I have become
Help me believe it's not the real me
Somebody help me tame this animal
No one would ever change this animal I have become
Help me believe it's not the real me
Somebody help me tame this animal
![]() |
Other Threads in the Java Forum
- Previous Thread: binary search tree question
- Next Thread: Need urgent help in code!
| Thread Tools | Search this Thread |
account android applet application apps array automation awt bidirectional binary birt bluetooth businessintelligence busy_handler(null) card class classes client code collision columns component constructor database designadrawingapplicationusingjavajslider draw eclipse error eventlistener exception expand fractal free game givemetehcodez graphics gui guidancer homework html ide image inheritance integer integration intellij j2me java javafx javamicroeditionuseofmotionsensor javaprojects jlabel jme jni jpanel jtextfield jtree julia linux loop method midlethttpconnection migrate mobile mobiledevelopmentcreatejar monitoring myaggfun netbeans newbie nullpointerexception open-source oracle plazmic print problem program property ria scanner server set sharepoint smart sms smsspam sourcelabs splash sql sqlite subclass support swing testautomation textfield threads tree trolltech unlimited utility windows





