Answered step by step
Verified Expert Solution
Question
1 Approved Answer
1 (40 marks) Write a recursive method that has one integer parameter (n) and returns the number of binary strings of length n that do
1 (40 marks) Write a recursive method that has one integer parameter (n) and returns the number of binary strings of length n that do not have two consecutive 1's. You should not use any loops in your solution. For example, for n = 4, the number of binary strings of length 4 that do not contain two consecutive 1's is 8: 0000,0001,0010,0100,0101,1000,1001,1010 For this problem, your method needs to return only the number of such strings, not the strings themselves. HINT: The number of such strings is the number of strings that start with a 0 plus the number of strings that start with 10. 2. (60 marks) Write a complete recursive java program to compute the heights of the tick marks on a ruler. Assume that the length of the ruler is a power of 2 (say L=2^n, where n >=1) , and the marks are to be placed at every point between 0 and 2^n, not including the endpoints. The endpoints 0 and 2^n will have height 0. Here are the rules for computing the heights of the ticks for a ruler of length L=2^n: 1. The middle point of the ruler has height log(L), where the logarithm is base 2. Stop if L <= 2. 2. Break the ruler into two equal halves of length L/2 each and set L=L/2 3. Repeat step 1 for both halves resulting from step 2. You may use an array of the appropriate size to hold the heights. When printing your output, print the heights on one line, and the position of the points below. For the printing of the positions of the points, just print the last digit of the position. Here is an example for n=8. 012131210 012345678 Here is an example for n=16. 01213121412131210 01234567890123456 Your program should prompt the user for an integer, check to make sure the length entered by the user is a power of 2 and at least 2. To check if a number is a power of 2, you need the Math.log and Math.round methods. Be careful that the Math.log routine in Java is base e (not base 2). You will need to do the appropriate conversion between base 2 and base e. Finally, your program should print the heights of the ruler's points as illustrated above
Step 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