Projects Content
Instructions
Your Programming project 1 includes 2 parts. The first part is about recursion (Chapter 18) while the second part is about linear searching into an array using recursion (Chapter 19). You MUST submit both parts. Each part is 50 points worth (total = 100 points)
Part A
Write a method that can be used to test how well a computer performs recursion.
Write a method called Recurse(x, y). Use the following logic in your method:
If x = 0 then return y + 1
If y = 0 then return Recurse(x−1, 1)
Otherwise, return Recurse(x−1, Recurse(x, y−1))
Test your method in a program that displays the following values:
Recurse(0, 0)
Recurse(0, 1)
Recurse(1, 1)
Recurse(1, 2)
Recurse(1, 3)
Recurse(2, 2)
Recurse(3, 2)
Part B
Go to chapter 19, page 813, figure 19.2, and perform the following:
a) Modify the linear search highlighted method to perform a recursive linear search of the array.
b) The method should receive the search key and the starting index as arguments. If the search key is found, then return its index in the array. Else,return -1.
c) Each call to the recursive method should check one index in the array
d) Name the recursive method as linearSearchRecurse
e) Modify the rest of the program as needed. Submit the whole program.
When you are done with both parts A and B:
- Put both files (PartA, and PartB) in a folder
- Save the folder as FirstName_LastName_Project1 (e.g. Dimitrios_Sellountos_Project1)
- Zip the folder (.zip extension)
- Submit your zipped folder.
I // Fig. 19.2: LinearSearchTest.java
2
// Sequentially searching an array for an item.
3 import java.security.SecureRandom;
4 import java.util.Arrays;
5
import java.util.Scanner;
6
7 public class LinearSearchTest
8
{
9
10
II
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
// perform a linear search on the data
public static int linearSearch(int data[], int searchKey)
{
// loop through array sequentially
for (int index = 0; index < data.length; index++)
if
(data[index]==searchKey)
return index; // return index of integer
return -1; // integer was not found
} // end method linearSearch
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
SecureRandom generator = new SecureRandom();
int[] data = new int [10]; // create array
for (int i = 0; i < data.length; i++) // populate array
data[i] = 10 + generator.nextInt (90);
System.out.printf("%s%n%n", Arrays.toString(data)); // display array
// get input from user
System.out.print("Please enter an integer value (-1 to quit): ");
int searchInt = input.nextInt ();
// repeatedly input an integer; -1 terminates the program
while (searchInt != -1)
{
int position = linearSearch(data, search Int); // perform search
if (position == -1) // not found
System.out.printf("%d was not found%n%n", searchInt);
else // found
System.out.printf("%d was found in position %d%n%n",
searchInt, position);
// get input from user
System.out.print("Please enter an integer value (-1 to quit): ");
searchInt input.nextInt ();
47
48
49
50
}
51
} // end main
52 } // end class Linear SearchTest
Fig. 19.2 | Sequentially searching an array for an item. (Part 1 of 2.)[59, 97, 34, 90, 79, 56, 24, 51, 30, 69]
Please enter an integer value (-1 to quit): 79
79 was found in position 4
Please enter an integer value (-1 to quit): 61
61 was not found
Please enter an integer value (-1 to quit): 51
51 was found in position 7
Please enter an integer value (-1 to quit): -1