• 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

Java Lock Class program help

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Jon, it's [code], not [javadoc]. You can edit your post to change these tags.
 
Jon Benedetto
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Jon Benedetto
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic