Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Overview This homework will focus on String manipulation, interfaces and recursi

ID: 3796374 • Letter: O

Question

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

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 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.

Explanation / Answer


interface StringOperations{
   public boolean containsVowel(String string);
   public boolean containsConsonant(String string);
   public void printLengthyWords(String string);
   public int printNumberOfCharacters(String string,char chr);
   public boolean findPalindrome(String string);
}

class StringHelpers implements StringOperations{

   @Override
   public boolean containsVowel(String string) {
       if(string.contains("a")||string.contains("e")||string.contains("i")||string.contains("o")||string.contains("u")||string.contains("A")||string.contains("E")||string.contains("I")||string.contains("O")||string.contains("U"))
           return true;
       else
           return false;
   }

   @Override
   public boolean containsConsonant(String string) {
       int i=0;
       while(i<string.length()){
           if(string.charAt(i)!='a' && string.charAt(i)!='e' && string.charAt(i)!='i' && string.charAt(i)!='o' && string.charAt(i)!='u' && string.charAt(i)!='A' &&string.charAt(i)!='E' && string.charAt(i)!='I'&&string.charAt(i)!='O' && string.charAt(i)!='U')
               return true;
           i++;
       }
       return false;
   }

   @Override
   public void printLengthyWords(String string) {
       String[] words = string.split(" ");
       for(int i=0; i<words.length; i++){
           if(words[i].length()>5){
               System.out.println(words[i]);
           }
       }
   }

   @Override
   public int printNumberOfCharacters(String string, char chr) {
       int i = 0;
       int count = 0;
       while(i<string.length()){
           if(string.charAt(i)==chr)
               count++;
           i++;
       }
       return count;
   }

   @Override
   public boolean findPalindrome(String string) {
       int i=0;
       int j=string.length()-1;
       while(i<j){
           if(string.charAt(i)!=string.charAt(j))
               return false;
           i++;
           j--;
       }
       return true;
   }
  
}
public class StringOperationsDrive {

   public static void main(String[] args) {
       StringOperations strOps = new StringHelpers();
       System.out.println(""loknath" containsVowel :"+strOps.containsVowel("loknath"));
       System.out.println(""kjlrt" containsVowel :"+strOps.containsVowel("kjlrt"));
       System.out.println(""loknath" containsConsonant :"+strOps.containsConsonant("loknath"));
       System.out.println(""aeaeo" containsConsonant :"+strOps.containsConsonant("aeaeo"));
       System.out.println(""I solved this question completly" printLengthyWords :");
       strOps.printLengthyWords("I solved this question completly");
       System.out.println(""I solved this question completly" printNumberOfCharacters :"+strOps.printNumberOfCharacters("I solved this question completly",'e'));
       System.out.println(""abradadarba" findPalindrome :"+strOps.findPalindrome("abradadarba"));
      
   }

}