Question

In: Computer Science

Declare a class ComboLock that works like the combination lock in a gym locker, as shown...

Declare a class ComboLock that works like the combination lock in a gym locker, as shown here. The lock is constructed with a combination — three numbers between 0 and 39. The reset method resets the dial so that it points to 0. The turnLeft and turnRight methods turn the dial by a given number of ticks to the left or right. The open method attempts to open the lock. The lock opens if the user first turned it right to the first number in the combination, then left to the second, and then right to the third.

public class ComboLock {. . .

public ComboLock(int secret1, int secret2, int secret3) {...}

public void reset() { . . . }

public void turnLeft(int ticks) { . . . }

public void turnRight(int ticks) { . . . }

public boolean open() { . . . }

}

In Class P8_1’s main method, test the operation of the lock by showing one Successful case of opening the lock and one Unsuccessful case of opening the lock given a wrong order. To do so, instantiate a ComboLock object and provide the 3 secrets to the constructor and use the ComboLock methods to turn the lock left/right and attempt to open the lock.

Solutions

Expert Solution

Code to copy:

   //let comboCode is 171229

   import java.util.*;

   public class ComboLock {
       int key_1,key_2,key_3;
      
       public ComboLock()
       {
           key_1=0;
           key_2=0;
           key_3=0;
       }
      
       public ComboLock(int secrt_1, int secrt_2, int secrt_3) {
           key_1 = secrt_1;
           key_2 = secrt_2;
           key_3 = secrt_3;
       }
      
       public static boolean turnLeft(int num_Of_tickes) {
           if(num_Of_tickes == 1 || num_Of_tickes == 3)
               return true;
           return false;
       }
      
       public static boolean turnRight(int num_Of_tickes) {
           if(num_Of_tickes == 2)
               return true;
           return false;
       }
      
       public static boolean open() {
           System.out.println("Your Combolock is open");
           return true;
       }
      
       public static boolean menu_list(int a,int s)
       {
           Scanner sc = new Scanner(System.in);
           System.out.println("1. Turn left");
           System.out.println("2. Turn right");
           int ch = sc.next().charAt(0);

           if(ch == '1') {
               if (a==1 && turnLeft(a) && s == 12)
                   return true;
               else if (a==3 && turnLeft(a) && s == 29)
                   return true;
           }

           if(ch == '2') {
               if(a==2 && turnRight(a) && s == 17)
                   return true;
           }
           return false;
       }

       public static void main(String[] args) {
           Scanner sc = new Scanner(System.in);
           int opt=1,num1,num2,num3;
           System.out.println("Enter first number: ");
           num1 = sc.nextInt();
           System.out.println("Enter second number: ");
           num2 = sc.nextInt();
           System.out.println("Enter third number: ");
           num3 = sc.nextInt();
          
           ComboLock lock = new ComboLock(num1,num2,num3);
           if (menu_list(opt,num1))
               opt++;
          
           if(opt == 2) {
               if (menu_list(opt,num2)) {
                   opt++;
               }
           }

           if(opt == 3) {
               if (menu_list(opt,num3)) {
                   open();
               }  
           }
           else
               System.out.println("Invalid Input");
       }
   }

Output Screenshot:


Related Solutions

A well-known company manufactures combination locks. Their retractable cable lock works well for securing sports equipment...
A well-known company manufactures combination locks. Their retractable cable lock works well for securing sports equipment (skis, bikes, golf equipment, etc.) as well as duffel or sports bags. A five-digit (0–9) dial combination lock secures the cable. Suppose that you purchase one of these retractable cable locks to secure your bike to a bike rack at the library. How many five-digit combination lock codes are possible for your lock if you cannot repeat digits? Is this problem a combination or...
Health & Fitness Gym Date Prepared: Client Membership Cost Locker Annual Total Years Total Due Down...
Health & Fitness Gym Date Prepared: Client Membership Cost Locker Annual Total Years Total Due Down Payment Balance Monthly Payment Andrews Deluxe Yes 1 Baker Individual Yes 2 Carter Family No 3 Dudley Deluxe No 2 Evans Deluxe Yes 3 Foust Individual No 1 Gardner Individual No 2 Hart Individual No 3 Ivans Individual Yes 3 Totals Membership Cost Down Payment Summary Statistics Deluxe $           575 $      250 Number of New Members Family $        1,500 $      700 Lowest Monthly Payment...
ToneLock Ltd, security experts and lock manufacturers are considering entering into the production of impenetrable combination...
ToneLock Ltd, security experts and lock manufacturers are considering entering into the production of impenetrable combination safes. The company has called you, their trusted financial advisor, to recommend whether to proceed with the project. The company tells you that the cost of the machinery required to manufacture these safes is $560,000, with an additional $40,000 to install.  The machinery will be depreciated straight line on an annual basis over its entire useful life of 4 years to a salvage value of...
A combination lock has a 1.3-cm-diameter knob that is part of the dial you turn to...
A combination lock has a 1.3-cm-diameter knob that is part of the dial you turn to unlock the lock. To turn that knob, you grip it between your thumb and forefinger with a force of 4.0 N as you twist your wrist. Suppose the coefficient of static friction between the knob and your fingers is  0.75. A)What is the most torque you can exert on the knob without having it slip between your fingers?
How is a combination lock with two numbers (0 and 1) similar to bit encryption, given...
How is a combination lock with two numbers (0 and 1) similar to bit encryption, given we had the ability to add as many wheels as we wanted?
Add a generic Node class to the Java project. In the class Declare the fields using...
Add a generic Node class to the Java project. In the class Declare the fields using the generic type parameter, follow the book specification Define the accessor method getLink( ) Define the constructor, follow the book implementation Note: at the definition the name of the constructor is Node, however every time you use it as a type must postfix the generic type like Node<T> Define the addNodeAfter( ) method as explained in the PP presentation, use the generic type as...
How does one declare a class variable in C#?
How does one declare a class variable in C#?
I have a BigInt class I created that works correctly. I would like to add a...
I have a BigInt class I created that works correctly. I would like to add a constructor BigInt(string s, int base) that creates a BigInt operating in the given base. The newly extended class should work with any base between 2 and 36, using the digits 0, 1, …, 9, A, B, C, …, Z. How do I do this from what I have? (you may add new data members and such of course) #include <string> #include <vector> class BigInt...
A weightlifter works out at the gym each day. Part of her routine is to lie...
A weightlifter works out at the gym each day. Part of her routine is to lie on her back and lift a 41 kg barbell straight up from chest height to full arm extension, a distance of 0.55 m . 1. How much work does the weightlifter do to lift the barbell one time? 2. If the weightlifter does 25 repetitions a day, what total energy does she expend on lifting, assuming a typical efficiency for energy use by the...
Declare and define a class for a fraction number. A fraction in mathematics is defined as...
Declare and define a class for a fraction number. A fraction in mathematics is defined as a/b, where a and b are integers and called numerator and denominator. Requirements Task1    Define a fraction class that has the following member functions: constructor that initializes the fraction by default arguments. set function that sets the numerator of the fraction. set function that sets the denominator of the fraction. get function that returns the numerator of the fraction. get function that returns...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT