Fill in the missing static methods in the following code. You\'re welcome to cop
ID: 3595721 • Letter: F
Question
Fill in the missing static methods in the following code. You're welcome to copy and paste what's here. main( ) is worth 13 points. Each other method is worth 4 points.
It's highly recommended that you use methods that you've written already to help program other methods. For example, it's a very good idea to use your isVowel() in the more complicated methods involving vowels. Similarly, you might consider using your reversed() method in your sameInReverse() method.
The only place where you should print or read input from a Scanner is in main(). Do not print or read input from a Scanner in any other method.
Explanation / Answer
import java.util.Scanner;
public class StringMethods {
public static boolean isVowel(char c) {
return c == 'a' || c == 'e' || c =='i' || c == 'o' || c =='u';
}
public static int indexOfFirstVowel(String s) {
for (int i = 0; i < s.length(); i++) {
if (isVowel(s.charAt(i))) {
return i;
}
}
return -1;
}
public static int indexOfFirstVowel(String s, int startPosition) {
for (int i = startPosition; i < s.length(); i++) {
if (isVowel(s.charAt(i))) {
return i;
}
}
return -1;
}
public static int indexOfLastVowel(String s) {
for (int i = s.length() -1 ; i >= 0; i--) {
if (isVowel(s.charAt(i))) {
return i;
}
}
return -1;
}
public static boolean sameFirstLastMiddle(String s) {
if (s.length() < 3) return false;
int mid = s.length()/2;
return Character.toLowerCase(s.charAt(s.length()-1)) == Character.toLowerCase(s.charAt(0)) &&
Character.toLowerCase(s.charAt(0)) == Character.toLowerCase(s.charAt(mid));
}
/* returns s in reverse. For example, if s is "Apple", the method
* returns the String "elppA" */
public static String reversed(String s) {
StringBuilder sb = new StringBuilder();
for(int i = s.length()-1; i>= 0; i--) {
sb.append(s.charAt(i));
}
return sb.toString();
}
/* returns the number of times that n occurs
* in h. For example, if h is "Mississippi" and n is "ss"
* the method returns 2. */
public static int numOccurrences(String h, String n) {
int count = 0;
for (int i = 0; i < h.length(); i++) {
if (h.substring(i).contains(n)) {
count++;
}
}
return count;
}
/* returns true if s is the same backwards and forwards
* and false otherwise */
public static boolean sameInReverse(String s) {
return s.equals(reversed(s));
}
/* returns a new String which is the same as s, but with
* all of the vowels removed. For example, if s is "summer vacation"
* the method returns "smmr vctn" */
public static String devoweled(String s) {
StringBuilder sb = new StringBuilder();
for(int i = 0; i < s.length(); i++) {
if (!isVowel(s.charAt(i))) {
sb.append(s.charAt(i));
}
}
return sb.toString();
}
/* Returns a new string consisting of all of the characters of s1
* and s2 interleaved with each other. For example, if s1 is
* "Spongebob" and s2 is "Patrick", the
* function returns the string "SPpaotnrgiecbkob" */
public static String zipped(String s1, String s2) {
int len1 = s1.length();
int min = len1;
int len2 = s2.length();
if (len2 < len1) {
min = len2;
}
StringBuilder sb = new StringBuilder();
for (int i = 0; i < min; i++) {
sb.append(s1.charAt(i)).append(s2.charAt(i));
}
return sb.toString();
}
/* returns a new String consisting of all of the letters
* of s, but where tab characters (' ') are replaced
* with n spaces */
public static String tabToSpace(String s, int n) {
String space = "";
for(int i = 0; i < n; i++) space += " ";
StringBuilder sb = new StringBuilder();
for (int i = 0; i < s.length(); i++) {
if (s.charAt(i) == ' ') {
sb.append(space);
}
else {
sb.append(s.charAt(i));
}
}
return sb.toString();
}
/* returns true if all of the characters in chars are
* found in the String s, or false otherwise */
public static boolean containsAll(String s, String chars) {
int count = 0;
for (int i = 0; i < chars.length(); i++) {
for (int j = 0; j < s.length(); j++) {
if (s.charAt(j) == chars.charAt(i)) {
count++;
break;
}
}
}
return count == chars.length();
}
/* returns the index of the first occurrence of any of the
* characters in chars in String s or -1 if none of the characters
* in chars are found in s. */
public static int indexOfAny(String s, String chars) {
int count = 0;
for (int i = 0; i < chars.length(); i++) {
for (int j = 0; j < s.length(); j++) {
if (s.charAt(j) == chars.charAt(i)) {
return j;
}
}
}
return -1;
}
public static void main(String args[]) {
/* insert code to test each method */
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.