Difference between revisions of "ASCII Art Examples"

From CompSciWiki
Jump to: navigation, search
Line 55: Line 55:
 
2. The second line starts at position (0,9), then at (1,8), then at (2,7), and so on... Try adding the X and Y values together for each occurrence of a "*" on the grid. You'll notice that the value is always equal to 9. We've figured out the pattern for the second line through the grid!<br \><br \>
 
2. The second line starts at position (0,9), then at (1,8), then at (2,7), and so on... Try adding the X and Y values together for each occurrence of a "*" on the grid. You'll notice that the value is always equal to 9. We've figured out the pattern for the second line through the grid!<br \><br \>
  
We now know that if any of the two conditions above hold while we iterate through our grid, then we'll draw a "*", otherwise we'll draw a " ". Now we just need to figure out how we'll iterate through our grid in code.<br \>
+
We now know that if any of the two conditions above hold while we iterate through our grid, then we'll draw a "*", otherwise we'll draw a " ". Now we just need to figure out how we'll iterate through our grid in code. We're going to use 2 nested for-loops to iterate through our grid. The outer for-loop will represent the current row, and the inner for-loop will represent the current column. The code will look something like this:<br \>
We're going to use 2 nested for-loops to iterate through our grid. The outer for-loop will represent the current row, and the inner for-loop will represent the current column. The code will look something like this:<br \>
+
  
 
<pre>
 
<pre>
Line 70: Line 69:
 
}//end for
 
}//end for
 
</pre>
 
</pre>
 +
 +
The way this works is:<br \>
 +
  - Start at row 0 and column 0<br \>
 +
  - Iterate through columns 0 to 9 (staying at row 0)<br \>
 +
  - Go to the next row (row 1)<br \>
 +
  - Iterate through columns 0 to 9 (staying at row 1)<br \>
 +
  - Repeat until we've iterated through all rows
 +
 
}}
 
}}
  

Revision as of 13:57, 6 April 2010

Back to the Program-A-Day homepage

Problem

In this sample problem, we'll examine how to create a number of different "pictures" using two nested loops and printing characters to the console. These pictures include: a solid box, a solid box with a blank "X", an "X", a hollow box, a hollow box with an "X", and a right triangle.

The program will begin by prompting the user for a number. This number will represent the size for each picture that will be printed to the console. In essence, the size will represent the number of rows and columns for each picture.
NOTE: For each picture in the solutions, we will assume the size (the number of rows and columns) to be 10.

 

SideSectionTitle

float
Taken from http://www.flickr.com/photos/daniello/565304023/

An image or By Students section

Solution

Before we can actually start to think about solving these problems, we'll need to a piece of paper and a pencil. Each picture can be represented by a 2-dimensional grid, so let's draw out a 10 x 10 grid on our piece of paper. It may look something like this:

   0  1  2  3  4  5  6  7  8  9
 +–––––––––––––––––––––––––––––+
0|__|__|__|__|__|__|__|__|__|__|
1|__|__|__|__|__|__|__|__|__|__|
2|__|__|__|__|__|__|__|__|__|__|
3|__|__|__|__|__|__|__|__|__|__|
4|__|__|__|__|__|__|__|__|__|__|
5|__|__|__|__|__|__|__|__|__|__|
6|__|__|__|__|__|__|__|__|__|__|
7|__|__|__|__|__|__|__|__|__|__|
8|__|__|__|__|__|__|__|__|__|__|
9|__|__|__|__|__|__|__|__|__|__|

The best way in attempting to solve these problems is to draw each picture in the grid that you've drawn out for yourself. Let's use the picture of an "X" as an example:

   0  1  2  3  4  5  6  7  8  9
 +–––––––––––––––––––––––––––––+
0|* |__|__|__|__|__|__|__|__|* |
1|__|* |__|__|__|__|__|__|* |__|
2|__|__|* |__|__|__|__|* |__|__|
3|__|__|__|* |__|__|* |__|__|__|
4|__|__|__|__|* |* |__|__|__|__|
5|__|__|__|__|* |* |__|__|__|__|
6|__|__|__|* |__|__|* |__|__|__|
7|__|__|* |__|__|__|__|* |__|__|
8|__|* |__|__|__|__|__|__|* |__|
9|* |__|__|__|__|__|__|__|__|* |

Now that we've drawn out the "X", let's try to figure out the pattern:

1. Start at position (0,0). Look at the X and Y values for this particular line through the grid. Each of the X and Y values are the same! This means that whenever position X is equal to position Y, then we'll draw a "*".

We've figured out the pattern for one of the two lines on the grid, now we need to figure out the pattern for the second line.

2. The second line starts at position (0,9), then at (1,8), then at (2,7), and so on... Try adding the X and Y values together for each occurrence of a "*" on the grid. You'll notice that the value is always equal to 9. We've figured out the pattern for the second line through the grid!

We now know that if any of the two conditions above hold while we iterate through our grid, then we'll draw a "*", otherwise we'll draw a " ". Now we just need to figure out how we'll iterate through our grid in code. We're going to use 2 nested for-loops to iterate through our grid. The outer for-loop will represent the current row, and the inner for-loop will represent the current column. The code will look something like this:

		for (int i = 0; i < size; i++) 
		{
			for (int j = 0; j < size; j++) 
			{

				//code here

			}//end for
			System.out.println(); // print a "return-character" to go to the next row
		}//end for

The way this works is:

 - Start at row 0 and column 0
- Iterate through columns 0 to 9 (staying at row 0)
- Go to the next row (row 1)
- Iterate through columns 0 to 9 (staying at row 1)
- Repeat until we've iterated through all rows

Code

SolutionCode goes here. Please DO NOT put your code in <pre> tags!

Back to the Program-A-Day homepage


import javax.swing.*; // so we can use JOptionPane to get user input

public class ASCII_Art
{
	public static void main(String[] args)
	{
		String input; // what we'll be using for input... we'll parse each input string to an int
		int size; // we'll read this value in from the console
		
		// this command allows the user to enter a number which will determine the size of our pictures
		input = JOptionPane.showInputDialog("Enter a number for the size of each picture",null);
		
		//now we need to parse our input string to an integer
		size = Integer.parseInt(input);

		//solid box
		//we're just printing the same number of '*'s for each row
		for (int i = 0; i < size; i++) 
		{
			for (int j = 0; j < size; j++) 
			{
				System.out.print("*");
			}//end for
			System.out.println();
		}//end for

		System.out.println();

		//solid box w/ an 'X'
		for (int i = 0; i < size; i++)
		{
			for (int j = 0; j < size; j++)
			{
				if (i == j || j ==(size-1)-i)
				{
					System.out.print(" ");
				}//end if
				else 
				{
					System.out.print("*");
				}//end else
			}//end for			
			System.out.println();
		}//end for
		
		System.out.println();

		// an 'X'
		for (int i = 0; i < size; i++)
		{
			for (int j = 0; j < size; j++)
			{
				if (i == j || j ==(size-1)-i)
				{
					System.out.print("*");
				}//end if
				else
				{
					System.out.print(" ");
				}//end else
			}//end for
			System.out.println();
		}//end for

		System.out.println();

		// a hollow box
		for (int i = 0; i < size; i++)
		{
			for (int j = 0; j < size; j++)
			{
				if ((i == 0 || i == (size-1)) || (j == 0 || j == (size-1)) )
				{
					System.out.print("*");
				}//end if
				else 
				{
					System.out.print(" ");
				}//end else
			}//end for
			System.out.println();
		}//end for
		
		System.out.println();

		//Hollow box w/ an 'X'
		for (int i = 0; i < size; i++)
		{
			for (int j = 0; j < size; j++)
			{
				if (i == j || j ==(size-1)-i)
				{
					System.out.print("*");
				}//end if
				else if ((i == 0 || i == (size-1)) || (j == 0 || j == (size-1)) )
				{
					System.out.print("*");
				}//end else if
				else 
				{

					System.out.print(" ");
				}//end else
			}//end for
			System.out.println();
		}//end for

		System.out.println();

		//right triangle
		for (int i = 0; i < size; i++)
		{
			for (int j = 0; j < size; j++)
			{
				if (j <= i)
				{
					System.out.print("*");
				}//end if
				else
				{
					System.out.print(" ");
				}//end else
			}//end for
			System.out.println();
		}//end for
		
		System.out.println("*** END OF PROCESSING. ***");
	}//end main
}//end ASCII_Art