Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Write a recursive method with the following header: public static void numbers(String prefix, int levels) The method prints output consisting of the String prefix followed

Write a recursive method with the following header:

public static void numbers(String prefix, int levels)

The method prints output consisting of 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 and there are 9*9=81 of them. If levels is 3, then section numbers have the form x.y.z and there are 9*9*9 = 729 of them . The digits permitted in each level are always '1' through '9'.

As an example, if prefix is the string "THERBLIG" and levels is 2, then the method would start by printing:

 THERBLIG1.1. THERBLIG1.2. THERBLIG1.3. 

and end by printing:

 THERBLIG9.7. THERBLIG9.8. 
THERBLIG9.9. 

Every line in the output above consists of "THERBLIG" followed by a digit, ".", another digit, and ".". Total there are 81 lines.

As another example, if prefix is the string "Chapter" and levels is 1, then the method would print exactly the following:

 Chapter1. Chapter2. Chapter3. Chapter4. Chapter5. Chapter6. Chapter7. Chapter8. Chapter9. 

The stopping case of recursion occurs when levels reaches zero (in which case the prefix is printed once by itself followed by nothing else).

The Java String class has many manipulation methods, but you'll need only the ability to make a new string which consists of prefix followed by another character (such as '1') and a period ('.'). If s is the String that you want to create and c is the digit character (such as '1'), then the following statement will correctly form s:

 s = prefix + c + '.'; 

This new String s can be passed as a parameter to recursive calls of the method.

***For this method, write a program that would test it. The program should prompt the user to enter value(s) of parameter(s) and execute the method.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Processing Fundamentals Design

Authors: Marion Donnie Dutton Don F. Seaman

14th Edition Globel Edition

1292107634, 978-1292107639

Students also viewed these Databases questions

Question

How do modern Dashboards differ from earlier implementations?

Answered: 1 week ago