Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Please help me with this programming project. Please also write in Java. Write a Java interface called Lockable that includes the following methods: setKey, lock,

Please help me with this programming project. Please also write in Java.

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 from Chapter 5 so that it is Lockable.

This is the Coin.java program

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

====================================================================

Here is the Lockable.java program

//******************************************************************** // Lockable.java Author: Lewis and Loftus // // Solution to Programming Project 7.8 and 7.9 //********************************************************************

public interface Lockable { //----------------------------------------------------------------- // Sets the object's key. //----------------------------------------------------------------- public void setKey(int key);

//----------------------------------------------------------------- // Locks the object if the proper key is provided. //----------------------------------------------------------------- public void lock(int key);

//----------------------------------------------------------------- // Unlocks the object if the proper key is provided. //----------------------------------------------------------------- public void unlock(int key);

//----------------------------------------------------------------- // Returns true it the object is locked, else returns false. //----------------------------------------------------------------- public boolean locked(); }

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

Database Application Development And Design

Authors: Michael V. Mannino

1st Edition

0072463678, 978-0072463675

More Books

Students also viewed these Databases questions