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
Research the disadvantages of the computer and explain them.​
lisabon 2012 [21]

Answer:

Too much sitting-affects the back and makes our muscles tight

Carpal tunnel and eye strain-moving your hand from your keyboard to a mouse and typing are all repetitive and can cause injuries

Short attention span and too much multitasking-As you use a computer and the Internet and get immediate answers to your questions and requests, you become accustomed to getting that quick dopamine fix. You can become easily frustrated when something doesn't work or is not answered in a timely matter.

3 0
3 years ago
An ipv4 address in the range 169.254.0.1 through 169.254.255.254 indicates a problem with what type of service
DENIUS [597]

Dynamic Host Configuration Protocol, also known as DHCP.

3 0
3 years ago
With landscape photography, which depth of field do you normally want? A) Small B)Small to Medium C)Large D)With landscapes it d
RideAnS [48]
I am not 100% on this but i think you would want a Large depth of field. 
3 0
3 years ago
Read 2 more answers
Write a program that uses the function isPalindrome given in Example 6-6 (Palindrome). Test your program on the following string
Artist 52 [7]

Answer:

#include <bits/stdc++.h>

using namespace std;

bool isPalindrome(string str)

{

   char a,b;

int length = str.length();

for (int i = 0; i < length / 2; i++)

{

   a=tolower(str[i]);//Converting both first characters to lowercase..

   b=tolower(str[length-1-i]);

   if (b != a )

return false;

}

return true;

   

}

int main() {

   string t1;

   cin>>t1;

   if(isPalindrome(t1))

   cout<<"The string is Palindrome"<<endl;

   else

   cout<<"The string is not Palindrome"<<endl;

return 0;

}

Output:-

Enter the string

madam

The string is Palindrome

Enter the string

abba

The string is Palindrome

Enter the string

22

The string is Palindrome

Enter the string

67876

The string is Palindrome

Enter the string

444244

The string is not Palindrome

Explanation:

To ignore the cases of uppercase and lower case i have converted every character to lowercase then checking each character.You can convert to uppercase also that will also work.

6 0
2 years ago
An attacker is preparing to perform what type of attack when the target vulnerabilities include headers and payloads of specific
kari74 [83]

An attacker is preparing to perform what Application attack  when the target vulnerabilities include headers and payloads of specific application protocols.

<h3>What Is an Application Attack?</h3>

An application attack is known to be a type of system attack which is made up of cyber criminals that is said to have access to any unauthorized points.

Note that this kind of Attackers are  the ones that start by  looking at the application layer, and then looking for any kind of application vulnerabilities that is found within the code.

Learn more about vulnerabilities from

brainly.com/question/25633298

3 0
2 years ago
Other questions:
  • Which layer defines an interface that applications can use to request network services, rather than referring directly to applic
    5·1 answer
  • Ternary operators of computer<br><br>please explain. ​
    15·1 answer
  • A media scholar is trying to find out whether internet or television political ads are more persuasive. what kind of effects is
    7·1 answer
  • One group of students did an experiment to study the movement of ocean water. The steps of the experiment are listed below.
    10·1 answer
  • You’ve been stuck in bumper-to-bumper traffic for nearly an hour on a hot summer day. The temperature warning light has just com
    7·1 answer
  • True or False. A compact disc (CD) stores music in a coded pattern of tiny pits 10−7m deep. The pits are arranged in a track tha
    9·1 answer
  • Briley has all the hardware she needs to construct a fully functional personal computer. She connects the various components and
    7·1 answer
  • When referring to hard drives, access time is measured in
    11·1 answer
  • PLZZZ HELP!!! I’ll give brainliest
    12·1 answer
  • Create a program that finds (n+1)! -factorial- of an integer n in C++.
    13·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!