Question: 1. Please help me to pass the codecheck. https://codecheck.it/files/18101204157aj2ljmjdkn13zsenee50ht5e /** * Writing methods using loops. You may use * any kind of loop you like.
1. Please help me to pass the codecheck. https://codecheck.it/files/18101204157aj2ljmjdkn13zsenee50ht5e
/** * Writing methods using loops. You may use * any kind of loop you like. * * @author * @version */ public class PS11B { /** * Write the method named withoutString(). * * Given two strings, base and remove, return a * version of the base string where all instances * of the remove string have been removed (not case * sensitive). You may assume that the remove * string is length 1 or more. Remove only non-overlapping * instances, so with "xxx" removing "xx" leaves "x". * * Note: Despite the term "remove" you should actually not * try to remove the characters from a string. You'll continue * to use the "building a string" pattern from lecture. * * Examples: * withoutString("Hello there", "llo") returns "He there" * withoutString("Hello there", "e") returns "Hllo thr" * withoutString("Hello there", "x") returns "Hello there" * * @param base the base String to modify. * @param remove the substring to remove. * @return the base String with all copies of remove removed. */ // TODO - Write the withoutString method here
/** * Write the method named sameEnds(). * * Given a string, return the longest substring * that appears at both the beginning and end of * the string without overlapping. For example, * sameEnds("abXab") should return "ab". * * Examples: * sameEnds("abXYab") returns "ab" * sameEnds("xx") returns "x" * sameEnds("xxx") returns "x" * * @param str the String to search. * @return the longest substring that appears at beginning and end. */ // TODO - Write the sameEnds method here.
/** * Write the method named maxBlock(). * * Given a string, return the length of the * largest "block" in the string. A block is a run * of adjacent characters that are the same. * * Examples: * maxBlock("hoopla") returns 2 * maxBlock("abbCCCddBBBxx") returns 3 * maxBlock("") returns 0 * * @param str the String to examine. * @return the size of the longest run of adjacent characters. */ // TODO - Write the maxBlock method here.
}
2. Also, please help me to pass the code using while loop, not for loops
to pass the codecheck: https://codecheck.it/files/1810120451vw96f6uzht51a2y0fwbhe6a3
Here is my code
public class PS11A {
/**
* Write the method xyzMiddle().
*
* Given a String str, does "xyz" appear in the "middle" of the string. To
* define middle, we'll say that the number characters to the left and right of
* the "xyz" must differ by, at most, one.
*
* Examples: xyzMiddle("AAxyzBB") returns true xyzMiddle("AxyzBB") returns true
* xyzMiddle("AxyzBBB") returns false
*
* @param str the String to examine.
* @return true if xyz is in the "middle" of str.
*/
// TODO - Write the method xyZMiddle here.
public static boolean xyzMiddle(String str){
str = str.toLowerCase();
int pos = str.indexOf("xyz");
int len = str.length();
if(Math.abs(pos-(len-pos-3))<=1)
return true;
return false;
}
/**
* Write the method named repeatSeparator().
*
* Given two String inputs, word and separator, along with a third int input
* count, return a big String consisting of count copies of word, each separated
* by separator.
*
* Note: This is a very common algorithm, called the fencepost algorithm,
* because just like building a fence, you need 11 fenceposts to hold up 10
* sections of fence.
*
* Examples: repeatSeparator("Word", "X", 3) returns "WordXWordXWord"
* repeatSeparator("This", "And", 2) returns "ThisAndThis"
* repeatSeparator("This", "And", 1) returns "This"
*
* @param word the first String parameter.
* @param separator the second String parameter.
* @param count the number of times to repeat word.
* @return a new String as described here.
*/
// TODO - Write the repeatSeparator method here.
public static String repeatSeparator(String str, String repeatStr, int n){
String n_str = "";
for(int i=0; i n_str += str; if(i!=n-1) n_str+= repeatStr; } return n_str; } /** * Write the method named sameStarChar(). * * Given a String str, return true if for every '*' (star) in the string, if * there are chars both immediately before and after the star, they are the * same. * * Note: This is a little tricker than it looks. One way to look at the problem * is to see in what circumstances you should return false. You only return * false if the characters on either side of the * are different. That means * that if there are no characters in front of, or behind the *, then you should * return true, not false. * * Examples: sameStarChar("xy*yzz") returns true sameStarChar("xy*zzz") returns * false sameStarChar("*xa*az") returns true * * @param str the input String to process. * @return true if the characters before and after are the same. */ // TODO - Write the sameStarChar method here. public static boolean sameStarChar(String str){ int len = str.length(); for(int i=0; i if(str.charAt(i)=='*'){ if(i>0 && i if(str.charAt(i-1)!=str.charAt(i+1)) return false; } } } return true; } public static void main(String[] args) { System.out.println(xyzMiddle("AAxyzBB")); System.out.println(xyzMiddle("AxyzBB")); System.out.println(xyzMiddle("AxyzBBB")); System.out.println(repeatSeparator("Word", "X", 3)); System.out.println(repeatSeparator("This", "And", 2)); System.out.println(repeatSeparator("This", "And", 1)); System.out.println(sameStarChar("xy*yzz")); System.out.println(sameStarChar("xy*zzz")); System.out.println(sameStarChar("*xa*az")); } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
