CS 12300 - Programming Assignment 5

This assignment is due Wednesday, October 25.

This assignment uses the material from Sections 4.1 and 4.2. This assignment makes use of the files contained in this zip file.

In a file Hw5.java write a public static method called printFrame that takes two integer parameters. This method should print a "frame" in the console window consisting of two squares, one centered inside of the other, with the corners of the squares connected with 45 degree "lines". The dimensions of the two squares are given by the two integer parameters (the larger parameter is the outer dimension and the smaller parameter is the inner dimension). Below are a few examples. If the difference between the two parameters is not an even integer, your method should decrease the smaller parameter by 1 and use that new value. If the two parameters are equal, your method should draw only the outer frame. If the smaller parameter is zero or negative (including after it has been adjusted), your method should return without printing anything.

The method call printFrame(20,12) should produce the following output in the console window. (Notice that these really are "squares" in that they have the same number of characters in their horizontal and vertical sides.)

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

The method call printFrame(6,24) should produce the following output. Notice that the larger of the two input integers should be the dimension of the outer "square".

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

The method call printFrame(21,4), or the method call printFrame(4,21), should produce the following output. (Notice that this is the same output that would be produced by the method call printFrame(21,3) since 21 - 4 = 17 is an odd number.)

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

The method call printFrame(9,1) should produce the following output.

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

Also, write a main method that tests your printFrame method. You should call printFrame with enough inputs to be sure you have meet all the requirements. Print a blank line after the frame from each call to printFrame.

Work on this assignment in steps. First, just have your printFrame method figure out which of its two parameters is the larger one and print just a "square" box of that size. When that works, have your method print the larger "square" with the smaller "square" centered inside of it but without the diagonal lines. When that works, add the diagonal lines. Notice that the picture with the diagonal lines has seven "regions" of horizontal lines,

  1. the top line,
  2. the lines with the diagonals between the top corners of the two boxes,
  3. the line that contains the top of the inner box,
  4. the lines with the walls of the inner box
  5. the line that contains the bottom of the inner box,
  6. the lines with the diagonals between the bottom corners of the two boxes,
  7. the bottom line.

You can organize your code around these seven regions.

Here is another way to think about this problem. If the input parameters are m and n, with m > n, then your method is printing out an m rows by m columns grid of characters, where each character in the grid is either a space or *. It is not too hard to write a boolean expression using numbers i and j that is true when the character in the grid with coordinates (i,j) is * (and false when the character with coordinates (i,j) is a space). This way of thinking about the problem leads to a very short solution, just two nested for-loops with a kind of messy if-statement inside.

Turn in a zip file called CS123Hw5Surname.zip (where Surname is your last name) containing your version of Hw5.java.

This assignment is due Wednesday, October 25.