Question
Use java language to solve this question, if you copy the python code for this answer i will *report you*. Provide a nonrecursive implementation of
Use java language to solve this question, if you copy the python code for this answer i will *report you*.Provide a nonrecursive implementation of the drawInterval method for the En- glish ruler project of Section 5.1.2. There should be precisely 2 c 1 lines of output if c represents the lengthof the center tick. If incrementinga counter from 0 to 2 c 2, the number of dashes for each tick line should be exactly one more than the number of consecutive 1s at the end of the binary representation of the counter. I will give you helpful evaluation if you finished it, thank you!
In the case of computing the factorial function, there is no compelling reason for preferring recursion over a direct iteration with a loop. As a more complex example of the use of recursion, consider how to draw the markings of a typical English ruler. For each inch, we place a tick with a numeric label. We denote the length of the tick designating a whole inch as the major tick length. Between the marks for whole inches, the ruler contains a series of minor ticks, placed at intervals of 1/2 inch, 1/4 inch, and so on. As the size of the interval decreases by half, the tick length decreases by one. Figure 5.2 demonstrates several such rulers with varying major tick lengths (although not drawn to scale). Figure 5.2: Three sample outputs of an English ruler drawing: (a) a 2-inch ruler with major tick length 4 ; (b) a 1-inch ruler with major tick length 5 ; (c) a 3 -inch ruler with major tick length 3. A Recursive Approach to Ruler Drawing The English ruler pattern is a simple example of a fractal, that is, a shape that has a self-recursive structure at various levels of magnification. Consider the rule with major tick length 5 shown in Figure 5.2(b). Ignoring the lines containing 0 and 1, let us consider how to draw the sequence of ticks lying between these lines. The central tick (at 1/2 inch) has length 4 . Observe that the two patterns of ticks above and below this central tick are identical, and each has a central tick of length 3. In general, an interval with a central tick length L1 is composed of: - An interval with a central tick length L1 - A single tick of length L - An interval with a central tick length L1 Although it is possible to draw such a ruler using an iterative process (see Exercise P-5.29), the task is considerably easier to accomplish with recursion. Our implementation consists of three methods, as shown in Code Fragment 5.2. The main method, drawRuler, manages the construction of the entire ruler. Its arguments specify the total number of inches in the ruler and the major tick length. The utility method, drawLine, draws a single tick with a specified number of dashes (and an optional integer label that is printed to the right of the tick). The interesting work is done by the recursive drawlnterval method. This method draws the sequence of minor ticks within some interval, based upon the length of the interval's central tick. We rely on the intuition shown at the top of this page, and with a base case when L=0 that draws nothing. For L1, the first and last steps are performed by recursively calling drawInterval (L1). The middle step is performed by calling method drawLine (L). Code Fragment 5.2: A recursive implementation of a method that draws a rulerStep by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started