• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Problem from the book

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello
Does someone know an intelegent solution for the following problem:
Imagine you need to open a standard combination dial lock but don't know the combination and don't have a pair of bolt cutters. Write a program that prints all possible combinations so you can print them on a piece of paper and check off each one as you try it. Assume the numbers on the dial range from zero to thirty-six and three numbers in sequence are needed to open the lock.
Thanks a lot
 
Bartender
Posts: 4121
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's pretty easy....



Are you sure this isn't some kind of class assignment or something?

-Nate
[This message has been edited by Nathan Pruett (edited September 11, 2001).]
 
Ranch Hand
Posts: 168
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Mike
You can do this type of operation using nested for loops. The following code gives you the logic, all you need to do is change the value of the <code>MAXIMUM_VALUE</code> constant from 1 to 36.
I have used a StringBuffer rather than printing directly to the screen during the loop iterations, but if you wanted to do it that way, just replace the call to <code>Sbuf.append()</code> with a <code>System.out.println()</code> (bear in mind this will probably slow the program down even more).
It's also probably worth mentioning that when you run this code for numbers ranging from 0 to 36 rather than 0 to 1 it will dump 50653 (37 * 37 * 37) lines of output to your console, most of which you will obviously not be able to see when the program concludes. If you really want to print them to a piece of paper, I would suggest writing them out to a file instead of/as well as writing them to the screen. However, I suspect the question was just an exercise in using nested loops, so that's all I've done here.
<code><pre>
public class CombinationLock
{
// using a constant for the maximum number allowed means
// that we can change it more easily than if it was
// hardcoded into the loops themselves
private static final int MAXIMUM_VALUE= 1;

public static void main(String[] args)
{
StringBuffer sBuf = new StringBuffer();
int possibleCombinations = 0;
int firstNumber, secondNumber, thirdNumber;
for(int i = 0; i <= MAXIMUM_VALUE; i++)
{
firstNumber = i;
for(int j = 0; j <= MAXIMUM_VALUE; j++)
{
secondNumber = j;
for(int k = 0; k <= MAXIMUM_VALUE; k++)
{
thirdNumber = k;
possibleCombinations++;
sBuf.append("\n"
+ firstNumber + " "
+ secondNumber + " "
+ thirdNumber);
}// end of third loop
}// end of second loop
}// end of 1st loop
System.out.println( "POSSIBLE COMBINATIONS FOR 0 TO " + MAXIMUM_VALUE +
"\n" + sBuf.toString() );
// :NB: total possible combinations should equal size of range raised
// to the power of the number of locks (i.e. 3 in this case)
System.out.println("TOTAL OF " + possibleCombinations + " POSSIBLE COMBINATIONS");
}//end of main
}//end of class
</code></pre>
[This message has been edited by Michael Fitzmaurice (edited September 11, 2001).]
[This message has been edited by Michael Fitzmaurice (edited September 11, 2001).]
 
I've never won anything before. Not even a tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic