Summary: Implement a program that will process an array of strings (the words from the play Hamlet) in different ways. An Eclipse project template can be downloaded from the D2L assignment page. Any code you add should use only arrays, not ArrayList or any other Collection or Map class.
Change the project name to Lab7-yourname, substituting yourname with your name. Be sure to add appropriate comments before the main method.
Find the following information from the string array. Do not read the file again.
1. Print all the words with length 13 on one line, all 46 of them. The first one is Ship-wrights, (yes, there is a comma in the word) and the last one is Guildensterne.
2. Ask the user for a string to search for. Print the number of words that contain that string. Also print up to the first 10 words that contain that string. Example: 86 words contain vn, the first 10 are:
vnfold vnimproued vnto vnmanly vnfortified, vnschool'd: vnpreuayling vnforc'd vnprofitable vnweeded
3. Ask the user for a index into the array. The user should be asked repeatedly until a valid index is entered. Print the word at that index, the 10 previous words, and the 10 next words. The words should be printed in this way: the 10 previous words, a newline, the word at that index, a newline, the 10 next words, and a newline. Do something reasonable if the index is near 0 or the length of the string array.
4. Find the character (Unicode character from 32-126) that occurs the most. Print the character and how many times it occurs. Suggestion: Create an int array of length 127 which stores a count of each character. If the array is named counts, and you have a char named ch, then counts[ch]++ will increment the count for that character. This will require a double loop. The outer loop goes through the words. The inner loop goes through the characters in the current word.
5. (extra credit) Implement methods for the above items as seems reasonable to you. To get credit for a method, there must be comments before the method that explains what it does, the method must contain a loop that does significant work for the method, and the method does not duplicate other methods in the String class (or other methods implemented in the program).
6. (extra credit) For item 1, do not print any words twice. Your solution should work whether I change 13 to any other number.
Answer the following questions and save your answers in a text file (.txt extension) in your Eclipse project. You can start editing a text file in Eclipse using:
File > New > Untitled Text File
Then do File > Save to start a dialog to save the file in your project.
. What is output by the following code?
int[] myList = {1, 13, -13, 13, -13, 13, 1};
int foo = 0;
for (int i = 1; i < myList.length; i++) {
if (myList[i] >= myList[foo]) {
maxIndex = i;
}
}
System.out.println(foo);
2. Provide a flowchart of the code segment in the previous exercise. Include the image file in your Eclipse project. Move it into the project folder, then right-click on the project > refresh.
3. Write a statement that declares m to be a two-dimensional double array with 13 rows and 42 columns.
4. Suppose m is a two-dimensional array. What Java expression tells you the number of rows in m?
5. Suppose m is a two-dimensional array. What Java expression tells you the number of columns in m?
6. Suppose m is a two-dimensional array. What Java expression tells you the number of elements in m?
7. What does the following code print?
int[][] m = new int[3][3];
int val = 1;
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
m[j][i] = val;
val += 1;
}
}
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
System.out.printf("%s ", m[i][j]);
}
System.out.println();
}
8. In the above code, what change to m[j][i] = val; should be made so the code prints: [Hint: don't use val]
0 1 2
1 2 3
2 3 4
9. Suppose m is a two-dimensional array with 8 elements. What are the possible dimensions of m? Hint: 4 possibilities.
10. In the documentation for the Arrays class, write exactly the "Modifer and Type" and Method and Description for the fill method on int arrays (the one with 2 parameters). Write an example creating an int array of size 13, then using the fill method to fill it with -13.