Question: I need help with writing code to run my test code. OverviewThe lock mechanism consists of three discs mounted on the same axis. Each disc
I need help with writing code to run my test code.
OverviewThe lock mechanism consists of three discs mounted on the same axis. Each disc has a notch on its outer edge, and when the discsare rotated so that the three notches are alignedin the rightspot, the lock can be opened. The front disc, akadisc 3, is connected directly to a dial, which we assume is labeledaround its outer edge from 0 to 359, ordered clockwise,corresponding to degrees of rotation. 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 lockwith the dial, disc3,and disc 2 removed; you can see the notch and the toothon 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 disc2(the middle one) and force it to turn as well. Likewise, if turned far enough the tooth on the back ofdisc2will 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 1into 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.Breaking into people's lockers is evidently quite a thingthese days, because there is a ton of information available on the internet about how these locks work and how to defeat them.Here is a great computeranimation: https://www.youtube.com/watch?v=sftkP4CjjZs.(However, note that in our model weare assuming that both the front and backteeth for disc 2 are in the same position,unlike the animation).And here is a video of a similar lock constructed using transparent materials.(This one actually has four discs,but the idea is thesame.)https://interestingengineering.com/this-transparent-combination-lock-model-clearly-explains-what-goes-inside
We describe the positionof 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 positionangleto be "normalized" so it isalways a numberin 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 rightbehindthe 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 everydisc's current position is equalto its offset, that meansthe notches are all correctly aligned and the lock can be opened.The valuesof these offsets aredetermined bythe combination. The way we interpret a combination X, Y, Z is according to thepackage directions, like this:-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 ZIt would be convenient if when the combination isX, Y, Z theoffsets for discs 1, 2, and 3, respectively, would simply be X, Y, and Z. However, if we turn the dial several complete revolutions counterclockwiseand 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 102degrees and disc 1 would be at 104degrees. Similarly, if we then rotate the dial clockwise a full revolution and stop at 100 degrees, disc 2would 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.
import hw2.Padlock;
/** * A few usage examples for the Padlock class... */ public class SimpleTest { public static void main(String[] args) { // try getting and setting the disc positions Padlock p = new Padlock(10, 20, 30); printPositions(p); // expected 4 2 0 p.setPositions(42, 137, 17); // order of args is back to front (disc 1, 2, 3) printPositions(p); // expected 42 137 17 // Can we normalize the angles? p.setPositions(-90, 800, 42); printPositions(p); // expected 270 80 42 System.out.println(); // Try out the locking logic // Should initially be open System.out.println(p.isOpen()); // expected true p.close(); System.out.println(p.isOpen()); // expected false // Shouldn't open, since discs are not aligned System.out.println(p.isAligned()); // expected false p.open(); System.out.println(p.isOpen()); // expected false // But should open if we align the discs // Since the given combo was 10, 20, 30, and tooth width // is 2, the offsets should be 6, 22, 30 p.setPositions(6, 22, 30); System.out.println(p.isAligned()); // expected true
// now we should be able to open it p.open(); System.out.println(p.isOpen()); // expected true System.out.println(); // Try out the turn() method // first test - make sure the front disc position // ends up correct p.setPositions(4, 2, 0); p.turn(10); System.out.println(p.getDiscPosition(3)); // expected 10 p.turn(-100); System.out.println(p.getDiscPosition(3)); // expected 270 p.turn(800); System.out.println(p.getDiscPosition(3)); // expected 350 System.out.println(); // Next, how does disc 3 affect disc 2? // Difference when moving ccw is 90 - 30 = 60 degrees, // less the tooth width, leaving 58 degrees. // We're rotating 70 degrees ccw, so disc 2 is // 12 degrees ccw to angle 102. p.setPositions(0, 90, 30); p.turn(70); printPositions(p); // expected 0 102 100 // Try another one... // Difference when turning ccw is 20 - 350 = -330, // i.e. 30 degrees, less the tooth width leaves 28 degrees. // We're rotating 50 degrees ccw, so disc 2 is pushed 22 // degrees ccw. p.setPositions(0, 20, 350); p.turn(50); printPositions(p); // expected 0 42 40 System.out.println(); // Try similar examples with clockwise rotation // Difference when moving clockwise is 20 - 350 = -330 or // 30 degrees less tooth width leaves 28 degrees. // We're rotating 40 degrees cw, so disc 2 will be pushed // 12 degrees cw. // Disc 3 ends up at 20 - 40 = -20 or 340 degrees. // Disc 2 ends up at 350 - 12 = 338 degrees. p.setPositions(0, 350, 20); p.turn(-40); printPositions(p); // expected 0 338 340 System.out.println();
// Difference when moving clockwise is 30 - 40 which is -10 or // 350 degrees, less the tooth width is 348. // We're rotating 400 degrees clockwise, so disc 2 is // pushed 52 degrees cw. // Disc 3 ends up at 30 - 400 = -370 or 350. // Disc 2 ends up at 40 - 52 = -12 or 348. p.setPositions(45, 40, 30); p.turn(-400); printPositions(p); // expected 45 348 350 // What about disc 1? // Same example as above, rotating clockwise, but // disc 1 starts at 10 degrees. // Clockwise difference between disc 1 and disc 2 is // 40 - 10 = 30, less the tooth width is 28. // From previous example, disc 2 is rotating 52 degrees cw, // so it will push disc 1 clockwise 24 degrees. // New disc 1 position is 10 - 24 = -44 or 346. p.setPositions(10, 40, 30); p.turn(-400); printPositions(p); // expected 346 348 350 System.out.println(); // Try out turn-to-number methods with the combination 10, 20, 30 p.turn(-720); // spin it twice clockwise... p.turnRightTo(10); // ... and continue clockwise to 10 p.turn(360 - Padlock.TOOTH); // spin left a full revolution... p.turnLeftTo(20); // ... continue to 20 p.turnRightTo(30); // and turn left to third number System.out.println(p.isAligned()); // expected true } private static void printPositions(Padlock p) { int c = p.getDiscPosition(3); int b = p.getDiscPosition(2); int a = p.getDiscPosition(1); System.out.println(a + " " + b + " " + c); } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
