forked from sensei-thundercleese/closet
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSearching.java
More file actions
238 lines (162 loc) · 6.42 KB
/
Copy pathSearching.java
File metadata and controls
238 lines (162 loc) · 6.42 KB
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
/*==================================================
class Searching
==================================================*/
public class Searching {
/*==================================================
int frequency(int[],int) -- uses FOR loop to search int array for target
post: returns num of times target occurs in array
==================================================*/
public static int frequency( int[] a, int target ) {
int freq = 0;
for( int i : a )
if ( i == target )
freq++;
return freq;
}
/*==================================================
int linSearch(int[],int) -- searches an array of ints for target
post: returns index of first occurrence of target, or
returns -1 if target not found
==================================================*/
public static int linSearch( int[] a, int target ) {
int tPos = -1;
int i = 0;
while ( i < a.length ) {
if ( a[i] == target ) {
tPos = i;
break;
}
i++;
}
return tPos;
}
/*==================================================
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 ) {
int tPos = -1;
int i = 0;
while ( i < a.length ) {
if ( a[i].equals(target) ) {
tPos = i;
break;
}
i++;
}
return tPos;
}
/*==================================================
int linSearch(Binary[],Binary) -- searches an array of Binarys for target
post: returns index of first occurrence of target, or
returns -1 if target not found
==================================================*/
public static int linSearch ( Binary[] a, Binary target ) {
//YOUR IMPLEMENTATION HERE
return -1; //placeholder
}
/*==================================================
int linSearch(Hexadecimal[],Hexadecimal) -- searches an array
of Hexadecimals for target
post: returns index of first occurrence of target, or
returns -1 if target not found
==================================================*/
public static int linSearch ( Hexadecimal[] a, Hexadecimal target ) {
//YOUR IMPLEMENTATION HERE
return -1; //placeholder
}
/*==================================================
int linSearch(?,?) -- searches an array
of Objects of same type for target
post: returns index of first occurrence of target, or
returns -1 if target not found
==================================================*/
//YOUR FUNCTION HERE
// utility/helper fxn to display contents of an array of Objects
private static void printArray( int[] arr ) {
String output = "[ ";
for( int i : arr )
output += i + ", ";
output = output.substring( 0, output.length()-2 ) + " ]";
System.out.println( output );
}
private static void printArray( Object[] arr ) {
String output = "[ ";
for( Object o : arr )
output += o + ", ";
output = output.substring( 0, output.length()-2 ) + " ]";
System.out.println( output );
}
//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 linSearch on int array...");
//declare and initialize an array of ints
int[] iArr = { 2, 4, 6, 8, 6, 42 };
printArray( iArr );
//search for 6 in array
System.out.println( linSearch(iArr,6) );
//search for 43 in array
System.out.println( linSearch(iArr,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" };
printArray( y );
//search for "watermelon" in array y
System.out.println( linSearch(y,"watermelon") );
//search for "lychee" in array y
System.out.println( linSearch(y,"lychee") );
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
System.out.println("\nNow testing linSearch on Binary array...");
//declare Binary array
Binary[] bArr = new Binary[10];
printArray( bArr ); //should show array of nulls
//initialize Binary array
//Q: Why would a FOREACH loop not do the job here?
for( int i = 0; i < bArr.length; i++ ) {
bArr[i] = new Binary( (int)(100 * Math.random()) );
}
printArray( bArr ); //should now show Binary numbers
//search for the value in slot 3 in array
System.out.println( "Searching for " + bArr[3] + "..." );
System.out.println( linSearch(bArr, bArr[3] ) );
//search for 101 in array
System.out.println( linSearch(bArr, new Binary(5) ) );
//search for 100000000 in array
System.out.println( linSearch(bArr, new Binary(256)) );
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
System.out.println("\nNow testing linSearch on Hexadecimal array...");
//declare Hexadecimal array
Hexadecimal[] hArr = new Hexadecimal[10];
printArray( hArr ); //should show array of nulls
//initialize Hexadecimal array
//Q: Why would a FOREACH loop not do the job here?
for( int i = 0; i < hArr.length; i++ ) {
hArr[i] = new Hexadecimal( (int)(100 * Math.random()) );
}
printArray( hArr ); //should now show Hexadecimal numbers
//search for the value in slot 3 in array
System.out.println( "Searching for " + hArr[3] + "..." );
System.out.println( linSearch(hArr, hArr[3] ) );
//search for 5 in array
System.out.println( linSearch(hArr, new Hexadecimal(5) ) );
//search for 100 in array
System.out.println( linSearch(hArr, new Hexadecimal(256)) );
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
System.out.println("\nNow testing linSearch on Comparable array...");
//INSERT YOUR TESTING MACHINERY HERE
// Follow the pattern of the machinery above.
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
==================================================*/
}//end main()
}//end class Searching