Write a program that will contain the following methods: printSub1, printSub2, printSub3, printSub4 – print all substrings of S using recursion. For example, if S = "abcd": printSub1 will print "a", "ab", "abc", "abcd", "b", "bc", "bcd", "c", "cd", "d" printSub2 will print "d", "cd", "bcd", "abcd", "c", "bc", "abc", "b", "ab", "a" printSub3 will print "a", "b", "c", "d", "ab", "bc", "cd", "abc", "bcd", "abcd" printSub4 will print "abcd", "bcd", "abc", "cd", "bc", "ab", "d", "c", "b", "a" Note that the actual output will not have quotation marks around the substrings. The substrings must print in the order shown for each method. You may only use these String methods in your work: length(), substring() Your main method in this program should provide an appropriate menu which allows the user the enter the string of their choice and pick any of the print substring options they want. IN JAVA
Write a program that will contain the following methods: printSub1, printSub2, printSub3, printSub4 – print all substrings of S using recursion. For example, if S = "abcd": printSub1 will print "a", "ab", "abc", "abcd", "b", "bc", "bcd", "c", "cd", "d" printSub2 will print "d", "cd", "bcd", "abcd", "c", "bc", "abc", "b", "ab", "a" printSub3 will print "a", "b", "c", "d", "ab", "bc", "cd", "abc", "bcd", "abcd" printSub4 will print "abcd", "bcd", "abc", "cd", "bc", "ab", "d", "c", "b", "a" Note that the actual output will not have quotation marks around the substrings. The substrings must print in the order shown for each method. You may only use these String methods in your work: length(), substring() Your main method in this program should provide an appropriate menu which allows the user the enter the string of their choice and pick any of the print substring options they want. IN JAVA
Step-1: Start
Step-2: Import the java.util.Scanner package for user input.
Step-3: Define the SubstringPrinter class:
Step-3.1: Create a Java class named SubstringPrinter
Step-4: Define printSub1 method:
Step-4.1: Create a public method named printSub1 that takes a single argument, a string s.
Step-4.2: Inside printSub1, call the printSub1Helper method with the string s and an initial index of 0.
Step-5: Define printSub1Helper method:
Step-5.1: Create a private recursive method named printSub1Helper that takes two parameters: the input string s and an index index.
Step-5.2: Check if the index is equal to the length of the input string s. If it is, return from the method, which is the base case for recursion.
Step-5.3: Use a nested loop with variables i and index to iterate over the input string. The outer loop (i) starts at the index and goes up to the end of the string.
Step-5.4: Inside the loop, use the substring method to extract and print the substring starting from the index to the current value of i.
Step-5.5: After printing all substrings starting at the current index, make a recursive call to printSub1Helper with an incremented index to continue printing substrings starting from the next character.
Step-6: Define printSub2 method:
Step-6.1: Create a public method named printSub2 that takes a single argument, a string s.
Step-6.2: Inside printSub2, call the printSub2Helper method with the string s and an initial index set to s.length() - 1.
Step-7: Define printSub2Helper method:
Step-7.1: Create a private recursive method named printSub2Helper that takes two parameters: the input string s and an index index.
Step-7.2: Check if the index is less than 0. If it is, return from the method, which is the base case for recursion.
Step-7.3: Use a nested loop with variables i and index to iterate over the input string. The outer loop (i) starts at the end of the string and goes down to the index.
Step-7.4: Inside the loop, use the substring method to extract and print the substring starting from the index to the current value of i.
Step-7.5: After printing all substrings starting at the current index, make a recursive call to printSub2Helper with a decremented index to continue printing substrings starting from the previous character.
Step-8: Define printSub3 and printSub3Helper methods:
Step-8.1: printSub3 and printSub3Helper are structured similarly to printSub1 and printSub1Helper, but they print substrings in a different order.
Step-9: Define printSub4 and printSub4Helper methods:
Step-9.1: printSub4 and printSub4Helper are structured similarly to printSub2 and printSub2Helper, but they also print substrings in a different order.
Step-10: Define the main method:
Step-10.1: Create a main method, the entry point of the program.
Step-10.2: Create a Scanner object to read user input from the console.
Step-10.3: Prompt the user to enter a string and store it in the input variable.
Step-10.4: Display a menu of options for printing substrings (1, 2, 3, or 4).
Step-10.5: Read the user's choice and store it in the choice variable.
Step-10.6: Use a switch statement to call the appropriate printSubX method based on the user's choice.
Step-10.7: Close the Scanner object to release resources.
Step-11: Stop
Step by step
Solved in 4 steps with 8 images
the output for printsub3 should be in this order:
a
b
c
d
ab
bc
cd
abc
bcd
abcd
and output for printsub4 should be in this order:
abcd
bcd
abc
cd
bc
ab
d
c
b
a
the output for all of them are in correct
it should be
printSub1 will print "a", "ab", "abc", "abcd", "b", "bc", "bcd", "c", "cd", "d"
printSub2 will print "d", "cd", "bcd", "abcd", "c", "bc", "abc", "b", "ab", "a"
printSub3 will print "a", "b", "c", "d", "ab", "bc", "cd", "abc", "bcd", "abcd"
printSub4 will print "abcd", "bcd", "abc", "cd", "bc", "ab", "d", "c", "b", "a"
Note that the actual output will not have quotation marks around the substrings. The substrings must print in the order shown for each method. You may only use these String methods in your work: length(), substring()
AND ALSO THE OUTPUT SHOULD BE IN ORDER