Question: String Methods: Name your application: StringMethods.java You will write the three Utility Methods and any 16 of the 20 different main String Methods exactly as
String Methods:
Name your application: StringMethods.java
You will write the three Utility Methods and any 16 of the 20 different main String Methods exactly as described below. Begin by creating a project called StringMethods.
As you implement a method replace the default print statement in the stub and adjust its return value.
SO, your methods MUST have the EXACT same signatures and return types as given in the methods descriptions below.
This means the exact same method names and the exact same number and types of parameters.
NOTE 1: You may NOT use any Character class methods from the API.
The only String class methods allowed are: length, charAt, and substring. DO not use ANY OTHER JAVA API Classes or METHODS. You MAY call any of the methods.
you write from any other method in this assignment. (This implies that you may need/want to write utility methods such as equals, isLetter, isConsonant, isDigit, and others that will be useful to you.)
NOTE 2: NO print statements and NO input statements should be used in ANY of the 20 methods. Every method gets input via its parameters and produces output ONLY by returning a value (as defined below).
Utility Methods. The following methods must be written before you start on the String Methods. Write others as you see fit to help with your primary methods.
String charactersOnly(String); accepts a string. Returns a String stripped of all non characters and converted to all lower case. For example, if the string passed in is The answer: 42, is the QUESTION!!!, the string returned would be theansweristhequestion.
boolean isVowell(char); Accepts a character. Returns true if the character is a,e,i,o or u. False otherwise
boolean isConsonant(char); Accepts a character. Returns true if it is not a vowell. False otherwise.
boolean isDigit(char); Accepts a character. Returns true if is a digit. False otherwise.
boolean equals(String,String); Accepts two strings. Returns true if they are equal, false otherwise.
Primary String Methods. You must complete 16 of the 20 methods.
boolean isPalindrome(String): accepts a string and returns true if the string is a palindrome, otherwise returns false; should ignore spaces, punctuation and case (upper and lower are the same). So, the following ARE palindromes: Never odd or even., Cigar? Toss it in a can. It is so tragic., A man, a plan, a canal: Panama.
String reverse(String): accepts a string and returns a string that is the reverse of the original
String jumble(String): accepts a string and returns a jumbled version of the original: for this method, jumbled means that two randomly chosen characters other than the first and last characters of the string are swapped; This method may use the class, Random. The method must swap two different characters: in other words the two random indices into the string cannot be equal, cannot be 0, and cannot be equal to the strings length minus one. So, for example, a four-letter string MUST result in the returned string having the same first and last characters and have the second and third characters swapped. Examples of what this method might do: fist returns fsit, much returns mcuh, but for longer strings there will be more possible return values: spill could return splil or sipll. Only ONE pair of letters should be swapped and strings shorter than four characters are returned unchanged. When the string length is greater than 3, the original string must never be returned.
String swapPairs (String): accepts a string and returns a string that has adjacent pairs of the original string swapped; for strings of odd length, leave the last character unchanged. Examples:
IN: examples OUT: xemalpse
IN: count OUT: ocnut
int countVowels(String): accepts a string of any characters and returns the number of vowels in the string. (aeiou)
int countConsonants(String): accepts a string of any characters and returns the number of consonants in the string
boolean allDigits( String ) : accepts a string and returns true if every character is a digit, otherwise false
boolean allAlpha( String ) : accepts a string and returns true if every character is a letter (either case), otherwise false
String stripVowels(String): accepts a string and returns the string with no vowels. Example: IN: s-cat=tered OUT: s-ct=trd
String stripConsonants(String): accepts a string and returns the string with no consonants. Example: IN:
s_cat$tered OUT: _a$ee
String replace( String, char1, char2 ): accepts a string and two characters, returns the string with all occurrences of char1 replaced by char2. Example:
IN: mississippi, i, o OUT: mossossoppo
int countChar(String, char): accepts a string and a character, returns the number of occurences of the character in the string. Example: IN: is_si-ssi=pp!i, i OUT: 4
String multiString( String ): accepts a string and returns a string with repeated characters: the first character is repeated string-length times, the second character is repeated one less time, until the last character is included only once (not repeated). Example:
IN: test OUT: tttteeesst IN: sentence OUT: sssssssseeeeeeennnnnnttttteeeennncce
String lowerCase( String ): accepts a string and returns the string in all lowercase; do not use the String classs toLowerCase method, do not use the Character class method either.
String upperCase( String ): accepts a string and returns the string in all uppercase; do not use the String classs toLowerCase method, do not use the Character class method either .
int findInStr(String s2, String s2): accepts two strings and returns the character index of the position where the second string begins in the first; returns -1 if the second string does not appear in the first. Examples:
IN: mississippi ss OUT: 2
IN: superlative zzl OUT: -1
int findInStr(String s2, char c): overloaded version of previous method, accepts a string and a character,
returns the index of the position where the character first appears in the string; returns -1 if the character does not appear in the string. Examples:
IN: dazzling z OUT: 2
IN: superlative z OUT: -1
int findInStrLast(String s2, String s2): accepts two strings and returns the character index of the
rightmost position where the second string begins in the first; returns -1 if the second string does not appear in the first. Examples:
IN: mississippi ss OUT: 5
IN: superlative zzl OUT: -1
int findInStrLast(String s2, char c): overloaded version of previous method, accepts a string and a
character, returns the index of the rightmost position where the character first appears in the string; returns -1 if the character does not appear in the string. Examples:
IN: dazzling z OUT: 3
IN: superlative z OUT: -1
String inOrder(int a, int b, int c): accepts 3 integers and returns a string containing the three integers in ascending order separated by a single space between the first two and another single space between the second and third. Examples:
1, 2, 3 returns 1 2 3; 199, 8, 23 returns 8 23 199 .
Finally, write a main method only to test your methods.
StringMethods.java so far:
package stringmethods;
/**
*
* @author xxxxx
*/
public class StringMethods {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// Call each method to demonstrate it's functionality
//System.out.println(isPalindrome("abc d!!#cb123a")) ;
//System.out.println(reverse("reverse"));
//System.out.println(jumble("reverse"));
//System.out.println(swapPairs ("reverse"));
//System.out.println(countVowels("missasseppssa"));
//System.out.println(countConsonants("missasseppssa"));
//System.out.println(allDigits("123456"));
//System.out.println(allAlpha("alphanumeric"));
//System.out.println(stripVowels("missasseppssa"));
//System.out.println(stripConsonants("missasseppssa"));
//System.out.println(replace("mississippi",'i','o'));
//System.out.println(countChar("mississippi", 's'));
//System.out.println(multiString("blargle"));
//System.out.println(lowerCase("I'm Not Shouting"));
//System.out.println(upperCase("I'm Not Shouting!"));
//System.out.println(findInStr("find the missing dissing listing", "sing"));
//System.out.println(findInStr("find the missing dissing listing", 'n'));
//System.out.println(findInStrLast("find the missing dissing listing", "sing"));
//System.out.println(findInStrLast("find the missing dissing listing", 'n'));
//System.out.println(inOrder(42, 24, 86));
}
// Private helper method that strips out spaces and non-characters
// from a String
private static String charactersOnly(String str) {
return "abcdefg";
}
// Private helper method that checks to see if a character is a digit
private static boolean isDigit(char c) {
return true;
}
// Private helper method that checks to see if a character is a vowell
private static boolean isVowell(char c) {
return true;
}
// Private helper method that checks to see if a character is a
// consonant
private static boolean isConsonant(char c) {
return true;
}
// Private help method that checks if two strings are equal
private static boolean equals(String str1, String str2) {
return true;
}
/**
* isPalindrome accepts a string and returns true if the string is a
* palindrome, otherwise returns false; should ignore spaces, punctuation
* and case (upper and lower are the same). So, the following ARE
* palindromes: "Never odd or even., Cigar? Toss it in a can. It is so
* tragic., A man, a plan, a canal: Panama.
*/
public static boolean isPalindrome(String str) {
return true;
}
/**
* reverse accepts a string and returns a string that is the reverse of the
* original
*
* @param str
* @return reversed representation of str
*/
public static String reverse(String str) {
return "reverse";
}
/**
* jumble accepts a string and returns a jumbled version of the original:
* for this method, jumbled means that two randomly chosen characters other
* than the first and last characters of the string are swapped; this method
* must use the class, Random (see chapter 5). Examples of what this method
* might do: fist returns fsit, much returns mcuh
*
* @param str string to be jumbled
*/
public static String jumble(String str) {
return "jumble";
}
/**
* swapPairs accepts a string and returns a string that has adjacent pairs
* of the original string swapped; for strings of odd length, leave the last
* character unchanged. Examples: IN: examples OUT: xemalpse IN: count OUT:
* ocnut
*/
public static String swapPairs(String str) {
return "reverse";
}
/**
* countVowels accepts a string of any characters and returns the number of
* vowels in the string. (aeiou)
*
* @param str
* @return integer count of vowels in str
*/
public static int countVowels(String str) {
return -1;
}
/**
* countConsonants accepts a string of any characters and returns the number
* of consonants in the string
*
* @param str
* @return integer count of consonants in str
*/
public static int countConsonants(String str) {
return -1;
}
/**
* allDigits accepts a string and returns true if every character is a
* digit, otherwise false
*
* @param str
* @return true of all digits, false otherwise
*/
public static boolean allDigits(String str) {
return true;
}
/**
* allAlpha accepts a string and returns true if every character is a letter
* (either case), otherwise false
*
* @param str
* @return true if all alpha, false otherwise
*/
public static boolean allAlpha(String str) {
return true;
}
/**
* stripVowels accepts a string and returns the string with no vowels.
* Example: IN: s-cat=tered OUT: s-ct=trd
*
* @param str
* @return str with no vowels to be found
*/
public static String stripVowels(String str) {
return "rvrs";
}
/**
* stripConsonants accepts a string and returns the string with no
* consonants. Example: IN: s_cat$tered OUT: _a$ee
*/
public static String stripConsonants(String str) {
return "stripConsonants";
}
/**
* replace accepts a string and two characters, returns the string with all
* occurrences of char1 replaced by char2. Example: IN: mississippi, i, o
* OUT: mossossoppo
*
* @param str
* @param c1
* @param c2
* @return str with all instance of c1 replaced with c2
*/
public static String replace(String str, char c1, char c2) {
return "replace";
}
/**
* countChar accepts a string and a character, returns the number of
* occurences of the character in the string. Example: IN: m.is_si-ssi=pp!i,
* i OUT: 4
*
* @param str
* @param c
* @return integer count of c in str
*/
public static int countChar(String str, char c) {
return -1;
}
/**
* multiString accepts a string and returns a string with repeated
* characters: the first character is repeated string-length times, the
* second character is repeated one less time, until the last character is
* included only once (not repeated). Example: IN: test OUT: tttteeesst IN:
* sentence OUT: sssssssseeeeeeennnnnnttttteeeennncce
*
* @param str
* @return revised string
*/
public static String multiString(String str) {
return "multiString";
}
/**
* lowerCase accepts a string and returns the string in all lowercase; do
* not use the String classs toLowerCase method, do not use the Character
* class method either.
*
* @param str
* @return str in all lower case
*/
public static String lowerCase(String str) {
return "lowerCase";
}
/**
* upperCase accepts a string and returns the string in all uppercase; do
* not use the String classs toLowerCase method, do not use the Character
* class method either.
*
* @param str
* @return str in all upper case
*/
public static String upperCase(String str) {
return "UPPERCASE";
}
/**
* findInStr accepts two strings and returns the character index of the
* position where the second string begins in the first; returns -1 if the
* second string does not appear in the first. Examples: IN: mississippi ss
* OUT: 2 IN: superlative zzl OUT: -1
*
* @param s1
* @param s2
* @return index of s2 in s1
*/
public static int findInStr(String s1, String s2) {
return -1;
}
/**
* findInStr is an overloaded version of previous method, accepts a string
* and a character, returns the index of the position where the character
* first appears in the string; returns -1 if the character does not appear
* in the string. Examples: IN: dazzling z OUT: 2 IN: superlative z OUT: -1
*
* @param s1
* @param c
* @return
*/
public static int findInStr(String s1, char c) {
return -1;
}
/**
* findInStrLast accepts two strings and returns the character index of the
* rightmost position where the second string begins in the first; returns
* -1 if the second string does not appear in the first. Examples: IN:
* mississippi ss OUT: 5 IN: superlative zzl OUT: -1
*
* @param s1
* @param s2
* @return
*/
public static int findInStrLast(String s1, String s2) {
return -1;
}
/**
* findInStrLast is an overloaded version of previous method, accepts a
* string and a character, returns the index of the rightmost position where
* the character first appears in the string; returns -1 if the character
* does not appear in the string. Examples: IN: dazzling z OUT: 3 IN:
* superlative z OUT: -1
*
* @param s1
* @param c
* @return revised String
*/
public static int findInStrLast(String s1, char c) {
return -1;
}
/**
* inOrder accepts 3 integers and returns a string containing the three
* integers in ascending order separated by a single space between the first
* two and another single space between the second and third. Examples: 1,
* 2, 3 returns 1 2 3; 199, 8, 23 returns 8 23 199 .
*
* @param a
* @param b
* @param c
* @return String representation of numbers in order
*/
public static String inOrder(int a, int b, int c) {
return "inOrder";
}
}
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
