You are going to generate output that looks like the following: Instructions You
ID: 3878656 • Letter: Y
Question
You are going to generate output that looks like the following:
Instructions
You are to write a program that can produces an ASCII Art Rocket Ship.
You should use nested loops to reduce repeating code for specific characters on a given line.
You should use static methods to structure your solution in order to reduce repeating code for repetitious subfigures in the rocket ship.
You are required to properly indent your code and will lose points if you make indentation mistakes.
You should begin by trying to reproduce the figure above exactly. Once you can reproduce this rocket ship, you should introduce a class constant to your code to make your solution more flexible in order to allow rocket ships of different sizes to be produced.
Explanation / Answer
public class CharacterArt {
public static String makeACenteredTriangle(int height, int middleWidth, int sideBufferExtra) {
StringBuilder builder = new StringBuilder();
for (int row = 1; row <= height; row++) {
//Left buffer
for (int b = 1; b <= height - row + sideBufferExtra; b++)
builder.append(' ');
//Left slashes
for (int slash = 1; slash <= row; slash++)
builder.append('/');
//Middle column
for (int mid = 1; mid <= middleWidth; mid++)
builder.append('!');
//Right slashes
for (int slash = 1; slash <= row; slash++)
builder.append('\');
//Right buffer
for (int b = 1; b <= height - row + sideBufferExtra; b++)
builder.append(' ');
builder.append(' ');
}
return builder.toString();
}
public static String makeADiamondsStrip(int sideLength, int numberDiamonds, int rowNumber) {
StringBuilder builder = new StringBuilder();
//For the number of diamonds
for (int number = 1; number <= numberDiamonds; number++) {
//Left buffering
if (rowNumber <= sideLength)
for (int b = 1; b <= sideLength - rowNumber; b++) builder.append('o');
else
for (int b = 1; b <= rowNumber - sideLength - 1; b++) builder.append('o');
//Slashes
if (rowNumber <= sideLength)
for (int s = 1; s <= rowNumber; s++)
builder.append("/\");
else
for (int s = 1; s <= rowNumber - 2 * (rowNumber - sideLength) + 1; s++)
builder.append("\/");
//Right buffering
if (rowNumber <= sideLength)
for (int b = 1; b <= sideLength - rowNumber; b++) builder.append('o');
else
for (int b = 1; b <= rowNumber - sideLength - 1; b++) builder.append('o');
}
return builder.toString();
}
public static String makeADivider(int totalWidth, int middleWidth, int sideWidth) {
StringBuilder builder = new StringBuilder();
int remainingEachSide = (totalWidth - middleWidth - 2 * sideWidth) / 2;
for (int i = 0; i < sideWidth; i++) builder.append('+');
//Left
for (int left = 1; left <= remainingEachSide; left++)
builder.append(left % 2 == 1 ? '=' : '*');
//Middle
for (int middle = 1; middle <= middleWidth; middle++) builder.append('*');
//Right
for (int right = 1; right <= remainingEachSide; right++)
builder.append(right % 2 == 1 ? '=' : '*');
for (int i = 0; i < sideWidth; i++) builder.append('+');
return builder.toString();
}
public static void main(String[] args) {
/* Initialise our StringBuilder. */
StringBuilder builder = new StringBuilder();
/* Append the first triangle to the top, with a height of 5, middle column width of 2 and a side-padding of 1. */
builder.append(makeACenteredTriangle(5, 2, 1));
/* Append the first divider, with a width of 14 having a total middle column width of 2, and 1 '+' at each end. */
builder.append(makeADivider(14, 2, 1)).append(' ');
for (int i = 1; i <= 6; i++)
builder.append('|').append(makeADiamondsStrip(3, 2, i)).append('|').append(' ');
/* Append another divider, the same as the last. */
builder.append(makeADivider(14, 2, 1)).append(' ');
/* Create the next section of the body, this time with the bottom half then the top half of the diamonds; in that order. */
for (int i = 4; i <= 6; i++)
builder.append('|').append(makeADiamondsStrip(3, 2, i)).append('|').append(' ');
for (int i = 1; i <= 3; i++)
builder.append('|').append(makeADiamondsStrip(3, 2, i)).append('|').append(' ');
/* Append the last divider. */
builder.append(makeADivider(14, 2, 1)).append(' ');
/* Append another triangle, this one being the same as the first. */
builder.append(makeACenteredTriangle(5, 2, 1));
/* Print out the final ASCII art. */
System.out.println(builder.toString());
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.