Back to 111 Main Page

Mobius strip

Assignment 12

Task

Write a program that prints a textual diamond of user-specified size to the screen.

Concepts: nested loops and conditionals.

Steps

Ask the user to enter an odd integer between 1 and 79. (If the user does not, you do not need to keep prompting them; you may just gracefully exit the program.)

Then, draw an equilateral diamond of *s, where the width and height are both equal to the number given by the user. Use one or more loops to do this.

Extra credit: Draw a giant X across the center of the diamond. To do this, draw an 'X' in the center point and use '\' and '/'s on the diagonals. (See the sample output below for what this should look like.)

Sample Output

D:\TA\grading\A12>java ZtomaszeA12
Enter the size of a diamond to draw (an odd number between 1 and 79): 10
Size must be an odd integer between 1 and 79.
Please try again.

D:\TA\grading\A12>java ZtomaszeA12
Enter the size of a diamond to draw (an odd number between 1 and 79): 1
*

D:\TA\grading\A12>java ZtomaszeA12
Enter the size of a diamond to draw (an odd number between 1 and 79): 3
 *
***
 *

D:\TA\grading\A12>java ZtomaszeA12
Enter the size of a diamond to draw (an odd number between 1 and 79): 7
   *
  ***
 *****
*******
 *****
  ***
   *

D:\TA\grading\A12>java ZtomaszeA12
Enter the size of a diamond to draw (an odd number between 1 and 79): 27
             *
            ***
           *****
          *******
         *********
        ***********
       *************
      ***************
     *****************
    *******************
   *********************
  ***********************
 *************************
***************************
 *************************
  ***********************
   *********************
    *******************
     *****************
      ***************
       *************
        ***********
         *********
          *******
           *****
            ***
             *

If you do the extra credit, your program will now look like this:

D:\TA\grading\A12>java ZtomaszeA12b
Enter the size of a diamond to draw (an odd number between 1 and 79): 3
 *
*X*
 *

D:\TA\grading\A12>java ZtomaszeA12b
Enter the size of a diamond to draw (an odd number between 1 and 79): 7
   *
  ***
 *\*/*
***X***
 */*\*
  ***
   *

D:\TA\grading\A12>java ZtomaszeA12b
Enter the size of a diamond to draw (an odd number between 1 and 79): 17
        *
       ***
      *****
     *******
    \*******/
   **\*****/**
  ****\***/****
 ******\*/******
********X********
 ******/*\******
  ****/***\****
   **/*****\**
    /*******\
     *******
      *****
       ***
        *

What to Submit

Upload your UsernameA12.java file to Tamarin.

Grading [5 points]

1 - Compiles
Your program compiles successfully (no errors) and follows coding standards.
1 - Input
Reads in the size from the user; prints an error message if it is not an odd integer between 1 and 79 (inclusive).
3 - Prints diamond
Shape should be composed of * characters, with a width and height equal to the user-given value (1.0). Shape should be an equilateral diamond, as demonstrated above, drawn using one or more loops (2.0).
+2 - Extra credit
Extra credit program must do all of the above or no extra credit will be given. In addition, center character of diamond should be an X (rather than a *). (+0.5). Top-left to bottom-right diagonal should be formed of '\'s; the other diagonal should be of '/'s (+1.5). Slashes should not appear outside the diamond, or anywhere else besides on the diagonals.

FAQs

Problem set from lab
Practice for students (individually or in small groups) in lab. Use one or more loops to do the following:
  1. Print all the positive integers less than 100 that are divisible by 3. (1B: Once you have then done, then print those number 5 per line.)
Given a user-specified width:
  1. Print a line of "width" asterisks (*). That is, if the user enters 3, print 3 asterisks.
  2. Print a square box of *s with the given width (and height) That is, if the user enters 3, print:
     ***
     ***
     ***
    
  3. Print a box with a hole in its center. That is, print a box as in #3 but with the single * in the center (or one close to the center for boxes with an even width) replaced with a space.
  4. Print an equilateral triangle of *s with the given width. The right angle should be on the lower left. So, given a width of 4:
     *
     **
     ***
     ****
    
  5. Print an equilateral triangle of *s with the given width. The right angle should be on the lower right. So, given a width of 4:
        *
       **
      ***
     ****
    
Challenge:
  • Print an equilateral triangle of *s with the given width. The right angle should be on the upper left. So, given a width of 4:
     ****
     ***
     **
     *
    
  • Print an equilateral triangle of *s with the given width. The right angle should be on the upper right. So, given a width of 4:
     ****
      ***
       **
        *
    
  • #4 above looks bad for boxes of even width. Fix this so that there is a hole of 4 spaces in the center of a large even-width box.
  • Print a 12x12 (or any other size) multiplication table, properly spaced:
      0   1   2   3   4 ..  11  12
      1   1   2   3   4     11  12
      2   2   4   6   8     22  24
      3   3   6   9  12     33  36
      .                          .
      .                          .
      .                          .
     11  11  22  33  44 .. 121 132
     12  12  24  36  48 .. 132 144
    
Sample solutions: DivisibleByThree.java (#1 and 1B) and PrintingBoxes.java (#2 to 6).
Wow, this is hard! How do I do this?
First, work on the problem set above.

If these give you trouble, look at the sample solutions (once posted). Make sure you understand how each for loop works. (That doesn't mean stare glassy-eyed at it for a minute and then maybe run the program to see it prints some stuff. For those you don't understand, you may need to get out a piece of paper and pretend you are a computer. Pick an example value for width--like 3 or 4. Then trace through the code of each example like I do on the board in lab: line-by-line, drawing a box for each variable declared and tracking how its value changes and what gets printed to the screen.)

Once you understand how these examples work, it'll be a lot easier to write your own solution. Before you write any code, get out another piece of paper and draw what a sample diamond should look like. Pick a size that's large enough for you to see the patterns involved--maybe 9 or 11. As demonstrated in the comments of PrintingBoxes.java, note how many spaces and then how many *s are printed on each line. You might also consider the center/midpoint of a row or column. (What happens if you divide the size by 2? And then perhaps add or subtract 1? Does that give you any important/useful numbers? Similarily, how is the number of *s printed related to the row number?)

It is possible to print the whole diamond with 2 nested for loops and a few tricky if/else-if/else statements. However, feel free to break the problem down. Perhaps it's easier to handle only the top half of the diamond first, then the center row, and then the bottom half. Similarly, for each row, you may want one for loop to print the spaces and another to print all the *s.

Once it's clear in your head, only then do you start writing code. If you find you're starting to flail around while you write your code, wildly trying different things in the hopes they work, it'd be a better idea to pause and go back to your piece of paper to plan some more. (If you have time, you might also need a break for a little while. Sleeping on it often helps.)

As an example, here's one way to look at a diamond of size 11:
              row   pre-spaces  stars  (row * 2 - 1)  mid = (width / 2) + 1
     *         1        5         1          1            6
    ***        2        4         3          3            6
   *****       3        3         5          5            6
  *******      4        2         7          7            6
 *********     5        1         9          9            6
***********    6        0         11         11           6
 *********     7        1         9          13           6
  *******      8        2         7
   *****       9        3         5
    ***        10       4         3
     *         11       5         1
Perhaps you can spot some relationships here that are constant, at least until the midpoint of the diamond. So perhaps print the top half of the diamond only (a big triangle) and then tackle the bottom half (another inverted triangle) separately.

Remember you need to find relationships that depend only on width so your code will handle a triangle of any size. (row and col depend on width, as they very from 1 to width.) So perhaps try your method on paper with a couple other diamonds of different sizes before writing any code.


~ztomasze Index : TA Details: ICS111: A12
http://www2.hawaii.edu/~ztomasze
Last Edited: 08 Oct 2009
©2008 by Z. Tomaszewski.