CS46A Lab

Decisions

Copyright © Cay S. Horstmann, Kathleen O’Brien 2009-2014 Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.

A. Vowels

  1. In English, the letters a e i o u y denote vowels. Start a Bluej project with this class. Right now the isVowel method is a stub. Complete it so that it returns true if letter consists of a single letter that is a vowel. Otherwise retun false. Driver: What is your method?
  2. Copy WordTester.java into your project. Run the main method. Driver: Did all tests pass? If not, what did you need to do to fix your isVowel method?
  3. Look inside your isVowel method. Inexperienced programmers commonly test for both upper and lowercase in every branch. If you convert letter to lowercase before you test, you can cut the work in half
    String lower = letter.toLowerCase()
    if (lower.equals("a")
       return true;
    else if (some other condition)
       return true;
    else
       return false; 
            

    Rewrite your solution using toLowerCase(). Driver: What is your code.

  4. One can implement isVowel even more simply. Let's look at the the contains method of the String class
    boolean	contains(String str)
          Returns true if and only if this string contains the sequence of char values specified by str.
    Example
    String message = "Hello World";
    message.contains("d")      returns true
    message.conrains("x")      returns false

    Implement the isVowel method using contains. Your code will only use the contains method one time. There will be no logical operators (|| or &&). Ask your lab instructor if you can't get it. Driver: What is your implementation of isVowel using this method?
  5. Which implementation do you prefer? The one with the if statements or the one with contains? Scribe: tell Why?

B. Patterns

  1. Create a Bluej project with this class. It calls a method called fill.
    public boolean fill(int row, int column)
    {
       return your expression here;
    }

    for each row and column between 0 and 9, filling in the square with red when the method returns true.

    This class uses a different graphics library than we have been using. That is okay. You are only interested in the fill method. Just ignone all the other code

    Your task is to implement the fill method in various ways in order to generate different patterns. For example, if your fill method is

    public boolean fill(int row, int column)
    {
       return !(row == 0 && column == 0);
    }

    then all squares are filled in except the one at (0,0).

    In the following exercises, you will be asked to generate various patterns. First write the Boolean expression on a sheet of paper. Driver and scribe take turns for each of the patterns. Then discuss whether your expression is correct. Finally, the driver types in the expression in the program, compiles, and runs. The scribe types in the expression in the report.

    Your goal should be to get the correct result in one try. If you find yourself making random changes on the computer, take your hands off the keyboard and start thinking again!

    Some of the patterns depend on whether the row or column are even or odd. Remember that n % 2 == 0 is true whenever the integer n is even.

    What is the expression that makes this picture? Remember to write it down first. (Driver)

  2. And for this? (Scribe)

  3. And for this? (Driver)

  4. And for this? (Scribe)

  5. And for this? (Driver)

  6. And for this? (Scribe)

  7. And for this? (Driver)

  8. And for this? (Scribe)

  9. Together come up with an interesting pattern of your own. Driver: Put the Boolean expression in the report. Upload a screen capture along with your report.