Lab 9: Exploring the String Class and Working with 2D Arrays

In this lab, you will practice using the java.lang.String class in Java.

Exercise 1: Word Count

For the first exercise, implement a class named StringLab containing several functions. First, define wordCount which takes one String parameter and returns the integer number of words it contains. You may use spaces as separators. To complete this function, you may find it helpful to use the split function located in the java.lang.String class.

For example, the code

    wordCount("It is raining outside, and it looks like it will continue for a WHILE!");
should return 14.

Exercise 2: Average Word Length

Now that we can split a sentence into words, let's write a function averageWordLength to compute their average length. To do this, we will want to use the split function again, but this time we also want to exclude the comma, exclamation point, and period punctuation symbols. averageWordLength should accept a String argument and should return the average length of the words in the form of a double.

As an example, calling

    averageWordLength("It is raining outside, and it looks like it will continue for a WHILE!");
should return approximately 3.7.

Exercise 3: Word Search

Lastly, write a function wordSearch which takes two String parameters. The first String is a sequence of words to search, and the second is the word to look for. This should be able to find the given word without worrying about upper-case or lower-case letters, and should return the boolean value true if we find the String somewhere in the list, and false otherwise. You may find the function toLowerCase in the String class to be useful here.

If we run the following code:

   String s = "It is raining outside, and it looks like it will continue for a WHILE!";
   boolean aa = wordSearch( s, "looks" );
   boolean bb = wordSearch( s, "rain" );
   boolean cc = wordSearch( s, "While" );

we should find that aa is true, bb is false, and cc is true.

Exercise 4: Magic Square

In a file named MagicSquare.java add a method called isMagicSquare that accepts a two-dimensional square array of integers as a parameter and returns true if the array is a magic square and false otherwise. A square array/matrix is a magic square if the sum of all of it's rows, columns, and diagonals are equal (see this Wikipedia page for more on this if you like). Your method should work with square arrays of various sizes. Add a main method to the class to show that your method works for both true and false cases.

Note: strictly speaking, magic squares require that each number appear only once but we are not requiring you to check for that case (I suggest that you check that as well if you want a little more exercise).

Examples:

Array A:  2  7  6
          9  5  1
          4  3  8

Array A is a magic square because every row, column, and diagonal adds to exactly 15.

Array B:  2  7  1
          9  5  6
          4  3  8

Array B is not a magic square because row 0 adds to 10 while row 1 adds 20.

Array C: 16  3  2 13
          5 10 11  8
          9  6  7 12
          4 15 14  1

Array C is a magic square because every row, column, and diagonal adds to exactly 34.

When You Are Done . . .

When you are done with these, feel free to leave the meeting.