Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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; }
image text in transcribed

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

14.3 Contingency Tables

Answered: 1 week ago