Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Write a Java interface called Lockable that includes the following methods: setKey, lock, unlock, and locked. The setKey, lock and unlock methods take an integer

Write 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. Write a version of the Coin class (call it Coin2) from Chapter 5 so that it is Lockable.

//****************** ///Coin.java //flips a two sided coin //*******************

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."); } }

//******************************************************************** // 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; } }

**Then Use the following driver CoinFlipTest to test your Coin2 class and Lockable interface.

public class CoinFlipTest {

//------------ // Flips a coin multiple times and counts the number of heads // and tails that result. Locks and unlocks coins //----------------------------------------------------------

public static void main(String[] args) {

Coin2 myCoin = new Coin2(); myCoin.setKey(1111);

System.out.println("Initial: " + myCoin); myCoin.lock(1111);

System.out.println("After lock: " + myCoin); myCoin.flip(); System.out.println("After attempted flip: " + myCoin);

myCoin.unlock(1111); myCoin.flip(); System.out.println("After unlock: " + myCoin); }

Output should look like the following: (Heads and Tails maybe different)

Initial: Tails

After lock: LOCKED

After attempted flip: LOCKED

After unlock: Tails

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

Step: 3

blur-text-image

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

Spatial Databases With Application To GIS

Authors: Philippe Rigaux, Michel Scholl, Agnès Voisard

1st Edition

1558605886, 978-1558605886

More Books

Students also viewed these Databases questions

Question

Question What are the advantages of a written bonus plan?

Answered: 1 week ago