Design a java interface called lockable that includes the following methods: set
ID: 3668424 • Letter: D
Question
Design a java interface called lockable that includes the following methods: setkey, lock, unlock,and locked. 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 locked method returns a boolean that indicates whether or not the object is locked. a lockable object represents an object whose regular methods are protected: if the object is locked, the methods cannot be invoked; if it is unlocked, they can be invoked. redesign and implement a version of the coin class from chapter 5 so that it is lockable. using java please
Explanation / Answer
Lockable interface:
public interface Lockable {
public void setKey(int newkey);
public void lock(int key);
public void unlock(int key);
public boolean locked();
}
Java Code:
public class Pass implements Lockable{
private int k, setKey;
private boolean locked;
public Pass(int key){
k = key;
locked = false;
}
public void setKey(int newKey) {
newKey = 555;
setKey = newKey;
return;
}
public void lock(int key) {
if(k == setKey)
locked = true;
}
public void unlock(int key) {
if ( k == setKey)
locked = false;
}
public boolean locked() {
return locked;
}
public String toString(){
if(locked = true)
return "Locked";
else
return "Unlocked";
}
}
Java Code:
import java.util.Scanner;
public class PassDriver {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int keyGuess;
System.out.println("Enter key: ");
keyGuess = scan.nextInt();
Pass key = new Pass(keyGuess);
System.out.println(key);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.