Question: In this exercise you are going to create an Interface called Lockable. The lockable interface should be used by classes who wish to have objects

In this exercise you are going to create an Interface called Lockable. The lockable interface should be used by classes who wish to have objects that may enter or leave a locked state. We will say that if an object is in a locked state, then certain methods of the object cannot be executed. For example, if a class called Door has an open method, then if a door object enters the locked state, calling door.open() would not succeed. We would have to unlock the door before we could successfully call the open method.
Create the Lockable interface that includes the following methods: setKey, lock, unlock, and isLocked. The setKey, lock, and unlock methods take an integer parameter that represents the key. The setKey method establishes the key. The lock and unlock methods lock and unlock the object, but only if the key passed in is correct. The isLocked method returns a boolean that indicates whether or not the object is locked. A Lockable object represents an object whose regular methods can be frozen: if the object is locked, the methods cannot be invoked; if it is unlocked, they can be invoked. [5 Marks]
Below is a class called Door. A Door can be open or closed and has methods to open the door and close the door.
Make Door implement the Lockable interface and provide implementations for the abstract methods of Lockable. Initialize the door to be unlocked and have a key value of 0. Use the locked state to prevent the door from being opened. [5 Marks]
public class Door{
private boolean isOpen;
public Door(){
isOpen = false;
}
public void open(){
isOpen = true;
}
public void close(){
isOpen = false;
}
public boolean getState(){
return isOpen;
}
public String toString(){
if(isOpen)
return "The door is open";
else return "The door is closed";
}
Here is the Account class, Make Account implement the Lockable interface. When an Account becomes locked, you cannot make deposits or withdrawals. [5 Marks]
public class Account{
private static int baseNum =100000000;
private int number;
private double balance;
public Account(){
this.number = baseNum++;
this.balance =0;
}
public Account(double balance){
this.number = baseNum++;
this.balance = balance;
}
public void deposit(double amount){
if (amount >0)
balance += amount;
}
public void withdraw(double amount){
if (amount >0 && amount = balance)
balance -= amount;
}
public int getNumber(){
return number;
}
public double getBalance(){
return balance; }
 In this exercise you are going to create an Interface called

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!