Difference between revisions of "Break This Combination"

From CompSciWiki
Jump to: navigation, search
 
(21 intermediate revisions by 4 users not shown)
Line 7: Line 7:
 
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.<br \><br \>
 
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.<br \><br \>
  
Your input and output should look something like this: <br \>
+
 
<pre>
+
After you have configured the lock, your output should look something like this: <br \>
Enter a number from 0 to 59 for the combination:
+
{{CodeBlock
44
+
|Code=
Enter a number from 0 to 59 for the combination:
+
Breaking combination...
21
+
Enter a number from 0 to 59 for the combination:
+
12
+
 
*** Combination broken ***
 
*** Combination broken ***
 
Sequence: [44][21][12]
 
Sequence: [44][21][12]
 
Attempts: 159673
 
Attempts: 159673
 
*** END OF PROCESSING. ***
 
*** END OF PROCESSING. ***
</pre>
+
}}
  
  
 +
|SideSectionTitle=Problem Solving and Programming Examples
 
|SideSection=
 
|SideSection=
[[Image:OperatingSystemExample.jpg|float|267px]]
+
[[Image:Wiki_bench01.jpg|center]]<BR>
<BR>
+
Taken from http://www.flickr.com/photos/daniello/565304023/
+
 
+
An image or By Students section
+
  
 
|Solution=
 
|Solution=
  
Before we start thinking about the solution, we need to declare all of our variables that we'll need in order to arrive at a solution. We'll declare two constants, the first will be the number of possibilities, NUM_POSSIBILITIES, for each dial on the lock. This can be any positive number. The higher the number, the harder the lock will be to break. The second constant will be the number of dials on the lock (we're using 3 dials in this problem) which we'll call NUM_DIALS. We'll also need an array of size 3 called "combo" to represent our combination lock, a boolean value called "found" that we can set to true once we find the correct sequence (so we don't keep trying to break the lock after it's already been broken), and an integer called "num_attempts" that we'll use to count the number of attempts before we actually break our lock.<br \>
+
To solve the problem you have to what you need to solve this problem.<br>
 +
Since you have a constant number like possibilites and dials, they have to be declared final. You have to make a Scanner to get the user's input. Then next step is to use an array to store the combination that the user is going to input. This program isn't errorproof, there are no validation included so you have to assume the user will enter valid input, in this case will only enter numeric input.
 +
{{CodeBlock
 +
|Code=
 +
final int NUM_POSSIBILITIES = 59; // you can change this to any size you like
 +
final int NUM_DIALS = 3;
  
<pre>
+
Scanner keyboard = new Scanner(System.in);     //will get the input number
final int NUM_POSSIBILITIES = 59; // you can change this to any size you like
+
String input; // what we'll be using for input... we'll parse each input string to an int
final int NUM_DIALS = 3;
+
int[] combo = new int[3]; // the combination lock
 +
boolean found = false; // so we know when to stop...
 
 
int[] combo = new int[NUM_DIALS]; // the combination lock
+
int num_attempts = 0; // we'll keep track of the number of attempts
boolean found = false; // so we know when to stop...
+
}}
+
int num_attempts = 0; // we'll keep track of the number of attempts
+
</pre>
+
  
We will then need to read in three numbers from the user, where each number represents the correct number to select in sequence to unlock the combination. For example, if the user enters "25", "12", "55", then the correct sequence to break the combination lock is 25-12-55. Also, Since we'll be using an array of size 3, well assign each number to their correct position in the array (eg. [25,12,55]). We'll accomplish this using a loop that will iterate over three times, and read an integer from the console:<br \>
 
  
<pre>
+
We will then need to read in three numbers from the user, where each number represents the correct number to select in sequence to unlock the combination. For example, if the user enters "25", "12", "55", then the correct sequence to break the combination lock is 25-12-55. Also, Since we'll be using an array of size 3, we'll assign each number to their correct position in the array (eg. [25,12,55]). We'll accomplish this using a loop that will iterate over three times, and read an integer from a JOptionPane window.<br \>
+
{{note}}<b> Don't forget to parse your input String to an int! Since the array is of type int, we can't assign String values to it.</b><br\>
Scanner in = new Scanner(System.in); // create a new "scanner" so we can get user input from the console
+
{{CodeBlock
 +
|Code=
 +
// read in combination
 +
for (int i = 0; i < NUM_DIALS; i++)
 +
{
 +
// this command allows the user to enter a number
 +
        System.out.print("Enter a number from 0 to " + NUM_POSSIBILITIES +
 +
" for the combination: ")
 +
input = keyboard.next();
 +
 +
// now assign it to a spot in our int array
 +
combo[i] = Integer.parseInt(input);
 +
}//end for
 +
}}
 +
{{OutputBlock
 +
|Code=
 +
Enter a number from 0 to 59 for the combination: 5
 +
Enter a number from 0 to 59 for the combination: 10
 +
Enter a number from 0 to 59 for the combination: 45
 +
}}
  
// read in combination
+
 
for (int i = 0; i < NUM_DIALS; i++)
+
To approach the solution, we need to think about how we will simulate each dial on the lock. Since there are three dials, we can use three nested for-loops, each simulating one dial. Each loop will iterate 0 to NUM_POSSIBILITIES, but will stop iterating as soon as we've found the correct lock sequence. We'll need another condition specified in each loop to achieve this.<br \>
 +
<b>NOTE: Typing "!found" is the same as typing "found == false". We usually read it as "while not found".</b><br \>
 +
{{CodeBlock
 +
|Code=
 +
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++)
 
{
 
{
System.out.println("Enter a number from 0 to " +  
+
num_attempts++; // increment the number of attempts
NUM_POSSIBILITIES + " for the combination: " );  
+
// more code here
+
// 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
 
}//end for
 +
}//end for
 +
}//end for
 +
}}
  
</pre>
 
 
To approach the solution, we need to think about how we will simulate each dial on the lock. Since there are three dials, we can use three nested for-loops, each simulating one dial. Each loop will iterate 0 to NUM_POSSIBILITIES, but will stop iterating as soon as we've found the correct lock sequence. We'll need another condition specified in each loop to achieve this. NOTE: Typing "!found" is the same as typing "found == false". We usually read it as "while not found".<br \>
 
 
<pre>
 
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
 
 
// more code here
 
 
}//end for
 
}//end for
 
}//end for
 
</pre>
 
  
 
Finally, all we need to do in order to check if our current sequence matches the combination lock's sequence, is compare each loop counter with each of the dials on the lock (remember, our lock is represented by an array of size 3, so we'll be comparing the array and indexes 0 to 2). If our current sequence matches the lock's sequence, then we can print a message to the console saying that we've broken the lock! We can also print how many attempts there were before we found the correct sequence. Also, don't forget to set our boolean value to true so we don't continue to try to break the lock! <br \>
 
Finally, all we need to do in order to check if our current sequence matches the combination lock's sequence, is compare each loop counter with each of the dials on the lock (remember, our lock is represented by an array of size 3, so we'll be comparing the array and indexes 0 to 2). If our current sequence matches the lock's sequence, then we can print a message to the console saying that we've broken the lock! We can also print how many attempts there were before we found the correct sequence. Also, don't forget to set our boolean value to true so we don't continue to try to break the lock! <br \>
 +
{{CodeBlock
 +
|Code=
 +
// 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
 +
}}
 +
{{OutputBlock
 +
|Code=
 +
Breaking combination...
 +
*** Combination broken ***
 +
Sequence: [5][10][45]
 +
Attempts: 18646
 +
*** END OF PROCESSING. ***
 +
}}
  
<pre>
 
// 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
 
</pre>
 
 
<br \><br \>
 
  
 +
If combination was not found it means the user enter a digit higher than the possible number.
 +
{{CodeBlock
 +
|Code=
 +
if (!found)
 +
{
 +
System.out.println("*** Could not break combination. ***");
 +
}//end if
 
}}
 
}}
 +
{{OutputBlock
 +
|Code=
 +
Enter a number from 0 to 59 for the combination: 30
 +
Enter a number from 0 to 59 for the combination: 89
 +
Enter a number from 0 to 59 for the combination: 29
 +
Breaking combination...
 +
*** Could not break combination. ***
 +
*** END OF PROCESSING. ***
 +
}}
 +
|SolutionCode=
  
<pre>
+
import javax.swing.*;
 +
import java.util.Scanner;
  
import java.util.Scanner; // we'll need to import this so we can type in stuff with the keyboard
+
public class Combination
 
+
 
+
public class Combination  
+
 
{
 
{
 
public static void main(String[] args)
 
public static void main(String[] args)
Line 109: Line 135:
 
final int NUM_POSSIBILITIES = 59; // you can change this to any size you like
 
final int NUM_POSSIBILITIES = 59; // you can change this to any size you like
 
final int NUM_DIALS = 3;
 
final int NUM_DIALS = 3;
+
 
 +
                Scanner keyboard = new Scanner(System.in);
 +
String input; // what we'll be using for input... we'll parse each input string to an int
 
int[] combo = new int[3]; // the combination lock
 
int[] combo = new int[3]; // the combination lock
 
boolean found = false; // so we know when to stop...
 
boolean found = false; // so we know when to stop...
+
 
 
int num_attempts = 0; // we'll keep track of the number of attempts
 
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
 
// read in combination
 
for (int i = 0; i < NUM_DIALS; i++)
 
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
 
// this command allows the user to enter a number
// and assign it to a spot in our int array
+
System.out.print("Enter a number from 0 to " + NUM_POSSIBILITIES +" for the combination: ");
combo[i] = in.nextInt();
+
        input = keyboard.next();
 +
 
 +
// now assign it to a spot in our int array
 +
combo[i] = Integer.parseInt(input);
 
}//end for
 
}//end for
+
 
in.close(); // close the scanner since we won't be using it anymore
+
System.out.println("Breaking combination...");
+
 
 
// try to break the combo
 
// try to break the combo
 
// we'll use a loop for each "dial" on the lock
 
// we'll use a loop for each "dial" on the lock
 
for (int i = 0; i <= NUM_POSSIBILITIES && !found; i++)
 
for (int i = 0; i <= NUM_POSSIBILITIES && !found; i++)
{
+
{
 
for (int j = 0; j <= NUM_POSSIBILITIES && !found; j++)
 
for (int j = 0; j <= NUM_POSSIBILITIES && !found; j++)
{
+
{
 
for (int k = 0; k <= NUM_POSSIBILITIES && !found; k++)
 
for (int k = 0; k <= NUM_POSSIBILITIES && !found; k++)
 
{
 
{
 
num_attempts++; // increment the number of attempts
 
num_attempts++; // increment the number of attempts
+
 
 
// if we have the right sequence of numbers, then we've broken
 
// if we have the right sequence of numbers, then we've broken
 
// the lock!
 
// the lock!
Line 149: Line 174:
 
System.out.println("Attempts: " + num_attempts);
 
System.out.println("Attempts: " + num_attempts);
 
found = true;
 
found = true;
}//end if
+
}//end if
 
}//end for
 
}//end for
 
}//end for
 
}//end for
 
}//end for
 
}//end for
+
 
 
// if we didn't find the sequence, then the user probably entered
 
// if we didn't find the sequence, then the user probably entered
 
// a number outside of the specified ranger
 
// a number outside of the specified ranger
Line 160: Line 185:
 
System.out.println("*** Could not break combination. ***");
 
System.out.println("*** Could not break combination. ***");
 
}//end if
 
}//end if
+
 
 
System.out.println("*** END OF PROCESSING. ***");
 
System.out.println("*** END OF PROCESSING. ***");
 
}//end main
 
}//end main
}//end test
+
}//end Combination
  
</pre>
+
 
 +
 
 +
}}

Latest revision as of 16:59, 7 December 2011

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 that 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.


After you have configured the lock, your output should look something like this:

 Breaking combination...
*** Combination broken ***
Sequence: [44][21][12]
Attempts: 159673
*** END OF PROCESSING. *** 
 

Problem Solving and Programming Examples

Wiki bench01.jpg

Solution

To solve the problem you have to what you need to solve this problem.
Since you have a constant number like possibilites and dials, they have to be declared final. You have to make a Scanner to get the user's input. Then next step is to use an array to store the combination that the user is going to input. This program isn't errorproof, there are no validation included so you have to assume the user will enter valid input, in this case will only enter numeric input.

 final int NUM_POSSIBILITIES = 59; // you can change this to any size you like
final int NUM_DIALS = 3;

Scanner keyboard = new Scanner(System.in);     //will get the input number
String input; // what we'll be using for input... we'll parse each input string to an int
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 


We will then need to read in three numbers from the user, where each number represents the correct number to select in sequence to unlock the combination. For example, if the user enters "25", "12", "55", then the correct sequence to break the combination lock is 25-12-55. Also, Since we'll be using an array of size 3, we'll assign each number to their correct position in the array (eg. [25,12,55]). We'll accomplish this using a loop that will iterate over three times, and read an integer from a JOptionPane window.
Note Note: Don't forget to parse your input String to an int! Since the array is of type int, we can't assign String values to it.<br\>

 // read in combination
for (int i = 0; i < NUM_DIALS; i++)
{				
	// this command allows the user to enter a number
        System.out.print("Enter a number from 0 to " + NUM_POSSIBILITIES + 
				" for the combination: ")
	input = keyboard.next();
	
	// now assign it to a spot in our int array
	combo[i] = Integer.parseInt(input);
}//end for 
 Enter a number from 0 to 59 for the combination: 5
Enter a number from 0 to 59 for the combination: 10
Enter a number from 0 to 59 for the combination: 45 


To approach the solution, we need to think about how we will simulate each dial on the lock. Since there are three dials, we can use three nested for-loops, each simulating one dial. Each loop will iterate 0 to NUM_POSSIBILITIES, but will stop iterating as soon as we've found the correct lock sequence. We'll need another condition specified in each loop to achieve this.
NOTE: Typing "!found" is the same as typing "found == false". We usually read it as "while not found".

 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
				// more code here
		
		}//end for
	}//end for
}//end for 


Finally, all we need to do in order to check if our current sequence matches the combination lock's sequence, is compare each loop counter with each of the dials on the lock (remember, our lock is represented by an array of size 3, so we'll be comparing the array and indexes 0 to 2). If our current sequence matches the lock's sequence, then we can print a message to the console saying that we've broken the lock! We can also print how many attempts there were before we found the correct sequence. Also, don't forget to set our boolean value to true so we don't continue to try to break the lock!

 // 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 
 Breaking combination...
*** Combination broken ***
Sequence: [5][10][45]
Attempts: 18646
*** END OF PROCESSING. *** 


If combination was not found it means the user enter a digit higher than the possible number.

 if (!found)
{
	System.out.println("*** Could not break combination. ***");
}//end if 
 Enter a number from 0 to 59 for the combination: 30
Enter a number from 0 to 59 for the combination: 89
Enter a number from 0 to 59 for the combination: 29
Breaking combination...
*** Could not break combination. ***
*** END OF PROCESSING. *** 

Code

Solution Code

Back to the Program-A-Day homepage