-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArrayWs4.java
More file actions
241 lines (208 loc) · 5.25 KB
/
Copy pathArrayWs4.java
File metadata and controls
241 lines (208 loc) · 5.25 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
239
240
241
public class ArrayWs4
{
/*
* An Array price[] exists and contains the selling price of all homes in the area over the previous year.
* Write the code that will traverse through the array and print out the lowest selling price.
*/
public static double Price(double[]price)
{
double min = 0;
price[0] = min;
for(int i = 1; i< price.length; i++)
{
if(price[i]<min)
{
min = price[i];
}
}
return min;
}
/*
* Create an array of the first 100 perfect squares starting with 1. Traverse through
the array from the end to print the numbers in reverse order.
*/
public static void Square()
{
int[]squares= new int[100];
for(int i = 0; i<squares.length; i++)
{
squares[i] = (i+1) * (i+1);
}
for(int i = squares.length-1; i>0; i--)
{
System.out.print(squares[i] + " ");
}
}
/*
* Create an Array that contains 5 last names. Traverse through the array to count the
number of letters.
*/
public static void three()
{
String[]names = {"Boris", "Donald", "Conolly", "Obama", "Bob"};
int sum = 0;
for(int i = 0; i<names.length; i++)
{
sum += names[i].length();
}
System.out.println("Letters: " + sum);
}
/*
* Create a String array that contains 6 animals. Traverse through the array to
display every other animal starting with the last.
*/
public static void four()
{
String[]animals = {"ape", "bear", "cat", "dog", "elephant", "gnu"};
for(int i = animals.length - 1; i>=0; i-=2)
{
System.out.println(animals[i]);
}
}
/*
* A String array exists that contains words (lower case)
* Create 2 new arrays aToM(contains words starting with a through m) and
* nToZ (contains words starting with n through z) that will
* separate the initial list into 2 others.
*/
public static void five()
{
String[][]lowerCase = {"dog", "cat", "bear", "horse"}, {"doge", "Cat", "Man"};
String[]aToM = {"a", "b","c","d","e","f","g","h","i","j","k","l","m"};
String[]nToZ = {"n", "o","p","q","r","s","t","u","v","w","x","y","z"};
int count1 = 0;
int count2 = 0;
for(int i = 0; i < lowerCase.length; i++)
{
for(int n = 0; n<aToM.length; n++)
{
for(int letter = 0; letter<lowerCase[i].length();letter++)
{
if((lowerCase[i].substring(letter,letter+1)).equals(aToM[n]))
{
count1++;
}
}
}
for(int m = 0; m<nToZ.length; m++)
{
for(int letter = 0; letter<lowerCase[i].length();letter++)
{
if((lowerCase[i].substring(letter, letter+1)).equals(nToZ[m]))
{
count2++;
}
}
}
}
String[]result1 = new String[count1];
String[]result2 = new String[count2];
int pos = 0;
int pos2 = 0;
for(int i = 0; i < lowerCase.length; i++)
{
for(int n = 0; n<aToM.length; n++)
{
for(int letter = 0; letter<lowerCase[i].length();letter++)
{
if((lowerCase[i].substring(letter,letter+1)).equals(aToM[n]))
{
result1[pos] = aToM[n];
pos++;
}
}
}
for(int m = 0; m<nToZ.length; m++)
{
for(int letter = 0; letter<lowerCase[i].length();letter++)
{
if((lowerCase[i].substring(letter, letter+1)).equals(nToZ[m]))
{
result2[pos2] = nToZ[m];
pos2++;
}
}
}
}
for(int n = 0; n<result1.length; n++)
{
System.out.print(result1[n] + " ");
}
System.out.println();
for(int m = 0; m<result2.length; m++)
{
System.out.print(result2[m] + " ");
}
}
/*
* An array words[] contains each word of the Gettysburg Address.
* Determine how many words have 1 letter, 2 letters, 3 letters, etc.
* The largest word is 14 letters.
* Print out a table of possible word lengths and how many words have that length.
LENGTH COUNT
1 18
2 48
3 101
14 1
*/
public static void six()
{
String[]words = {"a", "to", "cat", "dog", "puppy"};
int[]letterCount = {1,2,3,4,5,6,7,8,9,10,11,12,13,14};
System.out.println("LENGTH" + "\t" + "COUNT");
for(int i = 0; i<letterCount.length; i++)
{
int count = 0;
for(int n = 0; n<words.length; n++)
{
if(i+1 == words[n].length())
{
count++;
}
}
System.out.println(letterCount[i] + "\t" + count);
}
}
public static void seven()
{
String[]words = {"a", "to", "cat", "dog", "puppy","aaaaaaaa", "aaaaaaaa", "aaaaaaaa"};
int count = 0;
for(int n = 0; n<words.length; n++)
{
if(8 == words[n].length())
{
count++;
}
}
String[]vocab = new String[count];
int pos = 0;
for(int n = 0; n<words.length; n++)
{
if(8 == words[n].length())
{
vocab[pos] = words[n];
pos++;
}
}
System.out.println("Vocab: ");
for(int i = 0; i<vocab.length; i++)
{
System.out.println(vocab[i] + " ");
}
}
public static void main(String[]args)
{
Square();
System.out.println();
three();
System.out.println();
four();
System.out.println();
five();
System.out.println();
six();
System.out.println();
seven();
System.out.println();
}
}