Advertisement
Guest User

Untitled

a guest
Nov 28th, 2014
585
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.05 KB | None | 0 0
  1. /* Declare a class ComboLock that works like the combination lock
  2. in a gym locker, as shown here. The lock is constructed with a
  3. combination—
  4. three numbers between 0 and 39. The reset method
  5. resets the dial so that it points to 0. The turnLeft and turnRight
  6. methods turn the dial by a given number of ticks to the left or
  7. right. The open method attempts to open the lock. The lock opens
  8. if the user first turned it right to the first number in the combination,
  9. then left to the second, and then right to the third.
  10. public class ComboLock
  11. {
  12. . . .
  13. public ComboLock(int secret1, int secret2, int secret3) { . . . }
  14. public void reset() { . . . }
  15. public void turnLeft(int ticks) { . . . }
  16. public void turnRight(int ticks) { . . . }
  17. public boolean open() { . . . }
  18. }
  19.  */
  20. public class Lock {
  21.     private int[] code = new int[3];
  22.     private int[] attempt = new int[3];
  23.     private int[] direction = new int[3];
  24.     private int pos;
  25.     private int count;
  26.  
  27.     public Lock(int secret1, int secret2, int secret3) {
  28.         code[0] = secret1;
  29.         code[1] = secret2;
  30.         code[2] = secret3;
  31.         pos = 0;
  32.         count = 0;
  33.     }
  34.  
  35.     public void reset() {
  36.         attempt[0] = 0;
  37.         attempt[1] = 0;
  38.         attempt[2] = 0;
  39.     }
  40.  
  41.     public void turnLeft(int ticks) {
  42.         System.out.println(pos);
  43.         pos = pos - ticks;
  44.         System.out.println(pos);
  45.         if (pos >= 0) {
  46.             attempt[count] = pos;
  47.  
  48.         } else if (pos < 0) {
  49.             pos += 40;
  50.             attempt[count] = pos;
  51.  
  52.         }
  53.  
  54.         direction[count] = 1;
  55.         count++;
  56.  
  57.     }
  58.  
  59.     public void turnRight(int ticks) {
  60.  
  61.         pos = pos + ticks;
  62.  
  63.         if (pos <= 40) {
  64.             attempt[count] = pos;
  65.         } else {
  66.             attempt[count] = pos - 40;
  67.         }
  68.         direction[count] = 0;
  69.         count++;
  70.  
  71.     }
  72.  
  73.     public boolean open() {
  74.         int correct = 0;
  75.         for (int i = 0; i < 3; i++) {
  76.             if (attempt[i] == code[i]) {
  77.                 correct++;
  78.             }
  79.         }
  80.         System.out.println("------");
  81.         for (int z : attempt) {
  82.             System.out.println(z);
  83.         }
  84.  
  85.         if (direction[0] == 0 && direction[1] == 1 && direction[2] == 0) {
  86.             correct++;
  87.         }
  88.         if (correct == 4) {
  89.             return true;
  90.  
  91.         } else {
  92.             return false;
  93.         }
  94.  
  95.     }
  96.  
  97. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement