Question
Create a Java project called Padlock.java with the given information public static final int TOOTH = 2; There is one constructor:Padlock(int n1, int n2, int
Create a Java project called Padlock.java with the given information
public static final int TOOTH = 2;
There is one constructor:Padlock(int n1, int n2, int n3)Constructs a padlock whose combination willben1, n2, n3There are the following public methods:void close()closes the lock, regardless of whether the discs are alignedint getDiscPosition(int which)returns thecurrent position of the given disc (1, 2,or 3)booleanisAligned()returns true if the three discs are aligned in position for the lock toopenbooleanisOpen()returns true if the lock is currently openvoid open()opens the lock, ifpossiblevoid randomizePositions(Random rand)sets the disc positions to randomly generated, valid valuesvoid setPositions(int n1,int n2,int n3)sets the disc positions to the given values (as closely as possible)void turn(int degrees)turns the dial the given number of degrees, where a positive number indicates a counterclockwise rotation and anegative numberindicates a clockwise rotationvoid turnLeftTo(int number)turns the dial counterclockwise until the given number is at the topvoid turnRightTo(int number)turns the dial clockwise until the given number is at the top
The lock mechanism consists of three discs mounted on the same axis. Each disc has a notch on its outer edge, and when the discs are rotated so that the three notches are aligned in the right spot, the lock can be opened. The front disc, aka disc 3, is connected directly to a dial, which we assume is labeled around its outer edge from 0 to 359, ordered clockwise, corresponding to degrees of rotation. Nord The front disk has a small protrusion, or "tooth", on its back side, and the middle disc (disc 2) has a tooth on both its front and back, and the rear disk (disc 1) has a tooth on its front side. The illustration at right shows the inside of the lock with the dial, disc 3, and disc 2 removed; you can see the notch and the tooth on disc 1. Only the front disc (disc 3), connected to the dial, can be turned directly by the user, but if turned far enough the tooth will bump into the tooth on disc 2 (the middle one) and force it to turn as well. Likewise, if turned far enough the tooth on the back of disc 2 will contact the tooth on the disc 1 (the rear disc) and cause it to turn. Thus, after turning the dial two complete revolutions clockwise, it will be possible to push disc 1 into any desired position. After that, if the dial is turned counterclockwise, discs 1 and 2 will initially remain stationary. After one complete revolution counterclockwise, the tooth on the front disc will again contact the tooth on disc 2 and cause it to rotate counterclockwise, still leaving the rear disc alone. In this way disc 2 can be rotated into a desired position. Finally, the front disc can be rotated clockwise to a desired position without affecting the other two. 30 Tooth O We describe the position of each disc as the number of degrees that its tooth is rotated counterclockwise from the zero position at the top. In general, we require the position angle to be "normalized" so it is always a number in the range from 0 through 359. For the front disc, its position always matches the number at the top of the dial; i.e., we assume the tooth is right behind the number 0. Each disc also has an offset, in degrees, which is based on the angle between its tooth and the notch. If the notch is in the correct final alignment position when the disc's position is D degrees, then we say the offset is D. Notice we don't care what the actual angle is between the tooth and the notch; we care what the offset is: when every disc's current position is equal to its offset, that means the notches are all correctly aligned and the lock can be opened. A disc at position 30 degrees 30 30 358 3590 12., The values of these offsets are determined by the combination. The way we interpret a combination X, Y, Z is according to the package directions, like this: 270 08! - rotate the dial two complete revolutions clockwise and then stop at X - rotate the dial one complete revolution counterclockwise and then stop at Y - rotate the dial clockwise and stop at Z When the front disc is at position 30 degrees, the It would be convenient if when the combination is X, Y, Z the offsets number 30 is at the top of for discs 1, 2, and 3, respectively, would simply be X, Y, and Z. the dial. However, if we turn the dial several complete revolutions counterclockwise and stop when the dial is at, say, 100 degrees, the other two discs will not be rotated 100 degrees. We also have to account for the width of the tooth itself. If, for example, the tooth is wide enough to occupy two degrees worth of the disc's rotation, then in this scenario our disc 2 would be at 102 degrees and disc 1 would be at 104 degrees. Similarly, if we then rotate the dial clockwise a full revolution and stop at 100 degrees, disc 2 would be at 98. In general to get combination X, Y, Z, the offsets would be X - 2 * TOOTH for disc 1, Y + TOOTH for disc 2, and Z for disc 3, where TOOTH is the width of the tooth, expressed in degrees. There is one public constant, declared as follows, representing the width of the tooth on each disc, expressed in degrees of rotation. public static final int TOOTH = 2; There is one constructor: Padlock(int ni, int n2, int n3) Constructs a padlock whose combination will be ni, n2, n3 There are the following public methods: void close() closes the lock, regardless of whether the discs are aligned int getDiscPosition(int which) returns the current position of the given disc (1, 2, or 3) boolean isAligned() returns true if the three discs are aligned in position for the lock to open boolean isOpen() returns true if the lock is currently open void open() opens the lock, if possible void randomizePositions (Random rand) sets the disc positions to randomly generated, valid values void set Positions (int ni, int n2, int n3) sets the disc positions to the given values (as closely as possible) void turn (int degrees) turns the dial the given number of degrees, where a positive number indicates a counterclockwise rotation and a negative number indicates a clockwise rotation void turnLeftTo(int number) turns the dial counterclockwise until the given number is at the top void turnRightTo(int number) turns the dial clockwise until the given number is at the top The lock mechanism consists of three discs mounted on the same axis. Each disc has a notch on its outer edge, and when the discs are rotated so that the three notches are aligned in the right spot, the lock can be opened. The front disc, aka disc 3, is connected directly to a dial, which we assume is labeled around its outer edge from 0 to 359, ordered clockwise, corresponding to degrees of rotation. Nord The front disk has a small protrusion, or "tooth", on its back side, and the middle disc (disc 2) has a tooth on both its front and back, and the rear disk (disc 1) has a tooth on its front side. The illustration at right shows the inside of the lock with the dial, disc 3, and disc 2 removed; you can see the notch and the tooth on disc 1. Only the front disc (disc 3), connected to the dial, can be turned directly by the user, but if turned far enough the tooth will bump into the tooth on disc 2 (the middle one) and force it to turn as well. Likewise, if turned far enough the tooth on the back of disc 2 will contact the tooth on the disc 1 (the rear disc) and cause it to turn. Thus, after turning the dial two complete revolutions clockwise, it will be possible to push disc 1 into any desired position. After that, if the dial is turned counterclockwise, discs 1 and 2 will initially remain stationary. After one complete revolution counterclockwise, the tooth on the front disc will again contact the tooth on disc 2 and cause it to rotate counterclockwise, still leaving the rear disc alone. In this way disc 2 can be rotated into a desired position. Finally, the front disc can be rotated clockwise to a desired position without affecting the other two. 30 Tooth O We describe the position of each disc as the number of degrees that its tooth is rotated counterclockwise from the zero position at the top. In general, we require the position angle to be "normalized" so it is always a number in the range from 0 through 359. For the front disc, its position always matches the number at the top of the dial; i.e., we assume the tooth is right behind the number 0. Each disc also has an offset, in degrees, which is based on the angle between its tooth and the notch. If the notch is in the correct final alignment position when the disc's position is D degrees, then we say the offset is D. Notice we don't care what the actual angle is between the tooth and the notch; we care what the offset is: when every disc's current position is equal to its offset, that means the notches are all correctly aligned and the lock can be opened. A disc at position 30 degrees 30 30 358 3590 12., The values of these offsets are determined by the combination. The way we interpret a combination X, Y, Z is according to the package directions, like this: 270 08! - rotate the dial two complete revolutions clockwise and then stop at X - rotate the dial one complete revolution counterclockwise and then stop at Y - rotate the dial clockwise and stop at Z When the front disc is at position 30 degrees, the It would be convenient if when the combination is X, Y, Z the offsets number 30 is at the top of for discs 1, 2, and 3, respectively, would simply be X, Y, and Z. the dial. However, if we turn the dial several complete revolutions counterclockwise and stop when the dial is at, say, 100 degrees, the other two discs will not be rotated 100 degrees. We also have to account for the width of the tooth itself. If, for example, the tooth is wide enough to occupy two degrees worth of the disc's rotation, then in this scenario our disc 2 would be at 102 degrees and disc 1 would be at 104 degrees. Similarly, if we then rotate the dial clockwise a full revolution and stop at 100 degrees, disc 2 would be at 98. In general to get combination X, Y, Z, the offsets would be X - 2 * TOOTH for disc 1, Y + TOOTH for disc 2, and Z for disc 3, where TOOTH is the width of the tooth, expressed in degrees. There is one public constant, declared as follows, representing the width of the tooth on each disc, expressed in degrees of rotation. public static final int TOOTH = 2; There is one constructor: Padlock(int ni, int n2, int n3) Constructs a padlock whose combination will be ni, n2, n3 There are the following public methods: void close() closes the lock, regardless of whether the discs are aligned int getDiscPosition(int which) returns the current position of the given disc (1, 2, or 3) boolean isAligned() returns true if the three discs are aligned in position for the lock to open boolean isOpen() returns true if the lock is currently open void open() opens the lock, if possible void randomizePositions (Random rand) sets the disc positions to randomly generated, valid values void set Positions (int ni, int n2, int n3) sets the disc positions to the given values (as closely as possible) void turn (int degrees) turns the dial the given number of degrees, where a positive number indicates a counterclockwise rotation and a negative number indicates a clockwise rotation void turnLeftTo(int number) turns the dial counterclockwise until the given number is at the top void turnRightTo(int number) turns the dial clockwise until the given number is at the top
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started