Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Implementing a Driver to following code

  1. #1
    Junior Member
    Join Date
    Oct 2011
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Implementing a Driver to following code

    So here are my code segements:

    1. The Interface with problem Im trying to solve

     
    // Design a Java interface 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 indicating whether or not the object is locked. A Lockable object
    // represents an object whose regular methods are protected: if the object is
    // locked, the method cannot be invoked; if it is unlocked, they can be invoked.
    // Redesign and implement a version of this Coin class so that it is Lockable.
     
    public interface Lockable
    {
      boolean locked();
      void setKey(int password);
      boolean lock(int password);
      boolean unlock(int password);
     
      }

    2. The class coin with modifications:

     
     
    public class Coin implements Lockable 
    {
      private int key; // password to make it lockable
      private boolean locked; // if true, object is locked
      private final int HEADS = 0;
      private final int TAILS = 1;
      private int face;
     
      public boolean locked() 
      {
        return this.locked;
      }
     
      public void setKey(int password) 
      {
        this.key = password;
        this.locked = true;
     
      }
     
      public boolean lock(int password) 
      {
        if (password == this.key) 
        {
          this.locked = true;
          return true;
        } 
        else 
        {
          return false;
        }
      }
     
      public boolean unlock(int password) 
      {
        if (password == this.key) 
        {
          this.locked = false;
          return true;
        } 
        else 
        {
          return false;
        }
      }
     
      public Coin()
      {
        locked = true;
      }
     
      public boolean flip() 
      {
        if (this.locked == false) 
        {
          face = (int) (Math.random() * 2);
          return true;
        }
        return false;
      }
     
     
      public boolean isHeads() 
      {
        return (face == HEADS);
      }
     
      public String toString() 
      {
        if (this.locked == false) 
        {
     
          String faceName;
     
          if (face == HEADS)
            faceName = "Heads";
          else
            faceName = "Tails";
     
          return faceName;
        } 
        else
          return "";
      }
    }

    3. Since the questions asked for only the methods, i believe thats as far he wants us to go but he made a driver to test if it worked, something similar to this

     
    public class Driver
    {
      public static void main(String[] args) 
      {
     
        Coin myCoin = new Coin();
        boolean bSuccess, bResult;
     
        myCoin.setKey(1234);
        bSuccess = myCoin.flip(); // Should Fail, coin is locked
        bResult = myCoin.unlock(12345);
        if (bResult) // coin is unlocked
        {
          myCoin.flip();
          boolean iValue = myCoin.isHeads();
        }
        else
        {
          System.out.println("You better remember the LOCK value");
        }
     
     
     
      }
     
    }

    Some errors I get when running this:
    1. After prompting for the password it does nothing
    2. Even i tried to tidy the code, some variables were left behind since the program for this situation.

    If someone could help me fix my Driver class and allowing Coin to implement it

    Thanks everyone in Advance!


  2. #2
    Junior Member
    Join Date
    Apr 2011
    Location
    Pune, India
    Posts
    11
    My Mood
    Cool
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Default Re: Implementing a Driver to following code

    Hi,

    i have check the code its working fine.

    1) Very first thing your code is not asking any password.
    2) your give code is compiling and running without error.

    What extactly you expect as result and as input?
    Hardik Jadhav

  3. #3
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: Implementing a Driver to following code

    bResult = myCoin.unlock(12345);
    Your key is set to 1234 and you are passing 12345 so it will return false.
    So the output must be;
    You better remember the LOCK value

  4. #4
    Junior Member
    Join Date
    Oct 2011
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Implementing a Driver to following code

    The output I want for this program to do is when i run it, it will ask for a password, if its wrong, it will prompt "You better remember the Lock value" if its the correct password, it wil run the program

    What must I add to my code in order for it to work?

    Thanks!

  5. #5
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: Implementing a Driver to following code

    You must add;
    1. Take values from the user.
    2. Pass his value to the function which will perform your desired functions.

    Good Luck.

Similar Threads

  1. [SOLVED] Help with creating a class and driver
    By JackCannon15 in forum Object Oriented Programming
    Replies: 1
    Last Post: October 27th, 2011, 03:50 PM
  2. No suitable driver found
    By Josheh in forum JDBC & Databases
    Replies: 2
    Last Post: October 16th, 2011, 01:12 AM
  3. Replies: 5
    Last Post: September 17th, 2011, 09:05 PM
  4. Link From A Driver Class To The Subclasses
    By angels in forum What's Wrong With My Code?
    Replies: 1
    Last Post: May 29th, 2011, 07:39 AM
  5. Implementing HTML tags in Java Source Code
    By bookface in forum Java Theory & Questions
    Replies: 4
    Last Post: March 19th, 2010, 09:29 PM