Question
I have the code to start but I'm stuck where I'm at. Could use a hint or help to fix it! Here is the code
I have the code to start but I'm stuck where I'm at. Could use a hint or help to fix it!
Here is the code I have so far:
Here is the TestCase:
1. Create a new Eclipse project and within it create a package hw2. Create the Padlock class in the hw1 package and put in stubs for all the required methods, the constructor, and the required constant. Remember that everything listed is declared public. For methods that are required to return a value, just put in a "dummy" return statement that returns zero. There should be no compile errors. You should be able to run the sample main class SimpleTest.java.
2. Briefly javadoc the class, constructor and methods. This is a required part of the assignment anyway, and doing it now will help clarify for you what each method is supposed to do before you begin the actual implementation. (Copying from the method descriptions here or in the Javadoc is perfectly acceptable; however, DO NOT COPY AND PASTE DIRECTLY from the pdf or online Javadoc! This leads to insidious bugs caused by invisible characters that are sometimes generated by sophisticated document formats. If you are super lazy about typing, it should be ok if you copy, paste into a dumb text editor like Notepad, and then copy/paste from there.)
3. You could start by first making sure you can set and get the current rotation of each disc. According to the constructor, assuming that the tooth width is 2 degrees, the initial position of the discs is always 0 degrees for disc 3, 2 degrees for disc 2, and 4 degrees for disc 1. (This is true regardless of the constructor arguments.). So you should be able to set up a simple test class with the following: Padlock p = new Padlock(10, 20, 30); printPositions(p); // expected 4 2 0 p.setPositions(42, 137, 17); // order is disc 1, 2, 3 printPositions(p); // expected 42 137 17 where printPositions is a helper method that just uses the getDiscPosition method to display the three angles: 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); // back to front (1, 2, 3) } This would also be a good time to make sure you can normalize the angles so they are always between 0 and 359, for example: p.setPositions(-90, 800, 42); printPositions(p); // expected 270 80 42 (Tip: the sample code shown above can be found in the given file SimpleTest.java)
3. At this point you could either continue working on disc rotations, or implement the lock logic; the two tasks can be tackled independently. Suppose we do lock logic first. The key piece is determining whether the discs are "aligned", as specified in the isAligned method. For that we need to know the offset for each disc, that is, the angle of rotation that will put its notch in the right position for the lock to open. According to the general description of the lock internals, if the combination is 10, 20, 30, then the offsets are 6, 22, and 30, for discs 1, 2, and 3, respectively. The isAligned method should return true if the current positions of all three discs match their offsets. So, the behavior we expect is something like this (according to the constructor, the lock should initially be open): System.out.println(p.isOpen()); // expected true System.out.println(p.isAligned()); // expected false p.close(); System.out.println(p.isOpen()); // expected false p.open(); // does nothing; it's locked System.out.println(p.isOpen()); // expected false p.setPositions(6, 22, 30); printPositions(p); // expected 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();
4. To start thinking about what happens when you rotate the dial, first think about just disc 3, the front one that is attached to the dial. You could start by just making sure you can get disc 3 to end up in the correct position: p.setPositions(4, 2, 0); p.turn(10); // 10 degrees ccw System.out.println(p.getDiscPosition(3)); // expected 10 p.turn(-100); // 100 degrees cw System.out.println(p.getDiscPosition(3)); // expected 270 p.turn(800); System.out.println(p.getDiscPosition(3)); // expected 350 System.out.println();
5. Now it gets interesting. How does disc 3 affect disc 2? Suppose disc 3 is at 30 degrees and disc 2 is at 90 degrees, and we rotate disc 3 counterclockwise (ccw) 70 degrees. p.setPositions(0, 90, 30); p.turn(70); printPositions(p); // expected 0 102 100 Here, (disc 2 position) (disc 3 position) = 90 30 = 60 degrees, the counterclockwise rotation from disc 3 to disc 2. There is also the tooth width to account for, so disc 3 can rotate 58 degrees before it starts pushing disc 2. We're rotating disc 3 a total of 70 degrees ccw, so disc 2 will be pushed 12 degrees ccw, ending up at position 102. Maybe we should try one where the subtraction comes out negative: p.setPositions(0, 20, 350); p.turn(50); printPositions(p); // expected 0 42 40 In this case, (disc 2 position) (disc 3 position) = -330 degrees, which normalizes to 30 degrees. With the tooth width, disc 3 can rotate 28 degrees ccw before affecting disc 2. We're rotating 50 degrees, so disc 2 is pushed 22 degrees.
6. Then try going clockwise: p.setPositions(0, 350, 20); p.turn(-40); printPositions(p); // expected 0 338 340 Here, (disc 2 position) (disc 3 position) = 330 degrees, but we want the complement of this angle since we're rotating the opposite direction, which is 30 degrees. (Equivalently, just perform the subtraction in the opposite order and normalize.) Less the tooth width that's 28 degrees. Disc 3 is rotating a total of 40 degrees clockwise (cw), so disc 2 will move 12 degrees cw. New disc 3 position = 20 40 = -20, which normalizes to 340. New disc 2 position = 350 12 = 338. Try another one: p.setPositions(45, 40, 30); p.turn(-400); printPositions(p); // expected 45 348 350 Here, (disc 2 position) (disc 3 position) = 10 degrees, but we want the complement of this angle since we're rotating the opposite direction, which is 350 degrees; less the tooth width that's 348 degrees. Disc 1 is rotating 400 degrees cw, so disc 2 will be pushed 52 degrees. New disc 3 position = 30 400 = -370, which normalizes to 350. New disc 2 position = 40 52 = -12, which normalizes to 348. Accounting for the effect of disc 2 on disc 1 is similar. In the test case above, for example, we can tell that the difference, (disc 1 position) (disc 2 position) = 45 - 40 = 5 degrees, the complement of which is 355, less the tooth width is 353; but disc 2 is only rotating 52 degrees cw, so disc 1 is unaffected. If we instead had the same example but with disc 1 at 10 degrees: p.setPositions(10, 40, 30); p.turn(-400); printPositions(p); // expected 346 348 350 Now, (disc 1 position) (disc 2 position) = -30 degrees, normalized to 330, but we want the complement which is 30, and allowing for the tooth width, we get 28 degrees cw from disc 2 to disc 1. Disc 2 is moving 52 degrees cw as in previous example, so disc 1 will be pushed 24 degrees cw. New disc 1 position is 10 24 = -14 or 346 degrees.
7. Once turn is implemented, it is easy to implement the methods turnLeftTo and turnRightTo. After that, you should be able to open the lock according the instructions on the package (recall we originally constructed a padlock with combination 10, 20, 30): p.turn(-720); // spin it twice clockwise p.turnRightTo(10); p.turn(360 - Padlock.TOOTH); // counterclockwise a full revolution p.turnLeftTo(20); p.turnRightTo(30); System.out.println(p.isAligned()); // expected true p.open(); System.out.println(p.isOpen()); // hooray
THANKS FOR ANY HELP!
1 package hw2; 2 import java.util.Random; 32 /** 4 * A padlock class that models a standard combination padlock 5 6 * @author deetshome. 7 * 8 */ 9 public class Padlock { 10 110 /*** 12 * Constant variable 13 */ 14 public static final int TOOTH = 2; 15 160 /** 17 * Constructs padlock whose combination will be ni, n2, n3 18 * 19 * @param ni the first lock position 20 * 21 * @param n2 the second lock position 22 23 * @param n3 the third lock position 24 */ 25 public Padlock(int ni, int n2, int n3) { 26 27 } 28 29 /** 30 * Sets the positions of the three discs to given angles * as closely as possible while ensuring the positions are valid */ 336 public void setPositions(int ni, int n2, int n3) { 34 35 } 31 32 /** * Closes the lock regardless of whether the discs are aligned */ public void close() { } /** * returns the current position of the given disc */ public int getDiscPosition(int which) { return 0; } /** 37 38 39 400 41 42 43 440 45 46 47 48 49 50 516 52 53 540 55 56 57 580 59 60 610 x 62 63 64 65 66 67 68 * Returns true if all three discs are aligned */ public boolean isAligned () { return true; } /** * Determines whether the lock is currently open */ public boolean isOpen() { if (isClosed = true) { return false; } else { return true; } } co /** * Opens the lock, if possible. Does nothing unless isAligned is true */ public void open() { if (isAligned true) { isOpen = true; } = else { isOpen } = false; 700 71 72 730 2x 74 1x 75 76 77 78 9x79 80 81 82 830 84 85 866 87 88 89 900 91 92 } /** * Set the three discs to random, valid positions */ public void randomizePositions(java.util.Random rand) { } 93 /** * Turns the dial (disc 3) the given number of degrees, where a positive number represents * a counterclockwise rotation and a negative number represents a clockwise rotation */ public void turn(int degrees) { return; } 946 95 96 97 986 99 100 1010 102 103 /** * Turns the dial counterclockwise until the given number is at the top */ public void turnLeftTo(int number) { return; } 1050 106 107 1080 109 110 111 112 } /** * Turns the dial clockwise until the given number is at the top */ public void turnRightTo(int number) [ return; } 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); System.out.println(); // expected 270 80 42 // Try out the locking logic // Should initially be open System.out.println(p.isOpen()); p.close(); System.out.println(p.isOpen()); // expected true // 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()); System.out.println(); // expected true // 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 cow is 90 - 30 = 60 degrees, // less the tooth width, leaving 58 degrees. // We're rotating 70 degrees cow, so disc 2 is // 12 degrees cow to angle 102. p.setPositions (0, 90, 30); p.turn(70); printPositions(p); // expected 0 102 100 // Try another one... // Difference when turning cow is 20 - 350 = -330, // i.e. 30 degrees, less the tooth width leaves 28 degrees. // We're rotating 50 degrees cow, so disc 2 is pushed 22 // degrees cow. 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); } } 1 package hw2; 2 import java.util.Random; 32 /** 4 * A padlock class that models a standard combination padlock 5 6 * @author deetshome. 7 * 8 */ 9 public class Padlock { 10 110 /*** 12 * Constant variable 13 */ 14 public static final int TOOTH = 2; 15 160 /** 17 * Constructs padlock whose combination will be ni, n2, n3 18 * 19 * @param ni the first lock position 20 * 21 * @param n2 the second lock position 22 23 * @param n3 the third lock position 24 */ 25 public Padlock(int ni, int n2, int n3) { 26 27 } 28 29 /** 30 * Sets the positions of the three discs to given angles * as closely as possible while ensuring the positions are valid */ 336 public void setPositions(int ni, int n2, int n3) { 34 35 } 31 32 /** * Closes the lock regardless of whether the discs are aligned */ public void close() { } /** * returns the current position of the given disc */ public int getDiscPosition(int which) { return 0; } /** 37 38 39 400 41 42 43 440 45 46 47 48 49 50 516 52 53 540 55 56 57 580 59 60 610 x 62 63 64 65 66 67 68 * Returns true if all three discs are aligned */ public boolean isAligned () { return true; } /** * Determines whether the lock is currently open */ public boolean isOpen() { if (isClosed = true) { return false; } else { return true; } } co /** * Opens the lock, if possible. Does nothing unless isAligned is true */ public void open() { if (isAligned true) { isOpen = true; } = else { isOpen } = false; 700 71 72 730 2x 74 1x 75 76 77 78 9x79 80 81 82 830 84 85 866 87 88 89 900 91 92 } /** * Set the three discs to random, valid positions */ public void randomizePositions(java.util.Random rand) { } 93 /** * Turns the dial (disc 3) the given number of degrees, where a positive number represents * a counterclockwise rotation and a negative number represents a clockwise rotation */ public void turn(int degrees) { return; } 946 95 96 97 986 99 100 1010 102 103 /** * Turns the dial counterclockwise until the given number is at the top */ public void turnLeftTo(int number) { return; } 1050 106 107 1080 109 110 111 112 } /** * Turns the dial clockwise until the given number is at the top */ public void turnRightTo(int number) [ return; } 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); System.out.println(); // expected 270 80 42 // Try out the locking logic // Should initially be open System.out.println(p.isOpen()); p.close(); System.out.println(p.isOpen()); // expected true // 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()); System.out.println(); // expected true // 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 cow is 90 - 30 = 60 degrees, // less the tooth width, leaving 58 degrees. // We're rotating 70 degrees cow, so disc 2 is // 12 degrees cow to angle 102. p.setPositions (0, 90, 30); p.turn(70); printPositions(p); // expected 0 102 100 // Try another one... // Difference when turning cow is 20 - 350 = -330, // i.e. 30 degrees, less the tooth width leaves 28 degrees. // We're rotating 50 degrees cow, so disc 2 is pushed 22 // degrees cow. 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
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