Lab 9 In-Class: More Loops

As usual, create a lab9 subdirectory for today's lab, open up Mozilla Firefox and the Web version of this handout, and open emacs.

  1. Program Account.java contains a simplified version of the Account class from an earlier lab. As you probably recall, the Account class models a simple bank account. In this exercise you will write a program that instantiates an Account object then lets the user deposit money, withdraw money, and check the balance as much as he/she wishes.

    1. Save Account.java to your directory.

    2. The file ATM.java contains a skeleton of the program described above. Complete the program as follows (as indicated by the comments in the program):
      • Declare and instantiate an Account object with the information read in.
      • Add a do...while loop that executes until the user chooses to quit. The loop should print out a menu of options in the following format:
                 Options:
                 1. Deposit
                 2. Withdraw
                 3. Check the balance
                 4. Quit
        
                 Enter your choice (1,2,3, or 4): 
        
        Then read in the user's choice (in the choice variable already declared) and do what the user requests (which requires using the appropriate method from the Account class). Note that if the user chooses deposit or withdraw, the program must ask for and read in the amount. Also, if the user types in an incorrect choice (such as a 6) the program should print a message telling the user to try again.

    3. Thoroughly test your program.

    4. Print ATM.java to turn in.

  2. Sports fans often get excited over a team or player who is having a "hot streak." However it turns out that long runs of events, even random ones, occur more often than many people think. In this exercise you'll write a program to find the longest run of heads in a sequence of coin flips. The Coin class from Listing 5.4 in the text (pages 221-222) is in the file Coin.java. Copy it to your directory, then write a program to find the length of the longest run of heads in 100 flips of the coin. A skeleton of the program is in the file Runs.java. You will add to the skeleton as follows (see the comments in the program):
    1. Create a coin object.
    2. Inside the loop, use the flip method to flip the coin, the toString method (implicitly -- just print the coin object, and it will call the toString method automatically) to print the results of the flip, and the isHeads method to see if the result was HEADS.
    3. To keep track of the current run length (the number of times in a row that the coin was HEADS), note that if the coin is HEADS on this flip, the current run is one longer than it was; if it's not HEADS, then the counter starts over. Be sure to update the maxRun variable as necessary.
    4. While you're checking for HEADS, if it's HEADS also print the number it is in the current run. For example, your output might start like this, indicating a longest run (so far) of 4:
      Heads 1
      Heads 2
      Tails
      Tails
      Tails
      Heads 1
      Heads 2
      Heads 3
      Heads 4
      Tails
      Heads 1
      ...
      
      You'll have to work a little on the formatting to get this to come out right (think about where you'll use print and where you'll use println).

    5. After the loop, print the length of the longest run (clearly labeled, of course).

    Print Runs.java to hand in.

  3. The Stars program in the text (Listing 5.14 on page 257) uses a nested for loop to print out a pattern of stars. In this exercise you will write a modified version to print a different pattern.
    1. The file Stars.java contains a skeleton of the program. Add the nested loop to print the following triangular pattern. Note that in each row you first need to print out some spaces (you need a loop to do this) then print the stars (a second loop). Carefully plan how many you need! Also note that the number of rows is an input variable (numRows) rather than a constant so the program should work for any number of rows.
                           *
                          * *
                         * * *
                        * * * *
                       * * * * *
                      * * * * * *
      

    2. When your triangle is correct, modify the program to print the following pattern:
                           *
                          * *
                         * * *
                        * * * *
                       * * * * *
                      * * * * * *
                       * * * * *
                        * * * *
                         * * *
                          * *
                           *
      
      You will need a second nested for loop after the first to print the bottom part of the diamond. It will be similar to the first - you can copy that one and make adjustments. The first nested loop should stay the same - the numRows variable will represent the number of rows in the upper triangle, not the whole diamond.

    3. Print a copy of your final program to hand in.

  4. Write a program that reads in a phrase or sentence and determines the percentage of letters in the phrase that are vowels. The program must count all the letters in the phrase and count the vowels (a, e, i, o, and u). The file Vowels.java contains the skeleton of the program. Open it and do the following as indicated in the comments:
    1. Add declarations for any additional variables you need and initialize them as appropriate.
    2. Change the phrase read in to all lower case for easier processing (so you don't have to test for both upper and lower case vowels).
    3. Write a for loop to go through the phrase character by character. In the body of the for loop you need to determine if the current character is a letter. If it is you need to count it and then see if it is a vowel. There are several ways to determine if a character is a letter including different ways of comparing characters. However, another way is to use the isLetter method from the Character class. The signature of the method is as follows:
            public static boolean isLetter(char c)
      
      Recall that a static method is called using the class name. Hence, a call would have the form
             Character.isLetter(ch)
      
      where ch is a variable of type char. Since a boolean is returned the above call should be placed in an appropriate place for a boolean such as an if.

    4. After the loop print out the number of vowels and the total number of letters, appropriately labeled.

    5. Also compute and print the percentage of letters that are vowels. Use a percent formatter object from the NumberFormat class to print the percent. To do this you need to do the following (see pages 131-132 for an example).
      • Add an import statement to import java.text.NumberFormat.
      • Just before you print, get a format object (named fmt for example) as follows:
              NumberFormat fmt = NumberFormat.getPercentInstance();
        
      • Then, in your print statement use the format method to print the percent (note: the formatter multiplies by 100 for you):
         System.out.println ("Percentage of vowels: " + fmt.format(whatever));
        
    6. [READ ALL BEFORE DOING ANYTHING!!] It would be nice to have the program let the user keep entering phrases rather than having to restart the program every time. To do this we need another loop surrounding the current code. That is, the current loop will be nested inside the new loop. Add an outer while loop that will continue to execute as long as the user does NOT enter Q or q for the phrase. Note that this makes the outer loop a sentinel controlled loop -- the program is processing phrases and a special value Q will stop it. The outline of the program is as follows:
           read in the first phrase
      
           while the phrase is neither "Q" nor "q"
           {
             initialize the counters etc. to set up the for loop
      
             for loop to count letters and vowels in the phrase
      
             print the number of vowels & letters and percentage of vowels
           
             read in the next phrase or "Q" to quit
           }
      
      
      Note that all you need to do is add the sentinel controlled while loop around the code you already have. Be sure the code to initialize the counters is inside the while loop (you start the counters over for each phrase).

      Be sure to go through the program and properly indent after adding code (with nested loops the inner loop should be indented). Emacs will do the work for you! Select the region you want to indent (with the mouse or keys), then press Alt-Ctrl-\ ("indent region"). Voila!

    Print the final version of your program

Hand in: