Objectives

In this lab you will be creating methods with array parameters.

Preparation


Exercise 1: (2 points)  A method for computing the average of an array of integers

The first task will be to revise the Inequality.java code to have a method named arrayAverage that computes the average of an array of integers and returns it to the calling program. When you are finished the main method will look like :   

    public static void main(String[] args) {
        // list of incomes in thousands
        int[] income = {2,10, 532, 4, 53, 28, 291, 38, 6, 17, 73, 21};
        int average = arrayAverage(income);
        System.out.println("Average of income array : " + average);
    }

As you can see in the call to arrayAverage, it takes a single parameter which is a one dimensional array and returns an integer value.

Copy your finished program and its output into your solutions document.

Exercise 2: (2 points) A method to check if a string is in an array of strings

We are going to continue improving the WordMorph program. Start with WordMorph3.java. It's main() method has grown too large which makes it harder to understand. It currently contains this code that checks if a word is in the dictionary:

            boolean foundIt = false;
            for (int j = 0; j < dictionary.length; j++) {
                if (word.equals(dictionary[j]))
                    foundIt = true;
            }
            if (!foundIt) {
                System.out.println("\"" + word + "\" is not a valid word. Exiting.");
                System.exit(1);
            }

We could just put all of that into a method, but there are really two parts to this code.

  1. Checking if the word is in the dictionary array
  2. Acting upon that and either printing a message and exiting, or continuing on

A good rule for method design is to make methods that do just 1 simple to understand task. You are going to make a method to check if a word is in an array.

You will find the location to write Exercise 2 after the end of the main() method. You are writing a method with parameters that are: a word and and some array of Strings. This method does not need to know that it is the dictionary being passed in. It just searches the array for the word and returns true if the word is in the array and false if it is not. Base your method on the code above but note that it is searching "array" rather than "dictionary", and that it is not printing an error message or exiting. It is just searching the array and returning if the word is found in the array. It performs one simple to understand task: "is this word in this array?"

   // Exercise 2: check if a word is in the array, returns true if the word is found
   public static boolean isInArray(String word, String[] array) {
       
      
       
       
       
       return ______;
   }

You won't be able to test this until the next exercise when we use your new method.

Paste your method into your solutions document. If you find on the next exercise that it doesn't work, come back here and fix it and copy your fixed version to your solutions document.

Exercise 3: (1 point) Use your new method to check the dictionary

Now that we have that method, the block of code from the main() method that was shown at the start of Exercise 2, can be greatly simplified by using our new method. Use this code instead. And just use your isInArray(...) method to test if the word is in the dictionary.

            // Exercise 3: check if word is in the dictionary
            if (_____use your method_____) {
                System.out.println("\"" + word + "\" is not a valid word. Exiting.");
                System.exit(1);
            }

Test it, and then copy to your solutions document your code for Exercise 3 and the results from an execution with several valid words and then an invalid word.

Exercise 4: (1 point) Checking if a string is in the history array

That turned out to be a great method because we can also use it for checking the history array. Use your isInArray(...) method to simplify the history checking code:

            // Exercise 4: check if the new word already appeared in history
            for (int j = 0; j < i; j++) {
                if (word.equals(history[j])) {
                    System.out.println("You used that word already. Exiting.");
                    System.exit(1);
                }
            }
        

In the original code we only checked the part of the array that had entries in it. Your isInArray(...) however checks the whole array. This probably doesn't matter. The unfilled array slots were initialized with null values and string.equals(null) will still work. However if you did the comparison in the reverse order than I expected, you will get a NullPointerException when it tries to evaluate null.equals(string). Fix that if you need to

Test it, and then copy to your solutions document your code for Exercise 4 and the results from an execution with several valid words and then a repeated word.

Exercise 5: (2 points) Counting non-matching letters

At the end of your program, add a method to count the number of non-matching characters between two words. For example "fox" and "box" have 1 non-matching letter.

    // Exercise 5: count the number of non-matching letters between 2 words
    public static int countNonMatchingLetters(String s1, String s2) {
       



    } 
    

You can't test this until Exercise 6.

Copy your Exercise 5 code to your solutions document.

Exercise 6: (2 points) Providing hints

Finally lets make our game more friendly by allowing the user to ask for a hint.

At the end of your program, complete this skeleton code to write a method to print hints. Use your countNonMatchingLetters(...) method to check each word in the dictionary and print all those that differ by 1 letter.

    // Exercise 6: method to print hint words that differ by 1 letter
    public static void printHints(String word, String[] dictionary) {
        System.out.print("\t\t");
        ___ loop through the dictionary ___
            ___ check if word is only one letter different than dictionary word ___
                ___ print the word followed by a space ___
        System.out.println();
    }

Copy your Exercise 6 code to your solutions document.

Show it all working

For your report, capture a run of your program where you ask for a hint at least once, and you make it to all 10 words.

Extra Credit (2 points): Not so many hints

Our hints went a bit overboard by showing them every possible choice for the next word. Modify your printHints() method to show them only 3 of the possible hint words, chosen randomly.

YOU ARE DONE

Save your solutions document and submit it.