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:7 | ISBN:9780136091813 | Edition: 2

Question

Write a program that produces the following figure (which vaguely resembles this textbook) 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 10).

 

TextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbook

Answer

Program:

public class Ch02PP07
{
	public static final int SIZE = 10;
	
	public static void main(String[] args)
	{
		drawBoarder1();
		drawLines();		
		drawBoarder2();
		drawBJPs();		
		drawBoarder3();
	}
	
	public static void drawLines()
	{
		for(int i = 1; i <= SIZE; i++)
		{
			drawLine(i);
		}
	}
	
	public static void drawBJPs()
	{
		for(int i = SIZE; i >= 2; i -= 2)
		{
			drawBJP(i);
		}
	}
	
	public static void drawBoarder1()
	{
		for(int i = 1; i <= SIZE + 1; i++)
		{
			System.out.print(" ");
		}
		
		drawBoarder3();	
		System.out.println();
	}
	
	public static void drawBoarder2()
	{
		drawBoarder3();	
		
		for(int i = 1; i <= SIZE; i++)
		{
			System.out.print("/");
		}
		System.out.println();
	}
	
	public static void drawBoarder3()
	{
		System.out.print("+");
		
		for(int i = 1; i <= 3 * SIZE; i++)
		{
			System.out.print("-");
		}
		
		System.out.print("+");
	}
	
	public static void drawLine(int n)
	{
		for(int i = 0; i <= SIZE - n; i++)
		{
			System.out.print(" ");
		}
		
		System.out.print("/");
		
		for(int i = 1; i <= 3 * (SIZE - n); i++)
		{
			System.out.print(" ");
		}
		
		for(int i = 1; i <= n; i++)
		{
			if(i == 1)
				System.out.print("___/");
			else
				System.out.print("__/");
		}
		
		for(int i = 1; i <= n - 1; i++)
		{
			System.out.print("/");
		}
		System.out.println();
	}
	
	
	
	public static void drawBJP(int n)
	{
		System.out.print("|");
		
		for(int i = 1; i <= (3 * SIZE - 22) / 2; i++)
		{
			System.out.print(" ");
		}
		
		System.out.print("Building Java Programs");
		
		for(int i = 1; i <= (3 * SIZE - 22) / 2; i++)
		{
			System.out.print(" ");
		}
		
		if(SIZE % 2 == 1)
		{
			System.out.print(" ");
		}
		
		System.out.print("|");
		
		for(int i = 1; i <= n; i++)
		{
			System.out.print("/");
		}
		
		System.out.println();
	}
}

Output:

 

0 0

Discussions

Post the discussion to improve the above solution.