Results 1 to 2 of 2
  1. #1
    Hector is offline Member
    Join Date
    Feb 2014
    Seattle, Washington
    Rep Power

    Default Lockable interface and driver

    im having trouble getting this program to run correctly and getting the code right. i also want sure whether or not to put this in the advanced or new to programming sections of this forum; so if you'd let me know where something like this should go next time i post something let me know.
    the programming project is to design a java interface called Lockable that includes the following methods: setKey, lock, unlock, and locked. the seyKey, 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 used is correct. the locked method returns a boolean of true for locked and false for unlocked. a Lockable object is 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.
    When i run the program, i get "locked" even when i enter the correct key(555). i need it to return the correct word for when i enter the correct number. also, if it seems like i have misunderstood the idea of the program, please let me know. thanks in advance!

    Lockable interface:
    Java Code:
    public interface Lockable {
    	  public void setKey(int newkey);
    	  public void lock(int key);
    	  public void unlock(int key);
    	  public boolean locked();
    Class used to pass the key:
    Java Code:
    public class Pass implements Lockable{
    	private int k, setKey; 
    	private boolean locked; 
    	public Pass(int key){ 
    	k = key;
    	locked = false;
    	public void setKey(int newKey) { 
    	newKey = 555;
    	setKey = newKey; 
    	public void lock(int key) { 
    	if(k == setKey)
    	locked = true; 
    	public void unlock(int key) { 
    	if ( k == setKey) 
    	locked = false; 
    	public boolean locked() { 
    	return locked; 
    	public String toString(){ 
    	if(locked = true)
    		return "Locked"; 
    		return "Unlocked"; 
    Java Code:
    import java.util.Scanner;
    public class PassDriver {
    	public static void main(String[] args) {
    		Scanner scan = new Scanner(;
    		int keyGuess;
    		System.out.println("Enter key: ");
    		keyGuess = scan.nextInt();
    		Pass key = new Pass(keyGuess);

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Washington, DC
    Rep Power

    Default Re: Lockable interface and driver

    Java Code:
    if(locked = true)
    Pretty sure you meant:

    Java Code:
    if(locked == true)
    Which can shortened to just:

    Java Code:
    Also, you should be using curly brackets for every if statement, even if they are just one line.

    Java Code:
       return "locked";
       return "unlocked";
    How to Ask Questions the Smart Way
    Static Void Games - GameDev tutorials, free Java and JavaScript hosting!
    Static Void Games forum - Come say hello!

Similar Threads

  1. Replies: 1
    Last Post: 07-08-2012, 06:23 PM
  2. Replies: 0
    Last Post: 03-03-2012, 02:01 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts