Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a Java interface called Lockable that includes the following methods: setK

ID: 3573342 • Letter: W

Question

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 Coin {

   private final int HEADS =0;
   private final int TAILS =1;
  
   private int face;
  
   public void Coin() {
  
       flip();
   }
   public void flip(){
       face = (int)(Math.random()*2);
   }
  
   public boolean isHeads(){
       return(face == HEADS);
   }
   public String toString(){
      
       String faceName;
       if(face==HEADS)
           faceName = "Heads";
       else
           faceName = "Tails";
       return faceName;
   }
}

//******************
///Coinflip.java
//totals the total amount of heads and class from the coin class
//*******************

public class coinflip {

   public static void main(String[] args) {
      
       int head;
       int tail;
      
       Coin coin = new Coin();
       head = 0;
       tail = 0;
      
       for( int num = 0; num < 100; num ++){
          
           coin.flip();
          
           if(coin.isHeads())
               head++;
           else
               tail++;
           }
       System.out.println("The amount of heads is: "+ head+ " The amount of tails is: "+ tail);
      
   }
}

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

public class Coin2Test {

//------------ // 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

Explanation / Answer

//Please find the code for both the Lockable interface and the Coin2 class

PROGRAM CODE:

Lockable.java

package samplepro;

public interface Lockable {

   public void setKey(int key);
   public void lock(int key);
   public void unlock(int key);
   public boolean locked();
}

Coin2.java

package samplepro;

public class Coin2 implements Lockable{

   private final int HEADS =0;
   private final int TAILS =1;
   private final int LOCKED = 2;
   private int key = 0;
   private int face;
  
   public Coin2()
   {
       flip();
   }
   @Override
   public void setKey(int key) {
       this.key = key;
      
   }

   @Override
   public void lock(int key) {
       if(this.key == key)
           face = LOCKED;
   }

   @Override
   public void unlock(int key) {
       if(this.key == key)
           face = -1;
      
   }

   @Override
   public boolean locked() {
       if(face == LOCKED)
           return true;
       else return false;
   }
  
   public void flip(){
   if(face != LOCKED)
       face = (int)(Math.random()*2);
   }

   public boolean isHeads(){
   return(face == HEADS);
   }
   public String toString(){
  
   String faceName;
   if(face==HEADS)
   faceName = "Heads";
   else if(face == TAILS)
   faceName = "Tails";
   else
       faceName = "LOCKED";
   return faceName;
   }
  
}

OUTPUT:

Initial: Tails
After lock: LOCKED
After attempted flip: LOCKED
After unlock: Tails

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote