SHARE
SPREAD
HELP

The Tradition of Sharing

Help your friends and juniors by posting answers to the questions that you know. Also post questions that are not available.


To start with, Sr2Jr’s first step is to reduce the expenses related to education. To achieve this goal Sr2Jr organized the textbook’s question and answers. Sr2Jr is community based and need your support to fill the question and answers. The question and answers posted will be available free of cost to all.

 

#
Authors:
Stuart Reges, Marty Stepp
Chapter:
Primitive Data And Definite Loops
Exercise:
Programming Projects
Question:6 | ISBN:9780136091813 | Edition: 2

Question

Write a program that produces the following figure (which vaguely resembles the Seattle Space Needle) as its output. Use a class constant to make it possible to change the size of the figure (the following output uses a size of 4).

TextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbook

Answer

Program:


public class Ch02PP06
{
	public final static int SIZE = 4;

	public static void main(String[] args)
	{
		printNeedle();
		printFirstHalf();
		printSpacer();
		printSecondHalf();
		printNeedle();
		printBody();
		printFirstHalf();
		printSpacer();
	}

	public static void printNeedle()
	{
		for (int i = 1; i <= SIZE; i++)
		{
			for (int j = 1; j <= 3 * SIZE; j++)
			{
				System.out.print(" ");
			}
			
			System.out.println("||");
		}
	}

	public static void printFirstHalf()
	{
		for (int i = 1; i <= SIZE; i++)
		{
			for (int j = 1; j <= SIZE - i; j++)
			{
				System.out.print("   ");
			}
			
			System.out.print("__/");
			
			for (int j = 1; j <= i - 1; j++)
			{
				System.out.print(":::");
			}
			
			System.out.print("||");
			
			for (int j = 1; j <= i - 1; j++)
			{
				System.out.print(":::");
			}
			
			System.out.println("\\__");
		}
	}

	public static void printSpacer()
	{
		System.out.print("|");
		
		for (int i = 1; i <= SIZE; i++)
		{
			System.out.print("\"\"\"\"\"\"");
		}
		
		System.out.println("|");
	}

	public static void printSecondHalf()
	{
		for (int i = 1; i <= SIZE; i++)
		{
			for (int j = 1; j <= 2 * i - 2; j++)
			{
				System.out.print(" ");
			}
			
			System.out.print("\\_");
			
			for (int j = 1; j <= 3 * SIZE - 2 * i + 1; j++)
			{
				System.out.print("/\\");
			}
			
			System.out.println("_/");
		}
	}

	public static void printBody()
	{
		for (int i = 1; i <= SIZE * SIZE; i++)
		{
			for (int j = 1; j <= 3 * SIZE - 3; j++)
			{
				System.out.print(" ");
			}
			
			System.out.println("|%%||%%|");
		}
	}
}

Output:

0 0

Discussions

Post the discussion to improve the above solution.