-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathLoopier.java
118 lines (83 loc) · 3.74 KB
/
Loopier.java
1
2
3
4
5
6
7
8
9
10
11
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/*==================================================
class Loopier
An exercise in basic array work, iterative and recursive repetition.
==================================================*/
public class Loopier {
/*==================================================
int freqFor(int[],int) -- uses FOR loop to search int array for target
post: returns num of times target occurs in array
==================================================*/
public static int freqFor ( int[] a, int target ) {
// *** YOUR IMPLEMENTATION HERE ***
return -1; //placeholder to get past compiler
}
/*==================================================
int freqForEach(int[],int) -- uses FOREACH loop to search int array
post: returns num of times target occurs in array
==================================================*/
public static int freqForEach ( int[] a, int target ) {
// *** YOUR IMPLEMENTATION HERE ***
return -1; //placeholder to get past compiler
}
/*==================================================
int freqWhile(int[],int) -- uses WHILE loop to search int array
post: returns num of times target occurs in array
==================================================*/
public static int freqWhile ( int[] a, int target ) {
// *** YOUR IMPLEMENTATION HERE ***
return -1; //placeholder to get past compiler
}
/*==================================================
int linSearchR(int[],int) -- recursively searches array of ints for target
post: returns index of first occurrence of target, or
returns -1 if target not found
==================================================*/
//hint: you may want to initialize a var outside the method...
public static int linSearchR ( int[] a, int target ) {
// *** YOUR IMPLEMENTATION HERE ***
return -1; //placeholder to get past compiler
}
/*==================================================
int linSearch(String[],String) -- searches an array of Strings for target
post: returns index of first occurrence of target, or
returns -1 if target not found
==================================================*/
public static int linSearch ( String[] a, String target ) {
// *** YOUR IMPLEMENTATION HERE ***
return -1; //placeholder to get past compiler
}
//main method for testing
public static void main ( String[] args ) {
//TIP: kill & yank the top comment bar down one section
// at a time to test your methods as they develop.
/*==================================================
System.out.println("\nNow testing linSearchR on int array...");
//declare and initialize an array of ints
int[] x = { 2, 4, 6, 8, 6, 42 };
//search for 6 in array x
System.out.println( linSearchR(x,6) );
//search for 43 in array x
System.out.println( linSearchR(x,43) );
System.out.println("\nNow testing linSearch on String array...");
//declare and initialize an array of Strings
String[] y = { "kiwi", "watermelon", "orange", "apple",
"peach", "watermelon" };
//search for "watermelon" in array y
System.out.println( linSearch(y,"watermelon") );
//search for "lychee" in array y
System.out.println( linSearch(y,"lychee") );
//declare and initialize an array of ints
int[] z = { 1, 5, 3, 5, 1, 5 };
//compute frequency of 5 in array z
int q = 5;
System.out.println( freqFor ( z, q ) );
System.out.println( freqForEach ( z, q ) );
System.out.println( freqWhile ( z, q ) );
//compute frequency of 2 in array z
int r = 2;
System.out.println( freqFor(z,r) );
System.out.println( freqForEach(z,r) );
System.out.println( freqWhile(z,r) );
==================================================*/
}//end main()
}//end class Loopier