SHOW:
|
|
- or go back to the newest paste.
1 | /********************************************************************** | |
2 | ** Program Name: Scaling Diamond | |
3 | ** Author: Terry Weiss | |
4 | ** Date Written: August 28, 2015 | |
5 | ** Course/Section: CSC 111-003W | |
6 | ** Program Description: | |
7 | ** This program will generate and print a diamond of stars with a | |
8 | ** size provided by the user. | |
9 | **********************************************************************/ | |
10 | ||
11 | import java.util.Scanner; | |
12 | ||
13 | /* | |
14 | * This class will make and display scalable diamond shape. | |
15 | * | |
16 | * Algorithm: | |
17 | * Ask dimensions | |
18 | * Make sure the dimensions are within bounds, or try again | |
19 | * Generate the diamond | |
20 | * Display the diamond shape | |
21 | */ | |
22 | public class Test | |
23 | { | |
24 | ||
25 | /* | |
26 | * This method generates and provides a String with the picture of a | |
27 | * diamond whose width is specified by the user. | |
28 | * | |
29 | * Algorithm: | |
30 | * For each line, draw the number of spaces and then the number of *s and | |
31 | * add it to the picture: | |
32 | * * Generate picture for current line. (currentLine) | |
33 | * * Append picture of line to full picture of diamond | |
34 | * (diamond = diamond + currentLine) | |
35 | * * If it’s an even diamond-like, and this is the longest line, | |
36 | * append a duplicate of the picture to make the final diamond’s total | |
37 | * height match the width | |
38 | * Provide the picture of the diamond (diamond) | |
39 | */ | |
40 | static String makeDiamond( int totalSize ) | |
41 | { | |
42 | /* | |
43 | * This is the workspace that holds the picture of the diamond. All | |
44 | * diamonds will start and end with an empty line so they can be | |
45 | * printed after each other more visibly. | |
46 | */ | |
47 | String diamond = "\n"; | |
48 | ||
49 | /* | |
50 | * This is whether or not the size of the diamond is even or odd, since | |
51 | * they need to be handled differently. Note: a real diamond can’t have | |
52 | * an even height or width and be symmetrical on an ASCII display, so | |
53 | * if an even width is requested, the picture won’t start or end with a | |
54 | * point, and it will have a duplicate largest line. | |
55 | * | |
56 | * The width is even if the total size is divisible by 2 with no | |
57 | * remainder. | |
58 | */ | |
59 | boolean isEven = ((totalSize % 2) == 0); | |
60 | ||
61 | /* | |
62 | * This is the number of the current line we're on so far. | |
63 | */ | |
64 | int lineNumber; | |
65 | ||
66 | /* | |
67 | * This is the picture of the current line we're on in the diamond. | |
68 | */ | |
69 | String currentLine; | |
70 | ||
71 | /* | |
72 | * This is the number of the widest line in the diamond. If the diamond | |
73 | * is even, this line will be added twice. | |
74 | */ | |
75 | int longestLine = (int) Math.ceil(totalSize / 2); | |
76 | ||
77 | ||
78 | /* | |
79 | * Algorithm: | |
80 | * For each line: | |
81 | * * Start with an empty String | |
82 | * * Append leading spaces to line up *s | |
83 | * * | longestLine - lineNumber | | |
84 | * * Calculate the number of *s and append to String to give shape of diamond | |
85 | * * -2 * | longestLine - lineNumber | + totalSize | |
86 | * * If we're on the longest line and it's an even diamond, append the | |
87 | * current line again. | |
88 | * * Finish the line with a newline character | |
89 | * Provide the finished picture | |
90 | */ | |
91 | for( lineNumber = 1; lineNumber <= totalSize; lineNumber++ ) | |
92 | { | |
93 | currentLine = ""; | |
94 | ||
95 | /* | |
96 | * space and star keep track of how many trailing spaces or | |
97 | * stars are being printed on each line. | |
98 | */ | |
99 | int space, star; | |
100 | ||
101 | ||
102 | - | // The number of leading spaces is | longestLine - lineNumber | |
102 | + | |
103 | { | |
104 | currentLine += " "; | |
105 | } | |
106 | ||
107 | for( star = 0; star < (-2 * Math.abs(longestLine - lineNumber + 1) + totalSize); star++ ) | |
108 | - | // The number of *s is -2 * | longestLine - lineNumber | + totalSize |
108 | + | |
109 | - | for( star = 0; star < (-2 * Math.abs(longestLine - lineNumber + 1) + totalSize); |
109 | + | |
110 | - | star++ ) |
110 | + | |
111 | ||
112 | if( isEven && ((lineNumber - 1) == longestLine) ) | |
113 | { | |
114 | currentLine += "\n" + currentLine; | |
115 | - | // Repeat the longest line if that's what we're on |
115 | + | |
116 | ||
117 | currentLine += "\n"; | |
118 | ||
119 | diamond += currentLine; | |
120 | } | |
121 | ||
122 | return diamond; | |
123 | } | |
124 | ||
125 | ||
126 | /* | |
127 | * This program will ask a user for a diamond's size and print out the | |
128 | * diamond. | |
129 | * | |
130 | * Algorithm: | |
131 | * Ask the total size (totalSize) | |
132 | * Make sure the size is a valid number between 1 and 10 or ask again | |
133 | * Generate the diamond with the specified size (makeDiamond(totalSize)) | |
134 | * Display the picture of the diamond | |
135 | */ | |
136 | public static void main( String[] args ) | |
137 | { | |
138 | Scanner user_input = new Scanner(System.in); | |
139 | ||
140 | /* | |
141 | * dimensions is used to capture the user's requested dimensions | |
142 | */ | |
143 | String dimensions; | |
144 | ||
145 | /* | |
146 | * size is used to see how big the dimensions are as a number | |
147 | */ | |
148 | Integer size = 0; | |
149 | ||
150 | ||
151 | // Continue to ask for the dimensions until it's within bounds | |
152 | while( size <= 0 || size > 15 ) | |
153 | { | |
154 | System.out.print("How big would you like your diamond? "); | |
155 | dimensions = user_input.next(); | |
156 | ||
157 | try { | |
158 | size = Integer.parseInt(dimensions); | |
159 | } catch( NumberFormatException e ) { | |
160 | System.out.println("If only we could measure dimensions without" | |
161 | + " whole numbers! Try a number between 1 and 10."); | |
162 | continue; | |
163 | } | |
164 | ||
165 | if( size <= 0 ) | |
166 | { | |
167 | System.out.println("Your negativity is killing my mood! Try " | |
168 | + "something between 1 and 10."); | |
169 | } | |
170 | else if( size > 10 ) | |
171 | { | |
172 | System.out.println("Woah! I love people who Think Big, but " | |
173 | + "that's just TOO big! Try something between 1 and 10."); | |
174 | } | |
175 | } | |
176 | ||
177 | String str = makeDiamond(size); | |
178 | System.out.println(str); | |
179 | } | |
180 | ||
181 | } |