aspose file tools*
The moose likes Java in General and the fly likes Java Lock Class program help Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Java Lock Class program help" Watch "Java Lock Class program help" New topic
Author

Java Lock Class program help

Jon Benedetto
Greenhorn

Joined: Jan 28, 2010
Posts: 6
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.
Thanks,
Jon
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19761
    
  20

Jon, it's [code], not [javadoc]. You can edit your post to change these tags.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Jon Benedetto
Greenhorn

Joined: Jan 28, 2010
Posts: 6
Rob Prime wrote:Jon, it's [code], not [javadoc]. You can edit your post to change these tags.

Thank you lol I was wondering how they got the Pictures like that.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19761
    
  20

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:
Jon Benedetto
Greenhorn

Joined: Jan 28, 2010
Posts: 6
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.
THanks
Anthony Hay
Greenhorn

Joined: Jan 08, 2010
Posts: 13
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.
Jon Benedetto
Greenhorn

Joined: Jan 28, 2010
Posts: 6
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
int distanceMoved(direction,position,destination)
P= position
d= direction
Destination= Cw(Clockwise) CCW=(Counter clockwise)

Table:
P>D
P<D
Clockwise
P-D
40-(D-P)
Counter clockwise.
40-(p-d)
P-D

SO can someone please put this together, I am having a very hard time. THanks.
Jon Benedetto
Greenhorn

Joined: Jan 28, 2010
Posts: 6
My assignment is due tomorrow. I asked my professor for help but he hasn't contacted me back yet. Can someone please send me the code, or how to construct it soon?
Thanks
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19761
    
  20

Jon Benedetto wrote:Can someone please send me the code

No, we can't.

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Java Lock Class program help