Question

In: Computer Science

**java** A bicycle combination lock has four rings with numbers 0 through 9. Given the actual...

**java**

A bicycle combination lock has four rings with numbers 0 through 9. Given the actual numbers and the combination to unlock, print instructions to unlock the lock using the minimum number of twists. A “twist up” increases the number value of a ring, and a “twist down” decreases it. For example, if the actual number shown is 1729 and the desired combination is 5714, write your instructions like this:

Ring 1: Twist up 4 times
Ring 3: Twist down once
Ring 4: Twist up or down 5 times

Solutions

Expert Solution

Here is the solution for your question.

PLEASE DO UPVOTE:

OUTPUT:

RAWCODE:

import java.util.*;

public class Lock{

   public static void main(String[] args) {
      
       int[] lockKey = {5,7,1,4};           //Original Key
       int[] userKey = new int[4];           //Array to store user key

       Scanner sc = new Scanner(System.in);

       for (int i=0;i<4;i++) {
           userKey[i] = sc.nextInt();       //Storing User input into userKey
       }

       int correct = 0,up,down;

       for (int i=0;i<4;i++) {
           if (lockKey[i] == userKey[i])       //If user key value and original key value is equal then,increase correct by 1
               correct++;
           else if (lockKey[i]>userKey[i]){   //If Lock key is greater than userkey

               up = Math.abs(lockKey[i] - userKey[i]);           //Up rotaion will be lock key - user key
               down = Math.abs(((10-lockKey[i])+userKey[i])%10);//Down rotation, we use a simple logic as it is a circular queue rotations will be ((10-lockKey[i])+userKey[i])%10)
               if (up == down)
                   System.out.println("Ring "+(i+1)+": Twist up or down "+down+" times");   //If up and down rotaitons are equal then shows below message
               else
                   System.out.println("Ring "+(i+1)+": Twist "+(up>down?"down ":"up ")+Math.min(up,down)+" times");   //Otherwise suggests either down or up rotation.Which is best

           }
           else{       //If Lock key is less than userkey
               down = Math.abs(lockKey[i] - userKey[i]);       //Down rotaion will be lock key - user key
               up = Math.abs(((10-userKey[i])+lockKey[i])%10); ////Up rotation, we use a simple logic as it is a circular queue rotations will be ((10-lockKey[i])+userKey[i])%10)

               if (up == down)
                   System.out.println("Ring "+(i+1)+": Twist up or down "+down+" times");   //If up and down rotaitons are equal then shows below message
               else
                   System.out.println("Ring "+(i+1)+": Twist "+(up>down?"down ":"up ")+Math.min(up,down)+" times"); //Otherwise suggests either down or up rotation.Which is best
           }

       }
       if (correct==4)       //If All user keys matches original key,then lock opens
           System.out.println("Lock Opened");

   }
}

PLEASE DO UPVOTE.

THANK YOU!


Related Solutions

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?
Create a JAVA lottery game application.  Generate four random numbers, each between 0 and 9 (inclusive).  Allow the...
Create a JAVA lottery game application.  Generate four random numbers, each between 0 and 9 (inclusive).  Allow the user to guess four numbers.  Compare each of the user’s guesses to the four random numbers and display a message that includes the user’s guess, the randomly determined four-digit number, and the amount of points the user has won as follows: No matches 0 points Any one digit matching 5 points Any two digits matching 100 points Any three digits matching 2,000 points All four...
One of the games of the Massachusetts Lottery is the daily numbers.  Four digits (0 - 9)...
One of the games of the Massachusetts Lottery is the daily numbers.  Four digits (0 - 9) are drawn at random thus producing a 4 digit number. Your friend has the following theories about the digits drawn: #1. Over the year the average of the total of the 4 digits drawn each day is 24 #2. If the first digit of the 4 digits drawn is even the last digit is odd at least 75% of the time #3. The middle...
Consider four-digit numbers that consist of 0, 1, 2, 5, 6, and 9. a) How many...
Consider four-digit numbers that consist of 0, 1, 2, 5, 6, and 9. a) How many four-digit numbers can be formed from the digits 0, 1, 2, 5, 6, and 9 if each digit can be used only once? (the four-digit numbers can't start with 0). b) How many of those four-digit numbers are even? c) How many are greater than 2200?
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?
In a lottery, each ticket has 5 one-digit numbers 0-9 on it. (with no digit repeating...
In a lottery, each ticket has 5 one-digit numbers 0-9 on it. (with no digit repeating twice) You win only if your ticket has the digits in the required order. What are your chances of winning?
Consider the set of integer numbers from 0 to 9, that is {0, 1, 2, 3,...
Consider the set of integer numbers from 0 to 9, that is {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}. Bob wishes to use these numbers to create a 7-digit password to secure his new laptop. Note that each number can appear in any position (for example, 0 can be the first number in the password). (a) Find the number of 7-digit passwords that are possible. (b) Find the number of 7-digit passwords with distinct digits. (c) Find...
In Java lang Standard telephone keypads contain the digits zero through nine. The numbers two through...
In Java lang Standard telephone keypads contain the digits zero through nine. The numbers two through nine each have 3~4 letters (case insensitive) associated with them. Many people find it difficult to memorize phone numbers, so they use the correspondence between digits and letters to develop seven-letter words that correspond to their phone numbers. For example, a person whose telephone number is 686-2377 might remember it as "NUMBERS." Digit Letters 2 ABC 3 DEF 4 GHI 5 JKL 6 MNO...
IN JAVA pls Standard telephone keypads contain the digits zero through nine. The numbers two through...
IN JAVA pls Standard telephone keypads contain the digits zero through nine. The numbers two through nine each have 3~4 letters (case insensitive) associated with them. Many people find it difficult to memorize phone numbers, so they use the correspondence between digits and letters to develop seven-letter words that correspond to their phone numbers. For example, a person whose telephone number is 686-2377 might remember it as "NUMBERS." Digit Letters 2 A B C 3 D E F 4 G...
This is a standard deviation contest. You must choose four numbers from the whole numbers 0...
This is a standard deviation contest. You must choose four numbers from the whole numbers 0 to 10, with repeats allowed. Step 1: Choose four numbers that have the smallest possible standard deviation. What is the numerical value of s in this case? Give your answer as a whole number (meaning no decimal places). Step 2: Is there more than one possibility for choosing four numbers that have the smallest possible standard deviation? No. Yes. Step 3: Choose four numbers...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT