This homework will focus on String manipulation, interfaces and recursion. We wi
ID: 3797577 • Letter: T
Question
This homework will focus on String manipulation, interfaces and recursion. We will be creating a StringHelpers class that allows us to perform interesting operations on String objects. Remember, the String class is immutable!
Interface
Write an interface called StringOperations with the following methods:
Takes a String input and returns whether the String contains a vowel or not:
"hello world!" would return true
"twyndyllyngs" would return false
Takes a String input and returns whether the String contains a consonant or not:
"hello world!" would return true
"aeaea" would return false
Prints all words in the String, one per line on the console, that have over five characters
"the lazy do pranced and danced all over the yard" would print out:
pranced
danced
String searchString,
Prints the number of times searchCharacter shows up searchString:
If searchString is "President Obama is interested in net neutrality" and searchCharacter is "e" then printNumberOfCharacters would return 7.
If searchString is "the world is flat" and searchCharacter is "u" then printNumberOfCharacters would return 0.
Takes a String input and returns whether the String is a palindrome. A palindrome is word that is the same when spelled backwards (ignoring spaces and punctuation). For example:
"civic", "radar", "deleveled", "borrow or rob", "a man, a plan, a canal, panama" would all return true.
"pizza" would return false.
StringHelpers Class
Write a class called StringHelpers that implements the StringOperations interface above.
containsVowel: you can use the indexOf() or contains() method to identify vowels
containsConsonant: you can use the indexOf() or contains() method to identify consonants
Note: this can be completed using only one line of code (take a moment and consider this)
printLengthyWords: use the split() method to identify words and use the length() method to determine word length
Note: first focus on retrieving the words, then worry about word length
printNumberOfCharacters: this method should be solved using recursion
You will need to use the indexOf() method to determine where the first searchCharacter is at in your searchString
You will need to keep track of a counter that counts the number of characters seen so far. You will then return this counter variable with each recursive call.
Your base case should be when the character is no longer found in searchString.
findPalindrome: this method sould be solved using recursion
To be a palindrome, the first and last character of a string must be equal: "deleveled"
Your recursive method should first check that these characters are equal and then pass the remaining string back to the method recursively "elevele" would be checked next, then "level", then "eve"...
Your base case will be when you get a string with one character "v" or no characters (a string with an even number of characters
Driver Class
Write a driver class that creates a StringHelpers object and lets the user interact with methods above on the console. You should ask the user for which method they wish to call, then give the user a chance to enter an parameters the method requires. Print the results of the method call if the method has a return type.
Name Return type Parameters Description containsVowel boolean String searchStringTakes a String input and returns whether the String contains a vowel or not:
"hello world!" would return true
"twyndyllyngs" would return false
Takes a String input and returns whether the String contains a consonant or not:
"hello world!" would return true
"aeaea" would return false
Prints all words in the String, one per line on the console, that have over five characters
"the lazy do pranced and danced all over the yard" would print out:
pranced
danced
String searchString,
char searchCharacterPrints the number of times searchCharacter shows up searchString:
If searchString is "President Obama is interested in net neutrality" and searchCharacter is "e" then printNumberOfCharacters would return 7.
If searchString is "the world is flat" and searchCharacter is "u" then printNumberOfCharacters would return 0.
findPalindrome boolean String searchStringTakes a String input and returns whether the String is a palindrome. A palindrome is word that is the same when spelled backwards (ignoring spaces and punctuation). For example:
"civic", "radar", "deleveled", "borrow or rob", "a man, a plan, a canal, panama" would all return true.
"pizza" would return false.
Explanation / Answer
import java.util.Scanner;
public class StringFinder {
public boolean containsVowel(String word) {
for (int i = 0; i < word.length(); i++) {
char c = word.charAt(i);
if (c == 'a' || c == 'A' || c == 'i' || c == 'I' || c == 'o'
|| c == 'O' || c == 'u' || c == 'U' || c == 'e' || c == 'E')
return true;
}
return false;
}
public boolean containsConsonant(String word) {
int count = 0;
int consonantCount = 0;
for (int i = 0; i < word.length(); i++) {
char c = word.charAt(i);
if (c == 'a' || c == 'A' || c == 'i' || c == 'I' || c == 'o'
|| c == 'O' || c == 'u' || c == 'U' || c == 'e' || c == 'E')
count++;
else
consonantCount++;
}
if (consonantCount == 0) {
return false;
} else
return true;
}
public String[] overFiveWords(String word) {
String[] lengthfive = new String[25];
int count = 0;
String[] words = word.split("\s+");
for (int i = 0; i < words.length; i++) {
if (words[i].length() > 5) {
lengthfive[count] = words[i];
++count;
}
}
return lengthfive;
}
public int noOfCharacters(String word, char searchCharacter) {
int occurances = 0;
for (int i = 0; i < word.length(); i++) {
if (searchCharacter == word.charAt(i)) {
++occurances;
}
}
return occurances;
}
public boolean pallindrome(String word) {
String string = word.toLowerCase().replaceAll("\W", "");
int i = 0;
int j = string.length() - 1;
while (i < string.length() / 2) { // loops until half the length of the
// string if
// even and floor value if odd.
if (string.charAt(i++) != string.charAt(j--)) {// check for first
// and last chars
// and go inwards. if char do not match print 'Not a Palindrome'
// and exit
return false;
}
}
return true;
}
public static void main(String[] args) {
StringFinder finder = new StringFinder();
Scanner scanner = new Scanner(System.in);
System.out
.println("enter the string to test it contains vowels or not");
String word = scanner.nextLine();
boolean flag = finder.containsVowel(word.trim());
System.out.println("the given string contains vowels " + flag);
System.out
.println("enter the string to test it contains consonants or not");
String word1 = scanner.nextLine();
boolean flag1 = finder.containsConsonant(word1.trim());
System.out.println("the given string contains consonants " + flag1);
System.out
.println("enter the string to retrieve the words over length five ");
String sentence = scanner.nextLine();
String[] words = finder.overFiveWords(sentence);
if (words.length == 0)
System.out
.println("there are no words contains over five characters in the given string");
else
for (String overlenghtfive : words) {
if (overlenghtfive != null)
System.out.println(overlenghtfive);
}
System.out
.println("enter the string to find specific character occurances");
String sentence1 = scanner.nextLine();
int noOfOccurances = finder.noOfCharacters(sentence1.trim(), 'e');
if (noOfOccurances != 0)
System.out.println("the no of occurances are" + noOfOccurances);
else
System.out.println("no charcter you specified in the string");
scanner.close();
System.out.println("enter the string to find pallindrome or not");
String palcheckerWord = scanner.next();
boolean flag2 = finder.pallindrome(palcheckerWord);
System.out.println(flag2);
}
}
output
enter the string to test it contains vowels or not
marksmith
the given string contains vowels true
enter the string to test it contains consonants or not
marksmith
the given string contains consonants true
enter the string to retrieve the words over length five
developed construction cook book
developed
construction
enter the string to find specific character occurances
obama is a great man
the no of occurances are1
enter the string to find pallindrome or not
madam
true
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.