/**!
********************************************************************************
* name - {###########}
* date - 2022-10-12
* brief - DrawRocket class contains methods that print different ascii patterns
* such as ring, cone and triangles.
*
* the functions implemented in the file is idiolized by the
* AP Computer Science Project 2 – Rocket Ship Doc File.
********************************************************************************
*/
public class DrawRocket {
/**
* This is a helper method to reduce redundancy in the functions in this class.
* name repeatStore means store a repeating value in a variable.
* @param value - the value that repeats int the line
* @param len - length of repetition of the value
* @return String which is value*len.
*/
public static String repeatStore( String value, int len){
String store = ""; /**variable to store the final repeating value. */
/**this loop adds the repeating value to the store variable len times. */
for(int j = 1; j<= len ; j++){
store+=value;
}
return store;
}
/**
*******************************
* First Part Starts Here
********************************
*/
/** this function prints a custom(hardcoded) Ascii Drawing.
* printing a pyramid with help of (*)
* the height of the pyramid is size (created locally for this part inside the method)
* at ith line number of stars = (2*i - 1)
*/
public static void asciiDrawing(){
int size = 12; //this size variable decides the height of the pyramid.
/**
* line - String to store and print ith line of the cone
* spaces - holds the number of spaces appearing before the cone stars printing.
* stars - stores the (*) x (i-1) stars for the ith line of the pyramid;
*/
String line = "", spaces = "", stars = "";
for(int i = 1; i<= size; i++){
/**get the size-i wide space in spaces variable. */
spaces = repeatStore(" ", size-i);
/**get the (*) x (i-1) stars in stars variable variable. */
stars = repeatStore("*", i-1);
/** adding the initial spaces to the line */
line = spaces;
/** since stars variable holds i-1 stars so below line adds (2*i-1) require stars in ith line*/
line+=stars+"*"+stars;
/**print the ith line to the console. */
System.out.println(line);
}
}
/**
*******************************
* Second Part Starts Here
*******************************
*/
/**
* The size of the Rocket as described in the doc.
* Change the SIZE value here to produce rocket of different sizes
* keep SIZE >= 1 ;
*/
private static final int SIZE = 3;
/**
* This method prints the cone structured ascii art.
* The height of the cone (number of lines in the cone) will be 2*SIZE-1
* @return Nothing
*/
public static void cone(){
int n = 2*SIZE ;
/**
* line - String to store and print ith line of the cone
* spaces - holds the number of spaces appearing before the cone starts printing.
* fs - Forward Slash (fs) : i number of forward slash in ith line to print on console
* bs - Backward Slash (bs) : i number of backward slash in the ith line to print on the console.
*/
String spaces = "", fs ="",bs="", line = "";
for(int i =1;i <= n-1; i++){
/**get the n-i wide space in spaces variable. */
spaces = repeatStore(" ", n-i);
/**get the (/) x (i) forward slash in fs variable. */
fs = repeatStore( "/", i);
/**get the (\) x (i) backward slash in fs variable. */
bs = repeatStore("\\", i);
/** adding the initial spaces to the line */
line = spaces;
/** putting all the charactes in the sequence in the ith line. */
line+= fs+"**"+bs;
/**print the ith line to the console. */
System.out.println(line);
}
}
/**
* Ring function prints an single line ascii pattern using +, = , and *
* Size of the ring = 2* SIZE
* number of stars in the ring will always be even
* @return Nothing
*/
public static void ring(){
/** in the ring (=+) appears 2*SIZS times hence adding the (=*) in the line */
String line = repeatStore("=*", 2*SIZE);
/** adding the + character at the begining and the end of the line printing it to the console. */
System.out.println("+"+line+"+");
}
/**
* TriangleUP function prints two verticle triaglular shaped ascii art with dot(.) filled in the blank spaces
* Characters used = (., / ,\ , and |)
* @return Nothing
*/
public static void triangleUp(){
/**
* line - String to store and print ith line of the cone
* dots - holds the dots appearing in one side of traingle on ith line.
* traingles - holds the number of triangles(/\) appearing in ith line of the pattern
*/
String line , traingles, dots ="";
for(int i = 1; i<= SIZE;i++){
/**get the (.) x (SIZE - i) dots in dots variable. */
dots = repeatStore(".",SIZE - i);
/**get the (/\) x (i) traingles in traingles variable. */
traingles = repeatStore("/\\",i);
/** putting all the charactes in the sequence in the ith line. */
line =dots + traingles + dots;
line+=line; /**this lines adds the symmetric traingle side ways to the current one */
/**print the ith line to the console. */
System.out.println("|"+line+"|");
}
}
/**
* This function prints the vertical mirror image (inverted down triagle)
* of the triangleUp function
* @return Nothing
*/
public static void triangleDown(){
/** the local variables here signify the same meaning as in function triangleUp */
String line , triangles, dots ="";
for(int i = 1; i<= SIZE;i++){
/**get the (.) x (i - 1) dots in dots variable. */
dots = repeatStore(".", i-1);
/**get the (\/) x (SIZE-i+1) traingles in traingles variable. */
triangles = repeatStore("\\/", SIZE-i+1);
/** putting all the charactes in the sequence in the ith line. */
line = dots + triangles + dots;
line+=line; /**this lines adds the symmetric traingle side ways to the current one */
/**print the ith line to the console. */
System.out.println("|"+line+"|");
}
}
/**
* This is the main method
* which makes use of cone, ring, triangleUp and triangleDonw functions to print the rocket.
* this function also calls the asciiDrawing function to print a custom ascii drawing of a doll.
* @param args Unused.
* @return Nothing.
*/
public static void main(String[] args){
/**First Part - Printing a custom ascii drawing which is being called */
asciiDrawing();
System.out.println();
/**Second Part - printing the rocket drawing */
cone();
ring();
triangleUp();
triangleDown();
ring();
triangleDown();
triangleUp();
ring();
cone();
}
}
In the second part of the assignment, you are going to generate output that looks like the following: