Hello, I need help for this JAVA homework. Recursive Descent Parser Implement a
ID: 3837369 • Letter: H
Question
Hello, I need help for this JAVA homework.
Recursive Descent Parser
Implement a class called SubstringTester that uses recursion to generate all substrings of a given String. For example, the substrings of the string "Sluggo" are the 22 strings:
Sluggo
Slugg
Slug
Slu
Sl
S
luggo
lugg
lug
lu
l
uggo
ugg
ug
u
ggo
gg
g
go
g
o
""
You are not allowed to use any loops to build the substrings -- at least in your final product. You may, however, find it productive to solve the problem using loops first, and then translate to recursive code.
Implement methods in SubstringTester that will interact with the user, acquire a phrase and produce all of the substrings of that phrase. This class should
* Get a string from the user (possibly in ‘main’)
* Generate and display the substrings of the phrase
* Repeat the above two steps until the user chooses to quit
Javadoc
No Javadoc on this assignment.
To Turn In
Turn in all source code in a single zip file.
Note
Hint: You will need either two recursive methods or one recursive method that accepts a ‘flag’ indicating the type of substringing to be performed.
You should not have a great deal of code for this assignment -- think about it -- its recursion :-)
Thank you
Explanation / Answer
public class Substrings {
public static void main(String[] args){
printSubstrings("abcdef", 6);
}
static void printSubstrings(String string, int length){
if (string.length() > 0){
if (length > 0)
System.out.println(string.substring(0, length));
if (length == 0)
printSubstrings(string.substring(1), string.length()-1);
else
printSubstrings(string, length-1);
}
}
}
Here is the solution...! Hope you like it. To understand it dry run the code. Even after that if you face problem, please let me know. I shall try my best to resolve the issue.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.