Advertisement
CyrusVerkest

comboLock

Nov 18th, 2014
172
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.56 KB | None | 0 0
  1. A)
  2. package comboLock;
  3.  
  4. public class comboLock {
  5.    
  6.     private int secret1;
  7.     private int secret2;
  8.     private int secret3;
  9.     private int ticks;
  10.     private int unlock = 0;
  11.  
  12.     public comboLock(int secret1, int secret2, int secret3)
  13.     {
  14.         this.secret1 = secret1;
  15.         this.secret2 = secret2;
  16.         this.secret3 = secret3;
  17.     }
  18.    
  19.     public void turnLeft(int ticks)
  20.     {
  21.         this.ticks = ticks;
  22.         if(ticks == secret2)
  23.         {
  24.             unlock++;
  25.         }
  26.     }
  27.     public void turnRight(int ticks)
  28.     {
  29.         this.ticks = ticks;
  30.         if(ticks == secret1)
  31.         {
  32.             unlock++;
  33.         }
  34.         if(ticks == secret3)
  35.         {
  36.             unlock++;
  37.         }
  38.     }
  39.     public void reset()
  40.     {
  41.         ticks = 0;
  42.     }
  43.     public void open()
  44.     {
  45.         if (unlock == 3)
  46.         {
  47.             System.out.println("The lock is open");
  48.         }
  49.         else
  50.         {
  51.             System.out.println("you have enter the incorrect Combination");
  52.         }
  53.     }
  54. }
  55.  
  56.  
  57. -----------------------------------------------------------------------------------------------------------------------------------
  58. B)
  59. package comboLock;
  60. import java.util.Scanner;
  61. public class comboLockTester {
  62.  
  63.     public static void main(String[] args) {
  64.         Scanner in = new Scanner(System.in);
  65.         System.out.println("Please set your combination");
  66.         int h = in.nextInt();
  67.         int b = in.nextInt();
  68.         int c = in.nextInt();
  69.         comboLock cyrus = new comboLock(h, b, c);
  70.         System.out.println("Enter your combination");
  71.         int v = in.nextInt();
  72.         int e = in.nextInt();
  73.         int r = in.nextInt();
  74.         cyrus.turnRight(v);
  75.         cyrus.reset();
  76.         cyrus.turnLeft(e);
  77.         cyrus.reset();
  78.         cyrus.turnRight(r);
  79.         cyrus.open();
  80.         System.out.println(cyrus);
  81.     }
  82.  
  83. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement