Coding

0 comments

This assignment can be completed either individual or by a group of 2 students, ensure to submit a zip file of all the .java files to canvas and name the zip file by your first and last name (example: Joe_Stuart.zip) and if you are a group of 2 make sure the zip file has both the students first and last name (example: Sally_Jones-Joe_Stuart.zip)

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

About the Author

Follow me


{"email":"Email address invalid","url":"Website address invalid","required":"Required field missing"}