1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
Tpy6a [65]
2 years ago
13

A bicycle combination lock has four rings with numbers 0 through 9. Given the actual numbers and the combination to unlock, prin

t 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 in java like this:Ring 1: Twist up 4 timesRing 3: Twist down onceRing 4: Twist up or down 5 times
Computers and Technology
1 answer:
Gnom [1K]2 years ago
6 0

/* (Here's a link to a much neater version of this:

https://github.com/HectorC1/Tasks/blob/master/bicycleLock.java

*/

import java.util.Scanner;

import java.util.HashMap;

import java.util.*;

class BicycleLock{

   public static int[] lock = new int[]{1,7,1,4};

   public static int[] unlock = new int[]{5,7,1,4};

   

   int currentRing = 0;

   

   HashMap<Integer, Integer> mapNum = new HashMap<>();

   public void createMap(){

       for(int i = 0; i < 10; i++){

           mapNum.put(i,i);

       }

   }

   public void printLock(){

       for(int i = 0; i < lock.length; i++){

           System.out.print(lock[i]+" ");

       }

   }

   public String userInput(){

       Scanner input = new Scanner(System.in);

       String ring;

       

       System.out.println("Choose a ring: ");

       ring = input.nextLine();

       return ring;

   }

       

   public boolean lockEquals(int[] array1, int[] array2){

       for(int i = 0; i < array1.length; i++){

           if(array1[i] == array2[i]){

               return true;

           }

           else

           return false;

       }

       return false;

   }

  public boolean hasTwistUp(String twistUp){

      if(twistUp.contains("Twist up") || twistUp.contains("twist up")){

          return true;

      }

      else return false;

   }

   public boolean hasRing(String ring){

       if (ring.contains("ring") ||

           ring.contains("Ring")){

       return true;

           }

       else return false;

   }

  public boolean hasTwistDown(String twistDown){

   if(twistDown.contains("Twist down") || twistDown.contains("twist down")){

       return true;

   }

   else return false;

    }

  public void turnCell1(int n){

   

       for(int i = 0; i < n; i++){

           

         

           lock[0] += 1;

           if(lock[0] > 9){

               lock[0] = 0;

           }

           

       }

  }

  public void turnCell2(int n){

   for(int i = 0; i < n; i++){

       lock[1] += 1;

       if(lock[1] > 9){

           lock[1] = 0;

   }

   

  }

}

  public void turnCell3(int n){

   for(int i = 0; i < n; i++){

       lock[2] += 1;

       if(lock[2] > 9){

           lock[2] = 0;

   }

}

   }  

   public void turnCell4(int n){

       for(int i = 0; i < n; i++){

           lock[3] += 1;

           if(lock[3] > 9){

               lock[3] = 0;

       }

    }

   }

  public void currentRing(int ring){

  }

 

  public void setRing(int currentRing){

      this.currentRing = currentRing;

  }

 

  public int getRing(){

      return currentRing;

  }

  public void containsRing(String s){

 

     

      String ring = "";

      String numbers = "";

      int ringNum = 0;

      int firstNum = 0;

      int turnTimes = 0;

      String ringSub = s.substring(0, 7);

      String turnSub= s.substring(7, s.length());

      String turnSubString = s.substring(7, s.length()); // "once or twice"

      char[] charArray = new char[s.length()];

      for(int i = 0; i < s.length(); i++){

       //loop through substring then get the number

          ring += s.charAt(i);

          numbers += s.charAt(i);

   

       //    }

       }

       System.out.println(ring);

       if(hasRing(ring)){

           System.out.println("has ring");

       }

       if(hasTwistUp(s)){

              System.out.println("has twist up ");

             

              // changing the cell turnTimes incrementing by 1 each time

              }

       

       if(hasTwistDown(s)){

           System.out.println("has twist down");

        }

       

       if(ring.contains("ring 1")){

           System.out.println(1);

       }

       else if(ring.equals("ring 2")){

           System.out.println(2);

       }

       

       else if(ring.equals("ring 3")){

           System.out.println(3);

       }

       else if(ring.equals("ring 4")){

           System.out.println(4);

       }

      for(int j = 0; j < ringSub.length(); j++){

           

       if(Character.isDigit(ringSub.charAt(j))){

           ringNum = ringSub.charAt(j);

           ringNum = Character.getNumericValue(ringNum);

           System.out.println("Ring number: " + ringNum);

           setRing(ringNum);

       }

   }

   // do if statement for string number of turns i.e once or twice

   for(int k = 0; k < turnSub.length(); k++){

       

       if(Character.isDigit(turnSub.charAt(k))){

           turnTimes = turnSub.charAt(k);

           turnTimes = Character.getNumericValue(turnTimes);

           System.out.println("Turn times: " + turnTimes);

           if(hasRing(s) && hasTwistUp(s)){

               switch(getRing()){

                   // turn array([ringnumber])

       

                   case 1: turnCell1(turnTimes);

                   break;

                   case 2: turnCell2(turnTimes);

                   break;

                   case 3: turnCell3(turnTimes);

                   break;

                   case 4: turnCell4(turnTimes);                

                   break;

                   

               }

           

       }

   }

   

   }

     

       

       }

 

     

  /*

  public final boolean containsDigit(String s) {

   boolean containsDigit = false;

   if (s != null && !s.isEmpty()) {

       for (char c : s.toCharArray()) {

           if (containsDigit = Character.isDigit(c)) {

               break;

           }

       }

   }

   

   return containsDigit;

}*/

   

   int actualNumber = 0;

   public BicycleLock(){

   }

   public void setLockNumber(int value){

       actualNumber = value;

   }  

   

   public static void main(String...args){

       BicycleLock newLock = new BicycleLock();

       String userInput;

       newLock.createMap();

       

       while(!newLock.lockEquals(lock, unlock)){

       userInput = newLock.userInput();

       newLock.containsRing(userInput);

       newLock.printLock();

       System.out.println();

       }

       

       System.out.println("unlocked");

       }

       

       /*

       Ring 1: Twist up 4 times

       Ring 3: Twist down once

           Ring 4: Twist up or down 5 times

*/

       

   }

You might be interested in
SOMEONE PLEASE HELP ME PLEASE HELP ME WITH THIS!!!!!
zavuch27 [327]

Answer:

The last one

Explanation:

I hope this is correct and have a great day

7 0
2 years ago
Read 2 more answers
Which of the following terms means the computer operating system automatically detects and installs the proper driver for a new
lukranit [14]
Plug and play installation
4 0
2 years ago
A set of programs that enable the hardware to process data is _____.
Mnenie [13.5K]
The most appropriate answer is C !! software os used for using hardware !!
4 0
3 years ago
Tightly.
Radda [10]

Answer:

AG 6.

AG 1.

AG 3.

AG 4.

AG 7.

AG 5.

AG 2.

Explanation:

Chronology can be defined as an arrangement of data, items or documents in the order in which they occurred or happened (in time), from the earliest to the most recent or latest.

Simply stated, ordered events are organized in order of time and this process is known as chronology.

Generally, a chronology can be used as a filing process in various areas such as schools, hospitals, hotels, and business firms to help put data (informations) in order by time or date.

In Computer networking, a cable can be defined as a wired connector used for connecting and transmitting signals between two or more network devices such as CAT-5, CAT-6 and fiber optic cables. These cables which are either twisted or untwisted pair are to be crimped in order to make them eligible and appropriate for use in connecting network devices.

Furthermore, there are two (2) color codes used for the arrangement of the wires in a network cable based on the type of network connection;

I. For straight-through, host device to a router or switch;

Color code: white-orange, orange, white-green, blue, white-blue, green, white-brown, brown.

II. For cross-over, network device to a network device e.g router or router;

white-green, green, white-orange, blue, white-blue, orange,

5 0
2 years ago
Select the word or phrase from the drop-down menu to complete each sentence. File names consist of a ______ and a file extension
Law Incorporation [45]

Answer:

base file name, file format, bitmap image

Explanation:

8 0
2 years ago
Other questions:
  • A type of memory that is expensive and therefore is often used only in cache memory applications.
    15·2 answers
  • How can you tell if a website is secure
    13·1 answer
  • Write an application that inputs a telephone number as a string in the form (555) 555-5555. The application should use String me
    10·1 answer
  • A number of related records that are treated as a unit is called
    6·1 answer
  • Write a program to test the various operations of the class clockType
    8·1 answer
  • select all examples of proper keyboarding technique. rest your fingers gently on the home row or home keys. slouch in your chair
    9·2 answers
  • True/False/Unknown. For the interpretation of function calls, we assign the formal parametersto the valuations of the actual arg
    11·1 answer
  • What is the first step that you have to perform before you can add a windows package to a wim file?
    9·1 answer
  • I need a explanation for this 02 question for a test I will have .
    11·1 answer
  • How much would it cost to get the screen replaced on a Moto G7?
    10·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!