|
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: Once you get this program working, you may create a second program that does this same thing, but also draws 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 second 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.
If you did the extra credit, also upload your UsernameA12b.java file to Tamarin.
Grading [5 points]
- 1 - Compiles
- Your program compiles successfully (no errors)
- 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; other diagonal should be of /s (+1.5). Slashes should not appear outside the diamond, or anywhere else besides on the diagonals.
FAQs
- Where's some of the demo code from class?
- Here: DivisibleByThree.java and PrintingBoxes.java
- Wow, this is hard! How do I do this?
- First, go through the demo code provided above. Make sure you understand how each for loop works. (That doesn't mean stare glassy-eyed at it for a minute and then run the program to see it print some stuff. You may need to get out a piece of paper and pretend you are a computer. Pick an example value for
num --like 4 or 5. Then trace through the code of each example, 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. (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.
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 = (num / 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 num so your code will handle a triangle of any size. (row and col depend on num, as they very from 1 to num .) So perhaps try your method on paper with a couple triangles of different sizes before writing any code.
|