Write a method with two parameters, prefix (a string) and levels (a nonnegative integer). The method prints

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 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.

Fantastic news! We've Found the answer you've been seeking!

Step by Step Answer:

Related Book For  book-img-for-question
Question Posted: