
mystery(3, 6).
int mystery( int a, int b )
{
if( a <= b )
{
return mystery( a, b - 1 ) + mystery( a + 2, b );
}
else
{
return a;
}
}
RecAlgosApp and write the methods for the problems listed below:
This sequence is generated in the following way:1st 2nd 3rd 4th 5th 6th 7th 8th 9th 10th 11th 12th 13th 14th -2, 0, -1, -3, 3, -5, -1, 7,-17, ? ? ? ? -65
i-th number is: twice the number 3 spots before minus the number one spot beforeai = 2*ai-3 - ai-1sequence(n) that computes the n-th integer in the sequence for n >= 1.
Draw the execution of the computation of sequence(6). It is fine to abbreviate in the drawing as seq(...):
System.out.println("the 6-th number is: " + sequence( 6 )); // displays: ... is -5
System.out.println("the 8-th number is: " + sequence( 8 )); // ... is 7
a and b, denoted by gcd(a, b), is the largest integer that divides evenly both a and b.
The greatest common divisor can be computed efficiently using a recursive algorithm based on the following observations:
gcd(a, b) equals gcd(b, r) where r is the remainder when a is divided by b
[ it is irrelevant whether a > b or b > a, so no special handling is needed ]
gcd(a, 0) equals agcd(24, 18):
Note: In Java the remainder from integer division can be computed using the % (percent) operator. That is, 6%4 will equal 2, since 6 divided by 4 leaves a remainder of 2; 24%9 will equal 5, since 24 divided by 9 leaves a remainder of 5.gcd(24, 18) // the original task | gcd(18, 6) // by 1st rule; 6 is remanider of 24/18 | gcd(6, 0) // by 1st rule; 0 is remanider of 18/6 | 6 // by 2nd rule since b is 0
gcd(a, b) which takes two non-negative integers a and b and returns their greatest common divisor.
Draw the execution of the computation for gcd(58, 32):
gcdLoop(a, b) which takes two non-negative integers a and b and returns their greatest common divisor.
Hint repeatedly update the given a,b according to the 1st rule and stop when 2nd rule reached.
System.out.println("gcd(24, 18) = " + gcd(24, 18)); // displays: gcd(24, 18) = 6
System.out.println("gcd(8, 13) = " + gcd(8, 13)); // gcd(13, 8) = ?
Loop version:
System.out.println("gcdLoop(24, 18) = " + gcdLoop(24, 18)); // displays: gcdLoop(24, 18) = 6
System.out.println("gcdLoop(8, 13) = " + gcdLoop(8, 13)); // gcdLoop(13, 8) = ?
RecDrawApp and write the following recursive methods:
drawMickey(faceX, faceY, radius, depth)
This is a recursive method that generates a pattern of overlapping Mickey Mouse looking characters:
The face of a Mickey is centered at the given
(faceX,faceY) coordinates and is of the given radius. The ears of a Mickey have half the radius and are centered precisely at the upper-left and upper-right corners of the bounding square of the face of the Mickey.
What are the Easy/Special Cases? What is the Hard Case?
The Mickeys above have been generated by calling:
drawMickey(180, 400, 80, the-depth-value).
Note: Make sure to add an outline for each circle as shown in the images above. Recall that to accomplish this effect we can use "fillColor(outlineColor)|lineWidth" when specifying shape color.
drawStackedLines(x1, x2, y, vertSpace, depth)
This is a recursive method that generates a pattern of stacked lines as shown below:
In the image above, the missing piece is equal in size to its neighbors. The longest line is shown at the original
y-level and the rest of the lines are spaced vertically based on the given value vertSpace.
What are the Easy/Special Cases? What is the Hard Case?
The lines above have been generated by calling:
// start from y-level 20 spanning along x between 10 and 300 skipping down by 35 between levelsdrawStackedLines(10, 300, 20, 35, the-depth-value)
fillRegion(image, r, c, color)
This is a recursive method that fills a non-black, i.e. non-background, region in the image with the given color.
You may assume that the region is originally white, but the original color is not important, and should not be mentioned, so do not make any references to white color.
The recoloring process works as follows:
Imagine the pixels in the region as a pack of chameleons that have to change to the given color. One chameleon is chosen at random to start the re-coloring process (to start color filling). This chameleon asks its four non-diagonal neighbors to start a re-coloring process, and these neighbors then ask their four non-diagonal neighbors to start a re-coloring process and so on. Of course, the "asking" chameleon also needs to change its own color. Hint: As we have done in class, identify the possible "easy cases" by considering the possible scenarios for pixel position and pixel color.This is a short method. For example (these test cases are sufficient):
fillAllRegions(image)
This method modifies the given image by filling all objects in the image with colors from a color palette.
Here is how the method works: It goes through all pixels in the givenimage. Whenever it comes across WHITE pixel, i.e. 0x00FFFFFF, it fills the region starting at that pixel.
Here you need to use the exact value for WHITE which is 0x00FFFFFF. This is an integer even though it has letters in it, so do not put quotes around it.
No special handling should be needed for pixels on the boundary.
Here is the starter code:
____ fillAllRegions( image )
{
// RED GRN BLU YEL MAG CYAN
int[] palette = { 0x00FF0000, 0x0000FF00, 0x000000FF, 0x00FFFF00, 0x00FF00FF, 0x0000FFFF };
int colorIndex = 0; // start with RED
// loop to find WHITE pixel:
...
... colorIndex = (colorIndex + 1) % palette.length; // select new color after region filled
...
}
For example (these test cases are sufficient):
BinarySearchApp and write the methods binary search methods described in this section.
Binary Search Algorithm (start the slideshow and slowly go through the animation)Here is a high-level description of Binary Search:
The task is to search in a "big book" between pages [i,j]. The solution is to check the middle page and if the item is not there, then search in a "smaller book" between pages [?,?] (hint: the words in the book are soted).
What are the Easy/Base Cases? What is the Hard Case?
i,j (inclusive):
binarySearch(numbers, value, i, j)
To simplify the code do not check whether the given indices are valid. The method returns true if the value is found in the array; otherwise returns false. For example:
// 0 1 2 3 4 5 6 7 8 the page numbers
double[] numbers = { 1, 4, 5, 8, 9, 10, 23, 35, 47 };
System.out.println("Is 5 between pages 0..8? " + binarySearch(numbers, 5, 0, 8)); // displays: ... true
System.out.println("Is 5 between pages 1..6? " + binarySearch(numbers, 5, 1, 6)); // ... ?
System.out.println("Is 5 between pages 2..6? " + binarySearch(numbers, 5, 2, 6)); // ... ?
System.out.println("Is 5 between pages 3..7? " + binarySearch(numbers, 5, 3, 7)); // ... ?
System.out.println("Is 4 in the array? " + binarySearch(numbers, 4, 0, 8)); // ... true
System.out.println("Is 25 in the array? " + binarySearch(numbers, 25, 0, 8)); // ... false
To make it convenient for the user to search in the whole array we provide the following method (copy in your project and fill in):
boolean binarySearch( double[] numbers, double value )
{
// call the recursive version that takes four parameters
// we fill in *first_page* and *last_page*
return binarySearch( numbers, value, _first?_, _last?_ );
}
The method returns true if the value is found in the array; otherwise returns false. For example:
double[] numbers = { 1, 4, 5, 8, 9, 10, 23, 35, 47 };
System.out.println("Is 4 in the array: " + binarySearch(numbers, 4)); // displays: ... true
System.out.println("Is 25 in the array: " + binarySearch(numbers, 25)); // ... false
We keep track of two indices,Write a non-recursive (i.e. with a loop) method that implements the Binary Search algorithm for finding a value in a sorted list of numbers:iandj, that represent the first and last "page", respectively, where the item of interest can possibly be. Then, as long as we have not run out of pages we open the book in the middle, check if we have found the item, and if not, update either the first or the last page (index) to eliminate half of the items in the array.
binarySearchLoop(numbers, value)
For example:
double[] numbers = { 1, 4, 5, 8, 9, 10, 23, 35, 47 };
System.out.println("Is 4 in the array: " + binarySearchLoop(numbers, 4)); // displays: ... true
System.out.println("Is 25 in the array: " + binarySearchLoop(numbers, 25)); // ... false
cs111 (where you run DrJava) hw/RecAlgosApp/src/cs1/RecAlgosApp (this is where your code is saved) RecAlgosApp.java and RecAlgosApp.log (ignore the other files) hw/RecDrawApp/src/cs1/RecDrawApp (this is where your code is saved) RecDrawApp.java and RecDrawApp.log (ignore the other files) hw/BinarySearchApp/src/cs1/BinarySearchApp (this is where your code is saved) BinarySearchApp.java and BinarySearchApp.log (ignore the other files)