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

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
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.

[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]

Criteria
Ratings
Pts
Documentation
10.0 pts
Implementation of Java Interface Lockable
25.0 pts
Sets the object's key.
public class Coin2 implements Lockable
public void flip()
public boolean isHeads()
public String toString()
35.0 pts
public void setKey(int key)
public void lock(int key)
public void unlock(int key)
public boolean locked()
public class Coin2Test
20.0 pts
Flips a coin multiple times and counts the number of heads and tails that result. Locks and unlocks coins.
Successful run
10.0 pts
Total Points: 100.0
Transcribed Image Text:Criteria Ratings Pts Documentation 10.0 pts Implementation of Java Interface Lockable 25.0 pts Sets the object's key. public class Coin2 implements Lockable public void flip() public boolean isHeads() public String toString() 35.0 pts public void setKey(int key) public void lock(int key) public void unlock(int key) public boolean locked() public class Coin2Test 20.0 pts Flips a coin multiple times and counts the number of heads and tails that result. Locks and unlocks coins. Successful run 10.0 pts Total Points: 100.0
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 2 steps with 1 images

Blurred answer
Knowledge Booster
Introduction to Interface
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education