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

    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; 
    	return;
    	}
    
    	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"; 
    	else 
    		return "Unlocked"; 
    	} 
    	}
    Driver:
    Java Code:
    import java.util.Scanner;
    public class PassDriver {
    
    	public static void main(String[] args) {
    		Scanner scan = new Scanner(System.in);
    		int keyGuess;
    		System.out.println("Enter key: ");
    		keyGuess = scan.nextInt();
    		Pass key = new Pass(keyGuess);
    		System.out.println(key);
    		
    		}
    
    }

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

    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:
    if(locked)
    Also, you should be using curly brackets for every if statement, even if they are just one line.

    Java Code:
    if(locked){
       return "locked";
    }
    else{
       return "unlocked";
    }
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. Replies: 1
    Last Post: 07-08-2012, 05:23 PM
  2. Replies: 0
    Last Post: 03-03-2012, 01: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
  •