programming forums Java Java JSRs Mobile Certification Databases Caching Books Engineering OS Languages Paradigms IDEs Build Tools Frameworks Products This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Sheriffs:
Saloon Keepers:
Bartenders:

# help with lock

Greenhorn
Posts: 3
Hi,

I've got an assignment that i need some help with. I have to create a lock class, which creates a combination lock that has a dial with 26 positions labeled A ... Z, and has a 3 letter combination. i've got most of it figured out, the thing I'm having difficulty is with the tolerance part.

Example 2: combination = CAT, tolerance = 1
The lock can be opened with: CAT, DAT, CAS, CBT, BAT (since the total error <= tolerance)
The lock cannot be opened with: EAT, AAT, CCT (since the total error > tolerance)
Assume the dial is circular, so that Z is next to A as well as Y.
Example 3: combination = XYZ, tolerance = 3
The lock can be opened with: XYZ, ZZZ, XYB, XYC, ABXZB (since the total error <= tolerance)
(The total errors in this example are XYZ (0), ZZZ (3), XYB (2), XYC (3), ABXZB (3)).
The lock cannot be opened with: WWY, since the total error is 4.

this is my code for the tolerance part. Right now its working if the total error= the tolerance, but how can I make it so it will work with the error <=tolerance. Also, if one of the combination letters is A, if the tolerance is 1, it can be opened with Z, any ideas on how to do that?

Let me know if you need any more details or clarification. Thanks!

Marshal
Posts: 56600
172
Welcome to the Ranch

How many people are there in your class? We seem to have had dozens of questions about lock classes in the last week or two Have you discussed their posting on this forum (or other fora) with them? You may be able to help one another, but please tell us if you find something interesting.

You know that contrary to popular belief, a char is not a letter. It is a (integer) number, and as such, can be used in arithmetic. You can try working out the difference between the letter wanted and the letter supplied. Get its absolute value, and see whether that is greater than the permitted tolerance or not. That would get rid of that block of || ||.

My suggestion would not help using Z instead of A. I shall send you back to the drawing board about A/Z.

Greenhorn
Posts: 3
It's a big course so there are probably a couple hundred. Thanks for letting me know, I'll try discussing with the other people.

Thanks for the information about char, that helped me out. I just need to figure out the A/Z, any clues?

Campbell Ritchie
Marshal
Posts: 56600
172
Start by writing down A..Z with their hexadecimal (or other number) equivalents. You can find those numbers in this Unicode page and lots of other places. You can consider using the remainder when you divide by LETTERS_IN_ALPHABET, which is a better name than was used in the other thread you were involved in. Work out the remainder after dividing each letter by 0x20 (=32) first. Leave the differences till later.

 Don't get me started about those stupid light bulbs.