Assignment 01

Task

Write a program that prints a octagon of user-specified size.

Text: Java Tutorial -> Learning the Java Language -> Language Basics and/or Appendix A in the textbook
Concepts: variables, conditionals, loops, exceptions, user input.

Steps

Your program should be text-based (no GUIs). Ask the user to enter a size. This should be an integer between 1 and 12. If the user enters a String or an invalid integer, your program should give an error message and then keep asking the user until they enter something valid. (If you want to, you may also accept 0 as a valid input, which means print nothing and quit.)

Then print an octagon of # characters on the screen. (You may also print spaces and newlines, but print no other characters on the same lines as the #s that make up the octagon.) The horizontal and vertical sides of the octagon should have a width equal to the number entered by the user. The full width and height of the octagon should be equal to 3 times the number entered by the user.

Because printed characters are usually taller than they are wide, the octagon might look a little stretched.

Sample Output

The following is taken from the command line and shows me running the program 5 separate times. The grey line is my prompt, not part of the program output. User input is shown in green.

A01> java ZtomaszeA01
This program draws an octagon.
Please enter a size as an integer (1 - 12): 1

 #
###
 #

A01> java ZtomaszeA01
This program draws an octagon.
Please enter a size as an integer (1 - 12): 2

  ##
 ####
######
######
 ####
  ##

A01> java ZtomaszeA01
This program draws an octagon.
Please enter a size as an integer (1 - 12): 3

   ###
  #####
 #######
#########
#########
#########
 #######
  #####
   ###

A01> java ZtomaszeA01
This program draws an octagon.
Please enter a size as an integer (1 - 12): 4

    ####
   ######
  ########
 ##########
############
############
############
############
 ##########
  ########
   ######
    ####

A01> java ZtomaszeA01
This program draws an octagon.
Please enter a size as an integer (1 - 12): 13
The size must be between 1 and 12, inclusive.  Try again.
Please enter a size as an integer (1 - 12): five
Sorry, but that is not an integer.  Try again.
Please enter a size as an integer (1 - 12): 5.5
Sorry, but that is not an integer.  Try again.
Please enter a size as an integer (1 - 12): lots and lots
Sorry, but that is not an integer.  Try again.
Please enter a size as an integer (1 - 12): 5

     #####
    #######
   #########
  ###########
 #############
###############
###############
###############
###############
###############
 #############
  ###########
   #########
    #######
     #####

What to Submit

Name your class UsernameA01 in a file named UsernameA01.java. Your class and its main method must both be public. Do not use any packages. Upload your UsernameA01.java file to the Tamarin for your section.

Remember to follow the course coding standards on all assignments.

Grading [50 points]

5 - Compiles
18 - User input
Continues to prompt the user until she enters an integer in the allowed range. Provides an appropriate error message on any bad input.
27 - Prints the octagon correctly
Correct size and shape and formed only of #, space, and newline characters. Use one or more loops to draw the shape.

Hints and Suggestions

Before you start, sit down with a piece of paper and determine the relationships between the number of spaces and the number of #s you'll need to print on each line (row). For example, look at an octagon of size 4:

    ####
   ######
  ########
 ##########
############
############
############
############
 ##########
  ########
   ######
    ####

If you divide this into a 9x9 tic-tac-toe grid, each quadrant is made up of 4x4 (that is, size x size) squares:

    |####|
   #|####|#
  ##|####|##
 ###|####|###
----+----+----
####|####|####
####|####|####
####|####|####
####|####|####
----+----+----
 ###|####|###
  ##|####|##
   #|####|#
    |####|

This suggests some different possible ways to print the whole shape. One way might to print the entire shape with 2 nested for loops, using a series of if/else-if/else statements inside to decide whether to print spaces or #s.

Another approach might be to break the octagon into three parts (each with a number of rows equal to the user-given size) and print them separately:

              no. of spaces  row   no. of #s
....####       size - 0       0      size
...######      size - 1       1      size + 2
..########     size - 2       2      size + 4
.##########    size - 3       3      size + 6
------------
############   size * 3
############   size * 3
############   size * 3
############   size * 3
------------
.##########    size - 3       ?      ?
..########     size - 2       ?      ?
...######      size - 1       ?      ?
....####       size - 0       ?      ?

You may be tempted to use a 2D array here. That's fine, but it probably won't do much to simplify the logic required.

Regardless of the technique you use, you will need to puzzle through these relationships to determine which character to print at any given (row, col) coordinate within the octagon.