Difference between revisions of "Break This Combination"

From CompSciWiki
Jump to: navigation, search
Line 15: Line 15:
 
An image or By Students section
 
An image or By Students section
  
|Solution=Write solution here...
+
|Solution=Write solution here...<br \><br \>
 +
 
 +
<pre>
 +
code snippet
 +
</pre>
 +
 
 +
<br \><br \>
  
 
}}
 
}}
Line 86: Line 92:
 
}//end test
 
}//end test
  
<pre>
+
</pre>

Revision as of 19:15, 5 April 2010

Back to the Program-A-Day homepage

Problem

In this sample problem, we will design a program that can determine the sequence of a combination lock, and keep track of the number of attempts until the combination is broken.

The program will begin by prompting the user to configure their combination lock. The user will then enter in THREE numbers ranging from 0 to NUM_POSSIBILITIES (in this example, we will assume NUM_POSSIBILITIES to be 59, but you may change this within the source code to a larger or smaller number if you like). We will store each number within an integer array of size 3 since there are three numbers we need to select in sequence in order to break the lock.

After the lock has been configured, the program will then proceed in attempting to break the lock. After every attempt, the program should increment a counter, num_attempts, until the lock has been broken.

 

SideSectionTitle

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

An image or By Students section

Solution

Write solution here...

code snippet



Code

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

Back to the Program-A-Day homepage



import java.util.Scanner; // we'll need to import this so we can type in stuff with the keyboard


public class Combination 
{
	public static void main(String[] args)
	{
		final int NUM_POSSIBILITIES = 59; // you can change this to any size you like
		final int NUM_DIALS = 3;
		
		int[] combo = new int[3]; // the combination lock
		boolean found = false; // so we know when to stop...
				
		int num_attempts = 0; // we'll keep track of the number of attempts
		
		
		Scanner in = new Scanner(System.in); // create a new scanner so we can get user input
		
		// read in combination
		for (int i = 0; i < NUM_DIALS; i++)
		{
			System.out.println("Enter a number from 0 to " + 
				NUM_POSSIBILITIES + " for the combination: " ); 
									
			// this command allows the user to enter a number
			// and assign it to a spot in our int array
			combo[i] = in.nextInt();
		}//end for
		
		in.close(); // close the scanner since we won't be using it anymore
		
		// try to break the combo
		// we'll use a loop for each "dial" on the lock
		for (int i = 0; i <= NUM_POSSIBILITIES && !found; i++)
		{			
			for (int j = 0; j <= NUM_POSSIBILITIES && !found; j++)
			{				
				for (int k = 0; k <= NUM_POSSIBILITIES && !found; k++)
				{
					num_attempts++; // increment the number of attempts
					
					// if we have the right sequence of numbers, then we've broken
					// the lock!
					if (i == combo[0] && j == combo[1] && k == combo[2])
					{
						System.out.println("*** Combination broken ***");
						System.out.println("Sequence: [" + i + "][" + j + "][" + k + "]");
						System.out.println("Attempts: " + num_attempts);
						found = true;
					}//end if				
				}//end for
			}//end for
		}//end for
		
		// if we didn't find the sequence, then the user probably entered
		// a number outside of the specified ranger
		if (!found)
		{
			System.out.println("*** Could not break combination. ***");
		}//end if
		
		System.out.println("*** END OF PROCESSING. ***");
	}//end main
}//end test