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
What will be displayed after code corresponding to the following pseudocode is run? Main Set OldPrice = 100 Set SalePrice = 70 C
Tatiana [17]

Answer:

A jacket that originally costs $ 100 is on sale today for $ 80                                    

Explanation:

Main : From here the execution of the program begins

Set OldPrice = 100  -> This line assigns 100 to the OldPrice variable

Set SalePrice = 70   -> This line assigns 70to the SalePrice variable

Call BigSale(OldPrice, SalePrice)  -> This line calls BigSale method by passing OldPrice and SalePrice to that method

Write "A jacket that originally costs $ ", OldPrice  -> This line prints/displays the line: "A jacket that originally costs $ " with the resultant value in OldPrice variable that is 100

Write "is on sale today for $ ", SalePrice  -> This line prints/displays the line: "is on sale today for $ " with the resultant value in SalePrice variable that is 80

End Program -> the main program ends

Subprogram BigSale(Cost, Sale As Ref)  -> this is a definition of BigSale method which has two parameters i.e. Cost and Sale. Note that the Sale is declared as reference type

Set Sale = Cost * .80  -> This line multiplies the value of Cost with 0.80 and assigns the result to Sale variable

Set Cost = Cost + 20  -> This line adds 20 to the value of Cost  and assigns the result to Cost variable

End Subprogram  -> the method ends

This is the example of call by reference. So when the method BigSale is called in Main by reference by passing argument SalePrice to it, then this call copies the reference of SalePrice argument into formal parameter Sale. Inside BigSale method the reference &Sale is used to access actual argument i.e. SalePrice which is used in BigSale(OldPrice, SalePrice) call. So any changes made to value of Sale will affect the value of SalePrice

So when the method BigSale is called two arguments are passed to it OldPrice argument and SalePrice is passed by reference.

The value of OldPrice is 100 and SalePrice is 70

Now when method BigSale is called, the reference &Sale is used to access actual argument SalePrice = 70

In the body of this method there are two statements:

Sale = Cost * .80;

Cost = Cost + 20;

So when these statement execute:

Sale = 100 * 0.80 = 80

Cost = 100 + 20 = 120

Any changes made to value of Sale will affect the value of SalePrice as it is passed by reference. So when the Write "A jacket that originally costs $ " + OldPrice Write "is on sale today for $ " + SalePrice statement executes, the value of OldPrice remains 100 same as it does not affect this passed argument, but SalePrice was passed by reference so the changes made to &Sale by statement in method BigSale i.e.  Sale = Cost * .80; has changed the value of SalePrice from 70 to 80 because Sale = 100 * 0.80 = 80. So the output produced is:

A jacket that originally costs $ 100 is on sale today for $ 80                              

7 0
2 years ago
To display the size of your backup file, you can use the Windows program called: Windows Explorer Scan disk Defrag Windows Acces
Nitella [24]
Windows Explorer scan disk
7 0
2 years ago
Sendstars is a package delivering company that recently made a study on its customer retention and service renewal metrics. They
masya89 [10]

Answer: C. Cause and Effect Modelling.

Explanation:

Data Mining is a technique which aims at obtaining important and useful information in a stockpile of data. This in turn would prove useful in making predictions. It would also inform present decisions.

The Cause and Effect modelling approach explains that for every given action ( effect ), there was a prompting factor ( cause ).

In the case of Sendstars Company, the downward spiral in customer retention was as a result of ill -mannered staff. This effect, prompted the decision to have the employees trained on customer service.

3 0
3 years ago
Read 2 more answers
Which managed service should you use if you want to do a lift and shift of an existing Hadoop cluster without having to rewrite
Viefleur [7K]

The managed service should you use if you want to do a lift and shift of an existing Hadoop cluster without having to rewrite your Spark code is f G. Cloud's.

<h3>What is Lift and shift Hadoop clusters?</h3>

This is one where a person can migrate their existing Hadoop and Spark deployment so that they can use G. Cloud without  having to engage in re-architecting.

The managed service should you use if you want to do a lift and shift of an existing Hadoop cluster without having to rewrite your Spark code is f G. Cloud's as it is advantage fast and flexible way to put together infrastructure as a service and others.

Learn more about  managed service from

brainly.com/question/20495853

#SPJ1

3 0
2 years ago
What are the third generation of computer?​
balu736 [363]

The period of third generation was from 1965-1971. The computers of third generation used Integrated Circuits (ICs) in place of transistors. A single IC has many transistors, resistors, and capacitors along with the associated circuitry. The IC was invented by Jack Kilby.

3 0
2 years ago
Read 2 more answers
Other questions:
  • 1. Describe an application where a series circuit might work better than a parallel circuit
    11·2 answers
  • When did the silver market drop sharply, hurting Bolivia's income?
    7·2 answers
  • What is the internet?
    5·2 answers
  • Give one advantage and two disadvantages of using a wireless network
    8·1 answer
  • What will the following loop display? 0 1 2 3 4 5 0 1 2 3 4 0 1 2 3 4 The loop will display numbers starting at 0, for infinity.
    8·1 answer
  • Google’s adwords system provides a quality score as a measure of _____, which indicates the usefulness of an ad message to consu
    6·1 answer
  • What is the best way to locate where my C program gets into an infinite loop
    5·1 answer
  • What are the four stages a customer goes through when buying a product?
    7·2 answers
  • A _____ is a number that summarizes an encrypted information. digital certificate hash function message digest hash algorithm
    8·1 answer
  • (100 points ASAP PLease) Type the correct answer in the box. Spell all words correctly.
    13·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!