141k views
2 votes
A combination lock has the following basic properties:

the combination (a sequence of three numbers) is hidden; the lock can be opened by providing the combination; and the combination can be changed, but only by someone who knows the current combination.
Design a class with public methods open and changeCombo and private data fields that store the combination. The combination should be set in the constructor. Do not compile and run your code, just provide a paper copy

1 Answer

3 votes

Answer:

public class CombinationLock {

private int combinationNumber1 = 0;

private int combinationNumber2 = 0;

private int combinationNumber3 = 0;

CombinationLock(int combinationNumber1, int combinationNumber2, int combinationNumber3){

this.combinationNumber1 = combinationNumber1;

this.combinationNumber2 = combinationNumber2;

this.combinationNumber3 = combinationNumber3;

}

public boolean open(int number1, int number2, int number3){

if(number1 == combinationNumber1 && number2 == combinationNumber2 && number3 == combinationNumber3)

return true;

else

return false;

}

public boolean changeCombo(int number1, int number2, int number3, int newNumber1, int newNumber2, int newNumber3){

if (open(number1, number2, number3)){

combinationNumber1 = newNumber1;

combinationNumber2 = newNumber2;

combinationNumber3 = newNumber3;

return true;

}else

return false;

}

}

Step-by-step explanation:

- Three variables are created to hold the combination.

- A constructor is created to set the combination.

- A boolean method called open is created to check if the given numbers are correct, and returns true (otherwise returns false).

- A boolean method method called changeCombo is created to check if given numbers are correct. If they are correct, it assigns new values for the combination and returns true (otherwise returns false).

User Bruno Ferreira
by
4.7k points