public class RocketShip {
public static void hourglass(int n) {
// print top half
for (int i = 0; i < n; i++) {
// print spaces
for (int j = 0; j < i; j++) {
System.out.print(" ");
}
// print backslash
System.out.print("\\");
// print dots
for (int j = 0; j < 2 * n - 2 * i - 2; j++) {
System.out.print(".");
}
// print forward slash
System.out.println("/");
}
// print bottom half
for (int i = 0; i < n; i++) {
// print spaces
for (int j = 0; j < n - i - 1; j++) {
System.out.print(" ");
}
// print forward slash
System.out.print("/");
// print dots
for (int j = 0; j < 2 * i; j++) {
System.out.print(".");
}
// print backslash
System.out.println("\\");
}
}
/*
* Method: code
* Usage: code(n);
* ----------------
* Prints the code for the rocket ship with 2 * n - 1 rows.
*/
public static void cone(int n) {
int s = 2 * n - 1;
for (int i = 0; i < s; i++) {
System.out.print(" "); // print spaces
for (int j = 0; j < s - i - 1; j++) { // print spaces
System.out.print(" ");
}
for (int j = 0; j < i + 1; j++) { // print / part of left
System.out.print("/");
}
System.out.print("**"); // print ** part
for (int j = 0; j < i + 1; j++) { // print \ part of right
System.out.print("\\");
}
System.out.println();
}
}
/*
* Method: joint
* Usage: joint(n);
* ----------------
* Prints the joint for the rocket ship
*/
public static void joint(int n) {
System.out.print("+*"); // print +*
for (int i = 0; i < n * 2; i++) { // print =* part
System.out.print("=*");
}
System.out.println("+"); // print +
}
/*
* Method: fuselageUp
* Usage: fuselageUp(n);
* ----------------
* Prints the fuselage for the rocket ship with \/ part
*/
public static void fuselageUp(int n) {
for (int i = 0; i < n; i++) {
System.out.print("|"); // print |
for (int j = 0; j < n - i - 1; j++) { // print . part of left
System.out.print(".");
}
for (int j = 0; j < i + 1; j++) { // print / part of left
System.out.print("/\\");
}
for (int j = 0; j < n - i - 1; j++) { // print . part of middle
System.out.print(".");
}
for (int j = 0; j < n - i - 1; j++) { // print . part of right
System.out.print(".");
}
for (int j = 0; j < i + 1; j++) { // print \ part of right
System.out.print("/\\");
}
for (int j = 0; j < n - i - 1; j++) { // print . part of right
System.out.print(".");
}
System.out.println("|"); // print |
}
}
/*
* Method: fuselageDown
* Usage: fuselageDown(n);
* ----------------
* Prints the fuselage for the rocket ship with /\ part
*/
public static void fuselageDown(int n) {
for (int i = n - 1; i >= 0; i--) {
System.out.print("|"); // print |
for (int j = 0; j < n - i - 1; j++) { // print . part of left
System.out.print(".");
}
for (int j = 0; j < i + 1; j++) { // print / part of left
System.out.print("\\/");
}
for (int j = 0; j < n - i - 1; j++) { // print . part of middle
System.out.print(".");
}
for (int j = 0; j < n - i - 1; j++) { // print . part of right
System.out.print(".");
}
for (int j = 0; j < i + 1; j++) { // print / part of right
System.out.print("\\/");
}
for (int j = 0; j < n - i - 1; j++) { // print . part of right
System.out.print(".");
}
System.out.println("|"); // print |
}
}
/*
* Method: rocket
* Usage: rocket(n);
* ----------------
* Prints the rocket ship using the methods above.
*/
public static void completeRocket(int n) {
// print top code
cone(n);
joint(n);
// print fuselage
fuselageUp(n);
fuselageDown(n);
joint(n);
// second reversed fuselage
fuselageDown(n);
fuselageUp(n);
joint(n);
cone(n);
}
// Sample
public static void main(String[] args) {
completeRocket(5); // print the complete rocket
System.out.println("\n\n");
hourglass(5); // Ascii art of own choice
}
}
This assignment will give you practice with for loops, static methods, print/println statements and a class constant.
Rocket Ship (20 points):