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. [code] //******************************************************************** // Coin.java Author: Lewis/Loftus // // Represents a coin with two sides that can be flipped. //******************************************************************** public class Coin { private final int HEADS = 0; private final int TAILS = 1; private int face; // ----------------------------------------------------------------- // Sets up the coin by flipping it initially. // ----------------------------------------------------------------- public Coin() { flip(); } // ----------------------------------------------------------------- // Flips the coin by randomly choosing a face value. // ----------------------------------------------------------------- public void flip() { face = (int) (Math.random() * 2); } // ----------------------------------------------------------------- // Returns true if the current face of the coin is heads. // ----------------------------------------------------------------- public boolean isHeads() { return (face == HEADS); } // ----------------------------------------------------------------- // Returns the current face of the coin as a string. // ----------------------------------------------------------------- public String toString() { String faceName; if (face == HEADS) faceName = "Heads"; else faceName = "Tails
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.
[code]
//********************************************************************
// Coin.java Author: Lewis/Loftus
//
// Represents a coin with two sides that can be flipped.
//********************************************************************
public class Coin {
private final int HEADS = 0;
private final int TAILS = 1;
private int face;
// -----------------------------------------------------------------
// Sets up the coin by flipping it initially.
// -----------------------------------------------------------------
public Coin() {
flip();
}
// -----------------------------------------------------------------
// Flips the coin by randomly choosing a face value.
// -----------------------------------------------------------------
public void flip() {
face = (int) (Math.random() * 2);
}
// -----------------------------------------------------------------
// Returns true if the current face of the coin is heads.
// -----------------------------------------------------------------
public boolean isHeads() {
return (face == HEADS);
}
// -----------------------------------------------------------------
// Returns the current face of the coin as a string.
// -----------------------------------------------------------------
public String toString() {
String faceName;
if (face == HEADS)
faceName = "Heads";
else
faceName = "Tails";
return faceName;
}
}
// ********************************************************************
// CoinFlip.java Author: Lewis/Loftus
//
// Demonstrates the use of an if-else statement.
// ********************************************************************
public class CoinFlip {
// -----------------------------------------------------------------
// Creates a Coin object, flips it, and prints the results.
// -----------------------------------------------------------------
public static void main(String[] args) {
Coin myCoin = new Coin();
myCoin.flip();
System.out.println(myCoin);
if (myCoin.isHeads())
System.out.println("You win.");
else
System.out.println("Better luck next time.");
}
}
[/code]
Trending now
This is a popular solution!
Step by step
Solved in 2 steps with 1 images