Question

In: Computer Science

Using Java Write the class RecursiveProbs, with the methods listed below. Write all the methods using...

Using Java

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 algorithms assigned. For example, don’t use String.revers().

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) return true if all the characters in the String are identical, otherwise false. if the String has length less than 2, all 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.

Write either a main() or a JUnit test case for test

Solutions

Expert 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"));

}

}


Related Solutions

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...
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...
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...
Define empty methods in Queue class using LinkedList class in Java ------------------------------------------------------------------------------- //Queue class public class...
Define empty methods in Queue class using LinkedList class in Java ------------------------------------------------------------------------------- //Queue class public class Queue{ public Queue(){ // use the linked list } public void enqueue(int item){ // add item to end of queue } public int dequeue(){ // remove & return item from the front of the queue } public int peek(){ // return item from front of queue without removing it } public boolean isEmpty(){ // return true if the Queue is empty, otherwise false }...
Define empty methods in Stack class using LinkedList class in Java ------------------------------------------------------------------------------- //Stack class public class...
Define empty methods in Stack class using LinkedList class in Java ------------------------------------------------------------------------------- //Stack class public class Stack{ public Stack(){ // use LinkedList class } public void push(int item){ // push item to stack } public int pop(){ // remove & return top item in Stack } public int peek(){ // return top item in Stack without removing it } public boolean isEmpty(){ // return true if the Stack is empty, otherwise false } public int getElementCount(){ // return current number...
In java the parts listed as todo in linkedsetwithlinkedbad Implement all the methods defined as skeletons...
In java the parts listed as todo in linkedsetwithlinkedbad Implement all the methods defined as skeletons in the LinkedSetWithLinkedBag class. The class implements the SetInterface (described in Segment 1.21 of chapter 1). It utilizes LinkedBag as defined in the UML diagram below: the instance variable setOfEntries is to be an object of LinkedBag. Test your class with the test cases provided in main. Do not make any changes to provided classes other than LinkedSetWithLinkedBag. Similar to Lab02, most of the...
Write a java program StudentDriver class to test the methods of the Student class. Use data:...
Write a java program StudentDriver class to test the methods of the Student class. Use data: Mary Brown 1234 John Jones 5678 Maty Jones 1234 Note: we are including Mary Jones twice so we can test the "equals" method. We can test "compareTo", by test student1 and student2, then student2 and student3. Just submit your driver class as text file. Make sure you also test addQuiz, getTotalScore, getAverage, and toString. Don't worry about testing all of the sets and gets......
in java Write a class named “Stock” to model a stock. The properties and methods of...
in java Write a class named “Stock” to model a stock. The properties and methods of the class are shown in figure below. The method “ percentage ” computes the percentage of the change of the current price vs the previous closing price. Write a program to test the “Stock” class. In the program, create a Stock object with the stock symbol SUND, name Sun Microsystem V, previous closing price of 100. Set a new current price randomly and display...
Java Programming Using the class below, please ), write a static method called parse that parses...
Java Programming Using the class below, please ), write a static method called parse that parses a String for balanced parentheses. we seek only to determine that the symbol ‘{‘ is balanced with ‘}’. parse accepts a single String parameter and returns an int. If parse returns a minus 1, then there are no errors, otherwise, parse should return the position within the String where an error occurred. For example parse(“{3 + {4/2} }”)   would return -1 parse(“{ { 4*X}”)...
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT