My college Professor told me to come here for help:
Here is the assignment:
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.
HEre is what I have so far:
Lock Class
class Lock { Lock(int num1, int num2, int num3) { } int getTop() { // Temporary Code return 0; } boolean tryOpen() { // Temporary Code return false; } boolean tryClose() { // Temporary Code return false; } boolean isOpen() { // Temporary Code return false; } void setCombo(int num1, int num2, int num3) { } void turnKnobTo(boolean direction, int num) { // direction == true => clockwise } } End of Class Lock
HEre is the test I did. I know that the test works because I am very strong:
public class TestLock1 { public static void main(String[ ] args) { Lock lock1 = new Lock(15, 25, 35); if(lock1.tryOpen()) { System.out.println("Error - Lock opened!"); System.exit(0); } lock1.turnKnobTo(true, 15); lock1.turnKnobTo(true, 15); lock1.turnKnobTo(false, 25); lock1.turnKnobTo(false, 25); lock1.turnKnobTo(true, 35); if (lock1.tryOpen()) { System.out.println("Success - lock opened."); } else { System.out.println("Error - Lock not opened!"); System.exit(0); } } }
Please tell me what to replace the //Temporary code with, and explain how you got it thanks!