/    Sign up×
Community /Pin to ProfileBookmark

How to make a lockable method??

I’m so confused. Does anyone know how to make a lockable method, lockable class, and lockable interface???

to post a comment
Java

12 Comments(s)

Copy linkTweet thisAlerts:
@ray326Nov 12.2004 — Lockable? Is that something new in 1.5?
Copy linkTweet thisAlerts:
@BuezaWebDevauthorNov 13.2004 — Shiet.

I was working at it in school today, and I found out that it's all in my interface (for all you java beginners...an interface is a collection of abstract methods that a class can incorporate).

so, here's my interface code.

<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();
}


Now, the reason why the [b]locked[/b] method is boolean is because I want it to identify if other methods are locked or unlocked.

My program has to do with a small banking system. It's pretty confusing. I'm just hacking away at it now. I'll keep you guys updated.
Copy linkTweet thisAlerts:
@Khalid_AliNov 13.2004 — I think what you are looking for is to look into [b]Synchronization[/b] in Java.
Copy linkTweet thisAlerts:
@ray326Nov 13.2004 — an interface is a collection of abstract methods that a class can incorporate[/QUOTE] Correction -- [b]must[/b] incorporate.

Interfaces apply to classes, not methods.
Copy linkTweet thisAlerts:
@ray326Nov 13.2004 — /deleted/
Copy linkTweet thisAlerts:
@BuezaWebDevauthorNov 14.2004 — Hey guys,

How do you check if a method is returning true or false??


for example, if my locked(); somehow returns true...

can i go

<i>
</i> public Coin()
{
if (locked() == true)
{
System.out.println("Method COIN is locked.");
}
else
{
flip();
}
}


I keep getting these error:

[b]

Coin.java:1: Coin is not abstract and does not override abstract method locked() in Lockable

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)

^

3 errors

[/b]
Copy linkTweet thisAlerts:
@Khalid_AliNov 14.2004 — you will need to provide us with full class structure that you have to figure out where you are making mistakes.
Copy linkTweet thisAlerts:
@HaganeNoKokoroNov 14.2004 — [B]Coin.java:1: Coin is not abstract and does not override abstract method locked() in Lockable

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]
These 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.
Copy linkTweet thisAlerts:
@BuezaWebDevauthorNov 14.2004 — <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 implemented them in, but how do I retrieve the value of setKey?? I want to do if statements where it'll say "If(userKey == setKey) {

allow them to use this method} else{ do not allow them to use it}"

Can anyone please help?
Copy linkTweet thisAlerts:
@BuezaWebDevauthorNov 14.2004 — Updated code.

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


I get the following errors:

[b]

LockableCoin.java:11: cannot resolve symbol

symbol : class Code

location: class LockableCoin

private Code password;

^

LockableCoin.java:16: cannot resolve symbol

symbol : class Code

location: class LockableCoin

Code password;

^

LockableCoin.java:18: cannot resolve symbol

symbol : class Code

location: class LockableCoin

password = new Code(pw);

^

3 errors

[/b]



Anyone know what the heck it's asking?? lol.
Copy linkTweet thisAlerts:
@HaganeNoKokoroNov 15.2004 — It cannot find the [font=courier]Code[/font] class. If it's from some package, then you need to import that package into your code. Otherwise, if it's a class you created, then you have to have the Code.class file in the same directory, or import the package you put it in, or something.

Also, it looks like your [font=courier]boolean locked()[/font] method is empty. You will get an error if it does not return a boolean.
Copy linkTweet thisAlerts:
@BuezaWebDevauthorNov 15.2004 — My intentions for new code(password); would be to make a wrapper class. :/

But yeah, after hours of debugging, I got it to work, finally. lol.

Thanks guys !
×

Success!

Help @BuezaWebDev spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.1,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,
)...