I am having trouble with the open method in this code It is
I am having trouble with the open method in this code. It is suposed to change the boolean lockOpen to true if the combination put into the array is equal to the array combination. Bellow is the code
public class Lock extends InvalidLockCombinationException {
private boolean lockOpen = true;
private int[] comb = new int[3];
private int upper;
private int[] guess = new int[3];
public Lock(int aLimit, Combination myCombo) {
this.upper = aLimit;
int[] comb = myCombo.getNumbers();
for (int i = 0; i < comb.length; i++) {
if (upper < comb[i]) {
throw new InvalidLockCombinationException();
}
}
lockOpen = true;
}
public int getDialLimit(){
return upper;
}
public boolean open(Combination testMe){
int[] guess = testMe.getNumbers();
if(guess.equals(comb)){
lockOpen = true;
}
return lockOpen;
}
public void close(){
lockOpen=false;
}
public boolean isOpen(){
return lockOpen;
}
}
----------------------------------------------------------------------------------------------------------------------------
bellow is the j unit code that is failing
@Test
public void testClosedLockIsOpenedWithCorrectCombination() {
Combination comb = new Combination( 0, 0, 7 );
Lock lock = new Lock( 9, comb );
Combination now = new Combination( 0, 0, 7 );
lock.close();
lock.open( now );
assertTrue( \"Incorrect result\", lock.isOpen() );
}
-------------------------------------------------------------------------------
bellow are the instructions of what each method should do
The Lock class should have fields representing the combination, an upper limit for its dial, and an indicator which states whether or not the Lock is open. Implement the following methods in the Lock class.
1. public Lock(int aLimit, Combination myCombo)
This constructor receives an int upper bound and a Combination object, which will represent the initial lock combination. If the Combination is within the range of the upper bound, then it should set its own combination to the input. Otherwise, it should throw an InvalidLockCombinationException. Furthermore, all locks should be open when created.
2. public int getDialLimit()
This method returns an int representing the dials upper limit.
3. public boolean open(Combination testMe)
This method returns a boolean representing whether or not the lock is open. If the received Combination equals the lock’s Combination, then set the lock’s state to open. If the lock is already open, then lock will remain open regardless of the received
3
Combination. Hint: use Combination.equals(otherCombination) to check if two combinations are equal.
4. public void close()
This method sets the lock’s state to closed.
5. public boolean isOpen()
This method returns true if the lock is open or false if the lock is closed.
Test your code against LockTest.java.
Solution
Explanaion:
1) The above code is good. Working fine without any errors.
2) Initially lockOpen=true, If you call close() method then it is changed to false.
3) Again If you want to call open() method lockOpen is changed depends on condition written in that method.
4) But In If condition comparing two arrays using equals() method.
5) In general it checks thier addresses, returns for every time false.
i.e if(guess.equals(comb)) returns false.
6) So change the this line as: if(Arrays.equals(comb,guess)). Now it compares the values of the array contains.
7) And other change is in: Lock(int aLimit, Combination myCombo) constructor
8) Remove the initialization for comb array; int[] comb = myCombo.getNumbers(); and make as
i.e comb = myCombo.getNumbers();
Let me know any concerns. Thank you


