Here is the assignment for the program that we had to make:
Specify, design, and implement a class that can be used in a program that simulates a combination lock. The lock has a circular knob with the numbers 0 through 39 marked on the edge, and it has a three-number combination, which we'll call x, y, z. To open the lock, you must turn the knob clockwise at least one entire revolution, stopping with x at the top; then you turn the knob counterclockwise, stopping the second time that y appears at the top; finally you turn the knob clockwise again, stopping the next time that z appears at the top. At this point, you may open the lock.
Your Lock class should have a constructor that initializes the three-number combination. Also provide methods:
(a) To alter the lock's combination to a new three-number combination.
(b) To turn the knob in a given direction until a specified number appears at the top.
(c) To close the lock.
(d) To attempt to open the lock.
(e) To enquire about the status of the lock.
(f) To tell what number is currently at the top.
I have completed the Testlock I just need help with the Lock Class. I have both of them enclosed in the file. Please help me.
Ok, now you've got code tags figured out. Now use them to copy-paste your actual source code (and please keep the indentation); the code you have posted now doesn't even come near compiling. For instance, your Lock class with indentation (and some comments) added:
Joined: Jan 28, 2010
Yea I know, I can't figure out what to put, thats why I am asking. Please give me hints on what to put where to make it works.
Well I would start by analyzing what states lock has, like whether the lock is open or not, the numbers in the combination, what place in the combination we are at. I would put all that information at the top of the class. like this
Then in your constructor your going to have to assign values to some variables you want initialized in the lock class when it is constructed. For example:
lastly your going to have to implement the logic. Look at how the lock steps through it combination and replicate those steps in the programing. Id recommend using a switch statement and using a int variable to keep track of the part of the combination we are in. An example switch statement is presented below.
at any rate http://java.sun.com/docs/books/tutorial/ has lots of basic information about the java language and should help you if you get stuck on what to type in for code or need help understanding the syntax and how to use it.
Solving problems is a hobby of mine, specially java problems.
Joined: Jan 28, 2010
Okay I figured out a couple of things. For instance, as stated in the problem, the program works like a real lock. So you have to make a "counter" that will record how the user got from 25 to 35. What i mean by this is if the user went clockwise from 25 to 35, it would record that the user went 30 units. If the user moved counter clock wise, then it would be able to recognize that the user went counter clockwise, because it only moved 10 units. I came up with a formula that will be able to detect this.
For this example
Destination= Cw(Clockwise) CCW=(Counter clockwise)
You have 4 important numbers; 3 for the lock's numbers and 1 for the current position. When you're turning the dial you modify the latter; keep in mind that it can overflow if you turn through the minimum / maximum. The minimum is 0 in real life, but for simplicity's sake you can keep the boundaries at Integer.MIN_VALUE and Integer.MAX_VALUE.
When the turning finds one of the lock's numbers you should mark that number as being matched. Then when you're done turning, the lock is open if all numbers have been matched. Keep in mind that the order is important; the first number must be matched before the second one can be matched. I would also suggest a method close() that resets the matched information; otherwise the lock will remain open once opened. The simple way of keeping the matched status is using a set of booleans; close() then simply sets these to false.