Menu
I’m so confused. Does anyone know how to make a lockable method, lockable class, and lockable interface???
<i>
</i>public interface Lockable
{
void setKey(int key);
<i> </i>void lock(int key);
<i> </i>void unlock(int key);
<i> </i>boolean locked();
}
an interface is a collection of abstract methods that a class can incorporate[/QUOTE]Correction -- [b]must[/b] incorporate.
<i>
</i> public Coin()
{
if (locked() == true)
{
System.out.println("Method COIN is locked.");
}
else
{
flip();
}
}
[B]Coin.java:1: Coin is not abstract and does not override abstract method locked() in LockableThese errors happen because you have not put an implementation of the [font=courier]locked()[/font] method in your [font=courier]Coin[/font] class. So, for the first error, it happens because you have [font=courier]public class Coin [i]implements Lockable[/i][/font] and when you implement an interface, you [B]must[/B] include the methods of that interface in your class (in this case [font=courier]void lock(int key), void unlock(int key), void setKey(int key), [/font]and [font=courier]boolean locked()[/font]). The other two errors happen simply because those lines are calling a method that doesn't exist.
public class Coin implements Lockable
^
Coin.java:10: cannot resolve symbol
symbol : method locked ()
location: class Coin
if (locked() == true)
^
Coin.java:22: cannot resolve symbol
symbol : method locked ()
location: class Coin
if (locked() == true)
^[/B][/QUOTE]
<i>
</i>public class Coin implements Lockable
{
private final int HEADS = 0;
private final int TAILS = 0;
<i> </i>private int face;
<i> </i>//start interface methods
<i> </i>void setKey(int key)
<i> </i>{
<i> </i> int password = key0r;
<i> </i> return password;
<i> </i>}
<i> </i>void lock(int userKey)
<i> </i>{
<i> </i> int userpass = userKey;
<i> </i> int accessKey = setKey();
<i> </i> boolean result = locked();
<i> </i> if (userpass != accessKey)
<i> </i> {
<i> </i> System.out.println("The following methods have been locked.");
<i> </i> }
<i> </i> else
<i> </i> {
<i> </i> System.out.println("The following methods have been unlocked.");
<i> </i> }
<i> </i>}
<i> </i>void unlock(int userKey)
<i> </i>{
<i> </i> int userpass = userKey;
<i> </i> int password = pw;
<i> </i> if (userpass != password)
<i> </i> {
<i> </i> System.out.println("The following methods have been locked.");
<i> </i> }
<i> </i> else
<i> </i> {
<i> </i> System.out.println("The following methods have been unlocked.");
<i> </i> }
<i> </i>}
<i> </i>boolean locked()
<i> </i>{
<i> </i> int getPassword = setKey();
<i> </i>}
<i> </i>//end interface methods
<i> </i>public Coin()
<i> </i>{
<i> </i> boolean result = locked();
<i> </i> if (result == true)
<i> </i> {
<i> </i> System.out.println("Method COIN is locked.");
<i> </i> }
<i> </i> else
<i> </i> {
<i> </i> flip();
<i> </i> }
<i> </i>}
<i> </i>public void flip ()
<i> </i>{
<i> </i> boolean result = locked();
<i> </i> if (result == true)
<i> </i> {
<i> </i> System.out.println("Method FLIP is locked.");
<i> </i> }
<i> </i> else
<i> </i> {
<i> </i> face = (int) (Math.random() * 2);
<i> </i> }
<i> </i>}
// haven't work past here yet.
public boolean isHeads()
{
return (face == HEADS);
}
<i> </i>public String toString()
<i> </i>{
<i> </i> String faceName;
<i> </i> if(face == HEADS)
<i> </i> {
<i> </i> faceName = "Heads";
<i> </i> }
<i> </i> else
<i> </i> {
<i> </i> faceName = "Tails";
<i> </i> }
<i> </i> return faceName;
<i> </i>}
}
<i>
</i>import cs1.Keyboard;
public class LockableCoin implements Lockable
{
private final int HEADS = 0;
private final int TAILS = 0;
<i> </i>private int face;
<i> </i>private boolean isLocked;
<i> </i>private Code password;
<i> </i>//start interface methods
<i> </i>public void setKey(int key)
<i> </i>{
<i> </i> Code password;
<i> </i> int pw = key;
<i> </i> password = new Code(pw);
<i> </i>}
<i> </i>public void lock(int key)
<i> </i>{
<i> </i> int inputKey;
<i> </i> boolean isLocked = locked();
<i> </i> System.out.print("Enter the password: ");
<i> </i> inputKey = Keyboard.readInt();
<i> </i> if(isLocked == false)
<i> </i> {
<i> </i> if(password == inputKey)
<i> </i> {
<i> </i> isLocked = true;
<i> </i> }
<i> </i> }
<i> </i> else if (isLocked == true )
<i> </i> {
<i> </i> if(password == inputKey)
<i> </i> {
<i> </i> isLocked = true;
<i> </i> }
<i> </i> }
<i> </i>}
<i> </i>public void unlock(int key)
<i> </i>{
<i> </i> int inputKey;
<i> </i> boolean isLocked = locked();
<i> </i> System.out.print("Enter the password: ");
<i> </i> inputKey = Keyboard.readInt();
<i> </i> if(isLocked == false)
<i> </i> {
<i> </i> if(password == inputKey)
<i> </i> {
<i> </i> isLocked = true;
<i> </i> }
<i> </i> }
<i> </i> else if (isLocked == true )
<i> </i> {
<i> </i> if(password == inputKey)
<i> </i> {
<i> </i> isLocked = true;
<i> </i> }
<i> </i> }
<i> </i>}
<i> </i>public boolean locked()
<i> </i>{
<i> </i>}
<i> </i>//end interface methods
<i> </i>public LockableCoin()
<i> </i>{
<i> </i> flip();
<i> </i>}
<i> </i>public void flip ()
<i> </i>{
<i> </i> face = (int) (Math.random() * 2);
<i> </i>}
<i> </i>public boolean isHeads()
<i> </i>{
<i> </i> return (face == HEADS);
<i> </i>}
<i> </i>public String toString()
<i> </i>{
<i> </i> String faceName;
<i> </i> if(isLocked == false)
<i> </i> {
<i> </i> if(face == HEADS)
<i> </i> {
<i> </i> faceName = "Heads";
<i> </i> }
<i> </i> else
<i> </i> {
<i> </i> faceName = "Tails";
<i> </i> }
<i> </i> }
<i> </i> return faceName;
<i> </i>}
}
0.1.9 — BETA 6.1