CS99

Fundamental Programming Concepts
Summer 2001

Lab 7

Reading for next lab: 4.1 Savitch. We'll introduce classes and methods in Lab 8.

 

Overview

The goals of this lab are to:

  • Use nested while loops to generate different patterns on the screen

  • Print the numbers of each kind of vowel in a string and output the total number of consonants

  • Calculate the mode in a sequence of data values

  • Part I: More star patterns, nested whiles

    In the sub-portions of this part of the Lab, you may use only the following types of print statements

    • System.out.println();

    • System.out.print("*") or System.out.print("* ");

    • System.out.print(" ");

    Part A:
    Write a program that reads in the size of a square and then prints a hollow square of that size out of asterisks.  For instance, if your program takes in an input of 5, it should print:

    *****
    *    *
    *    *
    *    *
    *****

    Part B:
    Write a program that takes as input a positive number n, and prints an equilateral triangle.  For instance, for an input of 4, it should print:

    *
    ***
    *****
    *******

    Part C:
    Modify Part B so that it prints two equilateral triangles on top of each other like so:

    *
    ***
    *****
    *******

    *******
    *****
    ***
    *

    Part D:
    Write a program that prints the following checkerboard pattern on the screen:

    * * * * * * * *
     * * * * * * * *
    * * * * * * * *
     * * * * * * * *
    * * * * * * * *
     * * * * * * * *
    * * * * * * * *
     * * * * * * * *

    Note that there are spaces between each star.

    Print and submit copies of each of the parts in files with names Lab7Part1A.java, Lab7Part1B.java, Lab7Part1C.java, and Lab7Part1D.java.
     

    Part II: Vowels, using chars

    Write a program that reads a string from the user, then determines and prints how many of each vowel (a, e, i, o, and u) appear in the string.  Have a separate counter for each vowel.  Also count and print the number of non-vowel characters.  All in all, you'll need at least 7 or 8 variables.  Use the String function charAt() (introduced in Lab 6) to test the type of characters at each location in the String. (Later, when we start to use arrays, you'll see how to make a question like this much easier!)

    Save and print a copy of this program in a file called Vowels.java

     

    Part III: Finding the Mode, stopping values

    Write a program that reads in a sequence of non-negative grades and prints out the "mode" and its frequency.  "Mode" is the technical term for "a value that occurs with maximum frequency".  Note that there can be more than one mode.  In the list 87 92 92 92 98 98 99 99 99 100, both 92 and 99 are modes that occur with the maximum frequency 3--every other value in the list occurs with smaller frequency 1 or 2.  

    For the input,

    ·         Assume the grades are sorted in ascending order (can you see why this is necessary?)

    ·         Assume the sequence is terminated by -1

    ·         If there is more than one mode, it doesn't matter which one the program prints

    A rough outline for your program should be:

    <read first grade>

    <initialize other variables>

    while (  < grade entered is not -1 > ) {

                    <process current grade>:

                                    is current grade same as the previous grade?

                                                    if yes, update frequency of previous grade

                                                    if no, start a new frequency for current value

                                    do we have a new mode?

                                                    update mode

                    <update previous grade>

                    <read next grade>

    }

    <print output>

    You should use the following variables in your program: grade, mode, modeFreq, prev, prevFreq.  (Once you start writing the program you'll realize why these variables are necessary.) Your program should be designed in such a way that it prints an appropriate statement when there is no input for a mode to be determined (i.e., when the user enters in -1 as the very first number).

    Save and submit a copy of this program in a file called Mode.java.
     

    Part IV: Submit Your Lab

    Hand in printouts for

    • Lab7Part1A.java, Lab7Part1B.java, Lab7Part1C.java, Lab7Part1D.java.

    • Vowels.java

    • Mode.java

    As usual, don't forget to put your name, net ID, and date at the top of each file.

    This lab is due Thursday, 19 July 2001 at the beginning of the lab session.  Hand it in to Siddharth as usual.