Overview This homework will focus on String manipulation int

Overview

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.

Name Return type Parameters Description
containsVowel boolean String searchString

Takes a String input and returns whether the String contains a vowel or not:

\"hello world!\" would return true
\"twyndyllyngs\" would return false

containsConsonant boolean String searchString

Takes a String input and returns whether the String contains a consonant or not:

\"hello world!\" would return true
\"aeaea\" would return false

printLengthyWords void String searchString

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

printNumberOfCharacters int

String searchString,

char searchCharacter

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.

findPalindrome boolean String searchString

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.

Solution


public class StringHelpers implements StringOperations{

   public static void main(String[] args) {
       // TODO Auto-generated method stub
StringHelpers so=new StringHelpers();//create object of class
System.out.println(so.containsVowel(\"aeaea\"));//check if string has vowels
System.out.println(so.containsConsonant(\"ae\"));//check if string has consonanats
so.printLengthyWords(\"the lazy do pranced and danced all over the yard\");
System.out.println(so.printNumberOfCharacters(\"President Obama is interested in net neutrality\",\'e\'));
System.out.println(so.findPalindrome(\"radar\"));//check string is Palindrome or not

   }

   @Override
   public boolean containsVowel(String s) {
       // TODO Auto-generated method stub
       return s.contains(\"a\")||s.contains(\"e\")||s.contains(\"i\")||s.contains(\"o\")||s.contains(\"u\");
   }

   @Override
   public boolean containsConsonant(String s) {
       // TODO Auto-generated method stub

   for(int i=0;i<s.length();i++)
   {
       if(s.charAt(i)!=\'a\'&& s.charAt(i)!=\'e\'&&s.charAt(i)!=\'i\'&&s.charAt(i)!=\'o\'&&s.charAt(i)!=\'u\')
       {
           return true;
             
       }
   }
   return false;
   }

   @Override
   public void printLengthyWords(String s) {
       // TODO Auto-generated method stub
       String[] result = s.split(\"\\\\s\");
       for (int x=0; x<result.length; x++) {
           if(result[x].length()>=5)
           System.out.println(result[x]);
           }
   }

   @Override
   public int printNumberOfCharacters(String s, char c) {
       // TODO Auto-generated method stub
       int count=0;
       for(int i=0;i<s.length();i++)
       {
           if(s.charAt(i)==c)
           {
               count++;
           }
       }
       return count;
   }

   @Override
   public boolean findPalindrome(String s) {
       if (s.length() < 2) return true;

// Otherwise a string is a palindrome is first character matches the last
// one
if (s.charAt(0) == s.charAt(s.length() - 1)) {
// And the rest of the string is also a palindrome
return findPalindrome(s.substring(1, s.length() - 1));
}
else {
// Otherwise it is not a palindrome
return false;

   }
   }

}

=====================================================================


public interface StringOperations {
   boolean containsVowel(String s);
   boolean containsConsonant(String s);
   void printLengthyWords(String s);
   int printNumberOfCharacters(String s,char c);
   boolean findPalindrome(String s);

}

===================================================================

Output:

true
false
pranced
danced
7
true

============================================================

Let me know if you have any problem

Overview This homework will focus on String manipulation, interfaces and recursion. We will be creating a StringHelpers class that allows us to perform interest
Overview This homework will focus on String manipulation, interfaces and recursion. We will be creating a StringHelpers class that allows us to perform interest
Overview This homework will focus on String manipulation, interfaces and recursion. We will be creating a StringHelpers class that allows us to perform interest
Overview This homework will focus on String manipulation, interfaces and recursion. We will be creating a StringHelpers class that allows us to perform interest

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site