Question

In: Computer Science

Write the class RecursiveProbs, with the methods listed below. Write all the methods using recursion, not...

Write the class RecursiveProbs, with the methods listed below. Write all the methods using recursion, not loops. You may use JDK String methods like substring() and length(), but do not use the JDK methods to avoid coding the algorithms assigned. For example, don't use String.reverse().
public boolean recursiveContains(char c, String s) returns true if the String contains the char, otherwise returns false.
Here is the code for this method, which you should use as an example to see how to write the remaining methods:
public boolean recursiveContains(char c, String s){
if(s.length() == 0) return false;
if(s.charAt(s.length()-1)==c) return true;
else return recursiveContains(c, s.substring(0, s.length() -1));
}
public boolean recursiveAllCharactersSame(String s) returns true if all the characters in the String are identical, otherwise false. If the String has length less than 2, all the characters are identical.
public String recursiveHead(int n, String s) returns the substring of s beginning with the first character and ending with the character at n - 1; in other words, it returns the first n characters of the String. Return empty String ("") in cases in which n is zero or negative or exceeds the length of s.

Test your work using a main() method .(language is java)

Solutions

Expert Solution

:: Solution ::

CODE

public class Main {

public static boolean recursiveContains(char c, String s) {

if (s.length() == 0)

return false;

if (s.charAt(s.length() - 1) == c)

return true;

else

return recursiveContains(c, s.substring(0, s.length() - 1));

}

public static boolean recursiveAllCharactersSame(String s) {

if (s == null || s.length() < 2) {

return true;

}

return s.charAt(0) == s.charAt(1) && recursiveAllCharactersSame(s.substring(1));

}

public static String recursiveHead(int n, String s) {

if (n <= 0 || n > s.length()) {

return "";

}

return recursiveHead(n-1, s) + s.charAt(n-1);

}

public static void main(String[] args) {

System.out.println(recursiveContains('h', "hello world"));

System.out.println(recursiveContains('a', "hello world"));

System.out.println(recursiveAllCharactersSame("aaaaa"));

System.out.println(recursiveAllCharactersSame("aaaab"));

System.out.println(recursiveHead(8, "hello world"));

}

}

Please rate my answer. Thank you...


Related Solutions

For each of the gene delivery methods listed below, write Yes or No in the spaces...
For each of the gene delivery methods listed below, write Yes or No in the spaces provided according to whether they have or do not have the features listed. Question 1 of 31 What follows is a fill in the blank question with 12 blanks. Can the delivery method be immunogenic? Retrovirus:  Blank 1. Fill in the blank, read surrounding text. Adenovirus:  Blank 2. Fill in the blank, read surrounding text. Liposome:  Blank 3. Fill in the blank, read surrounding text. Is the...
Write a program in python that implements quicksort, first using recursion and then without recursion.
Write a program in python that implements quicksort, first using recursion and then without recursion.
Guidelines for the program: All methods listed below must be public and static. If your code...
Guidelines for the program: All methods listed below must be public and static. If your code is using a loop to modify or create a string, you need to use the StringBuilder class from the API. Keep the loops simple but also efficient. Remember that you want each loop to do only one "task" while also avoiding unnecessary traversals of the data. No additional methods are needed. However, you may write additional private helper methods, but you still need to...
Coding Java Assignment Write the following static methods. Assume they are all in the same class....
Coding Java Assignment Write the following static methods. Assume they are all in the same class. Assume the reference variable input for the Scanner class and any class-level variables mentioned are already declared. All other variables will have to be declared as local unless they are parameter variables. Use printf. A method that prompts for the customer’s name and returns it from the keyboard. A method called shippingInvoice() that prompts for an invoice number and stores it in a class...
please use java swing and recursion and the suggested method hints listed Objective: Write a program...
please use java swing and recursion and the suggested method hints listed Objective: Write a program in which draws (yes it actually makes a picture) a triangular fractal using recursion. This is best if done using a java applet. Suggested Methodology The idea for it is this First draw a filled equilateral triangle Next draw another filled equilateral triangle of a different color that’s upside down in the middle of that triangle Using the other triangles formed repeat step 2...
Using Power Series methods, find the recursion formula and a 6th degree polynomial approximation to the...
Using Power Series methods, find the recursion formula and a 6th degree polynomial approximation to the solution of the Initial Value Problem: (x + 1) y ‘’ – x2 y ‘ + 3 y = 0; y(0) = 1; y ‘(0) = 0. What is the radius of convergence of the series?
JAVA: USE SWITCH METHOD Write a Temperature class using the Demo below. The class will have...
JAVA: USE SWITCH METHOD Write a Temperature class using the Demo below. The class will have three conversion methods: toCelcius(), toKelvin and toFahrenheit(). These methods will return a Temperature in those three scales equal to this temperature. Note that the value of this is not changed int these coversions. In addition to these conversion methods the class will have add(Temperature), subtract(Temperature), multiply(Temperature) and divide(Temperature). These four methods all return a temperature equalled to the respective operation. Note that the this...
1. Using the internet, explain the improvement methods used by some companies listed below. How do...
1. Using the internet, explain the improvement methods used by some companies listed below. How do they differ from the five and seven step methods in the text? • 8D Improvement method • TRIZ • Any other method used by organizations: Findings:
How to rewrite the bin() function below using recursion instead of a for loop in C?...
How to rewrite the bin() function below using recursion instead of a for loop in C? #include <stdio.h> #include <stdlib.h> #include <math.h> #include <stdint.h> char* bin(int x, int i); int main(int argc, char **argv) { char* binstr = bin(10, 0); printf("%s\n", binstr); free(binstr); return 0; } char* bin(int x, int i){ int bits = log10((double) x)/log10(2)+1; char* ret = malloc((bits+1)*sizeof(char)); for(int i = 0; i < bits; i++){ ret[bits-i-1] = (x & 1) ? '1' : '0'; x >>= 1;...
1. Write ALL the types of intermolecular attractions that can occur between the molecules listed below....
1. Write ALL the types of intermolecular attractions that can occur between the molecules listed below. You should assume that there are “many” molecules of each type present in solution. Use molecular dipole moments to justify your answers. a) ammonia, NH3 b) carbon monoxide, CO c) carbon dioxide, CO2 d) phosgene, COCl2 H-bond donor “donates” an H atom to the other H2O molecule H-bond acceptor “accepts” an H atom to the other H2O molecule O H H H O H...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT