Question: Write a method with two parameters, prefix (a string) and levels (a nonnegative integer). The method prints the string prefix followed by section numbers of

Write a method with two parameters, prefix (a string) and levels (a nonnegative integer). The method prints the string prefix followed by “section numbers” of the form 1.1., 1.2., 1.3., and so on. The levels argument determines how many levels the section numbers have. For example, if levels is 2, then the section numbers have the form x.y. If levels is 3, then the section numbers have the form x.y.z. The digits permitted in each level are always '1' through '9'. As an example, if prefix is the string "BOX:" and levels is 2, then the method would start by printing this:

BOX:1.1.

BOX:1.2.

BOX:1.3.

and finish by printing this:

BOX:9.7.

BOX:9.8.

BOX:9.9.

The stopping case occurs when levels reaches zero. The primary string manipulation technique that you will need is the ability to create a new string that consists of prefix followed by a digit and a period. If s is the string you want to create and i is the digit (an integer in the range 1 to 9), then the following statement will perform this task:

s = prefix + '.' + i;

The last part of the expression puts the character that corresponds to the integer i onto the end of the string. This new string, s, can be passed as a parameter to recursive calls.

Step by Step Solution

3.46 Rating (162 Votes )

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock

public void printS... View full answer

blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Data Structures and Other Objects Using Java Questions!