java problem with interface assigment- need help!

Hi all, I'm an undergrad student....taking object oriented Java at school (one of the required classes)...though programming isn't my major. I'm not very good at Java, and am wondering if anyone could help me solve the following problem:


Design 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. Redesign and implement a version of Coin class so its Lockable:

public class Coin
{
private final int HEADS = 0;
private final int TAILS = 1;

private int face;

public 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;
}
}


Any help is greatly appreciated. Thanks.

Comments

  • To create an interface you need to create it as you would a class, except add the interface keyword. Then add the required method definitions, do not implement them, just define them like you would an abstract method.

    It would look similar to this:

    [code]
    public interface Lockable
    {

    void setKey(int val);
    ...
    }
    [/code]

    Then in your coin class implement the Lockable interface and provide all all the methods along with an implementation of them.

    Hope that sets you in the right direction.

    : Hi all, I'm an undergrad student....taking object oriented Java at school (one of the required classes)...though programming isn't my major. I'm not very good at Java, and am wondering if anyone could help me solve the following problem:
    :
    :
    : Design 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. Redesign and implement a version of Coin class so its Lockable:
    :
    : public class Coin
    : {
    : private final int HEADS = 0;
    : private final int TAILS = 1;
    :
    : private int face;
    :
    : public 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;
    : }
    : }
    :
    :
    : Any help is greatly appreciated. Thanks.
    :
    :

  • Thanks for your help.......I'm really bad at Java.......but I'll see if I can figure it out now.
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories