/**
* StringRecursion.java
*
* starter code by: Computer Science S-111
*
* modified by:
* email:
*
* A class that contains recursive methods that operate onstrings.
*/
public class StringRecursion {
/*
* You may want to add test code for your methods to this
* main method, although doing so is not required.
* See the section of the assignment entitled
* "Testing your methods".
*
* IMPORTANT: If your method does not return anything
* (i.e., it is a void method), you should NOT try
* to call it from within a println command. Instead,
* you should call it on its own line -- for example:
* printSeries(3, 8);
*/
public static void main(String[] args) {
System.out.println("test 1 gives: " + numOccur('s',"Mississippi"));
System.out.println("test 2 gives: " + numOccur('e',"Mississippi"));
}
/*
* numOccur - a recursive method that returns the number oftimes
* that the character ch occurs in the String str.
*
* The main method includes two examples of using thismethod.
*
* You can also test this method by entering
* StringRecursion.numOccur(ch, str) -- where ch is replaced
* by a char and str is replaced by a string -- in the
* Interactions Pane.
*/
public static int numOccur(char ch, String str) {
// base case
if (str == null || str.equals("")) {
return 0;
}
// recursive case
int numOccurInRest = numOccur(ch, str.substring(1));
if (ch == str.charAt(0)) {
return 1 + numOccurInRest;
} else {
return numOccurInRest;
}
}
/*
* ****** ADD YOUR METHODS BELOW **********
*
* Make sure to use the exact method headers
* given in the assignment.
*/
}
---------------------------------------------------
the above is the StringRecursion.java that you shoulduse for the problem.
Rules:
Requirements:
The methodsAdd to StringRecursion your implementations of thefollowing methods, using the headers that are specified:
public static void printEveryOther(String str)This method must userecursion to print every other character in thestring str. For example, a callof printEveryOther("method") should produce the followingoutput:
The method should not return a value.
Special cases: If the value null or theempty string ("") are passed in as the parameter, the method shouldjust print a newline (by executing thestatement System.out.println();).
Hints:
public static void printTriangle(String str)This method must userecursion to print a “triangle” that consists ofincreasingly longer substrings of the string str. For example,a call of printTriangle("hello") should produce thefollowing output:
The method should not return a value.
Special cases: If the value null or theempty string ("") are passed in as the parameter, the method shouldjust return without printing anything.
Hint: When constructing the parameter for therecursive call, you may find it helpful to take a slightlydifferent approach than the one that we have typically used whenprocessing strings recursively.
public static boolean contains(String str, char ch)This method must userecursion to determine if the string specifiedby the parameter str contains at least one occurrence ofthe character specified by the parameter ch,returning true if it does and false otherwise.For example:
This method should not do any printing; itshould simply return the appropriate boolean value. In addition,this method may not callthe numOccur method that we have given you.
Special cases: If the value null or theempty string ("") are passed in for the first parameter, the methodshould return false.
public static int lastIndexOf(char ch, String str)This method must userecursion to find and return the index of thelast occurrence of the character ch in thestring str, or -1 if ch does not occurin str. For example:
This method should not do any printing; itshould simply return the appropriate integer.
Special cases: If the value null or theempty string ("") are passed in for the second parameter, themethod should return -1.
/** * StringRecursion.java * * starter code by: Computer Science S-111 * * modified by: * email: * * A class that contai
-
- Site Admin
- Posts: 899603
- Joined: Mon Aug 02, 2021 8:13 am