Java. The method isOpen() is undefined for the type Lock. Also need some help fixing some code/debugging
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
import java.io.*;
public class GentCPT3
{
public static void main (String args) throws IOException
{
BufferedReader objReader = new BufferedReader(new InputStreamReader (System.in));
System.out.println("Enter key");
int key1 = Integer.parseInt(objReader.readLine()); // set to 111
System.out.println("Enter key2");
int key2 = Integer.parseInt(objReader.readLine()); // set to 222
Lock lock1 = new Lock (key1);
Lock lock2 = new Lock (key2);
System.out.println(lock1.isOpen()); // prints false
lock1.close();
lock2.close();
lock1.open(111); // opens lock1
lock2.open(222); // opens lock2111
lock1.open(123); // fails to open
lock1.open(456); // fails to open
lock1.open(789); // fails - prints “Alarm”
}
}
class Lock //Initializing class
{
//Initializing variables
boolean isOpen;
int key;
int numAttempts = 0;
Lock(int key)
{
isOpen = false;
this.key = key;
}
public void close()//for incorrect combo
{
isOpen = false;
}
public void open(int key)//for correct combo
{
if(this.key == key)
{
isOpen = true;
}
else if(this.key != key && !isOpen)
{
numAttempts++;
}
if(numAttempts == 3)
{
System.out.println("ALARM");//prints alarm when the combo is incorrect **enter code here**3 times
}
}
}
I need help fixing the error mentioned in the title. Also a few other debugging things I need help with. feel free to edit the code if you think you can fix it. I need help setting the attempts for each combonation on the lock to 3 because as of right now its set to 2 and I don't know why
java
|
show 1 more comment
import java.io.*;
public class GentCPT3
{
public static void main (String args) throws IOException
{
BufferedReader objReader = new BufferedReader(new InputStreamReader (System.in));
System.out.println("Enter key");
int key1 = Integer.parseInt(objReader.readLine()); // set to 111
System.out.println("Enter key2");
int key2 = Integer.parseInt(objReader.readLine()); // set to 222
Lock lock1 = new Lock (key1);
Lock lock2 = new Lock (key2);
System.out.println(lock1.isOpen()); // prints false
lock1.close();
lock2.close();
lock1.open(111); // opens lock1
lock2.open(222); // opens lock2111
lock1.open(123); // fails to open
lock1.open(456); // fails to open
lock1.open(789); // fails - prints “Alarm”
}
}
class Lock //Initializing class
{
//Initializing variables
boolean isOpen;
int key;
int numAttempts = 0;
Lock(int key)
{
isOpen = false;
this.key = key;
}
public void close()//for incorrect combo
{
isOpen = false;
}
public void open(int key)//for correct combo
{
if(this.key == key)
{
isOpen = true;
}
else if(this.key != key && !isOpen)
{
numAttempts++;
}
if(numAttempts == 3)
{
System.out.println("ALARM");//prints alarm when the combo is incorrect **enter code here**3 times
}
}
}
I need help fixing the error mentioned in the title. Also a few other debugging things I need help with. feel free to edit the code if you think you can fix it. I need help setting the attempts for each combonation on the lock to 3 because as of right now its set to 2 and I don't know why
java
2
You don’t have a method namedisOpen
, just a field.
– Logan
Nov 21 '18 at 23:23
1
remove parenthesis inlock1.isOpen()
?
– Kartik
Nov 21 '18 at 23:28
I tried that and I don't get any errors, however my program doesn't function the way I want it to. I just need some 1 on 1 help tbh
– Joshua Gentile
Nov 21 '18 at 23:29
1
Also, for good design, you should make your class fields in Lock private and write getter and setter methods for them for other classes to use.
– Grant Foster
Nov 21 '18 at 23:29
2
@JoshuaGentile you can get 100 on 1 help here; all you have to do is update your question with specific issues you are having; explain them to the best of your ability
– Kartik
Nov 21 '18 at 23:31
|
show 1 more comment
import java.io.*;
public class GentCPT3
{
public static void main (String args) throws IOException
{
BufferedReader objReader = new BufferedReader(new InputStreamReader (System.in));
System.out.println("Enter key");
int key1 = Integer.parseInt(objReader.readLine()); // set to 111
System.out.println("Enter key2");
int key2 = Integer.parseInt(objReader.readLine()); // set to 222
Lock lock1 = new Lock (key1);
Lock lock2 = new Lock (key2);
System.out.println(lock1.isOpen()); // prints false
lock1.close();
lock2.close();
lock1.open(111); // opens lock1
lock2.open(222); // opens lock2111
lock1.open(123); // fails to open
lock1.open(456); // fails to open
lock1.open(789); // fails - prints “Alarm”
}
}
class Lock //Initializing class
{
//Initializing variables
boolean isOpen;
int key;
int numAttempts = 0;
Lock(int key)
{
isOpen = false;
this.key = key;
}
public void close()//for incorrect combo
{
isOpen = false;
}
public void open(int key)//for correct combo
{
if(this.key == key)
{
isOpen = true;
}
else if(this.key != key && !isOpen)
{
numAttempts++;
}
if(numAttempts == 3)
{
System.out.println("ALARM");//prints alarm when the combo is incorrect **enter code here**3 times
}
}
}
I need help fixing the error mentioned in the title. Also a few other debugging things I need help with. feel free to edit the code if you think you can fix it. I need help setting the attempts for each combonation on the lock to 3 because as of right now its set to 2 and I don't know why
java
import java.io.*;
public class GentCPT3
{
public static void main (String args) throws IOException
{
BufferedReader objReader = new BufferedReader(new InputStreamReader (System.in));
System.out.println("Enter key");
int key1 = Integer.parseInt(objReader.readLine()); // set to 111
System.out.println("Enter key2");
int key2 = Integer.parseInt(objReader.readLine()); // set to 222
Lock lock1 = new Lock (key1);
Lock lock2 = new Lock (key2);
System.out.println(lock1.isOpen()); // prints false
lock1.close();
lock2.close();
lock1.open(111); // opens lock1
lock2.open(222); // opens lock2111
lock1.open(123); // fails to open
lock1.open(456); // fails to open
lock1.open(789); // fails - prints “Alarm”
}
}
class Lock //Initializing class
{
//Initializing variables
boolean isOpen;
int key;
int numAttempts = 0;
Lock(int key)
{
isOpen = false;
this.key = key;
}
public void close()//for incorrect combo
{
isOpen = false;
}
public void open(int key)//for correct combo
{
if(this.key == key)
{
isOpen = true;
}
else if(this.key != key && !isOpen)
{
numAttempts++;
}
if(numAttempts == 3)
{
System.out.println("ALARM");//prints alarm when the combo is incorrect **enter code here**3 times
}
}
}
I need help fixing the error mentioned in the title. Also a few other debugging things I need help with. feel free to edit the code if you think you can fix it. I need help setting the attempts for each combonation on the lock to 3 because as of right now its set to 2 and I don't know why
java
java
edited Nov 21 '18 at 23:34
Joshua Gentile
asked Nov 21 '18 at 23:22
Joshua GentileJoshua Gentile
54
54
2
You don’t have a method namedisOpen
, just a field.
– Logan
Nov 21 '18 at 23:23
1
remove parenthesis inlock1.isOpen()
?
– Kartik
Nov 21 '18 at 23:28
I tried that and I don't get any errors, however my program doesn't function the way I want it to. I just need some 1 on 1 help tbh
– Joshua Gentile
Nov 21 '18 at 23:29
1
Also, for good design, you should make your class fields in Lock private and write getter and setter methods for them for other classes to use.
– Grant Foster
Nov 21 '18 at 23:29
2
@JoshuaGentile you can get 100 on 1 help here; all you have to do is update your question with specific issues you are having; explain them to the best of your ability
– Kartik
Nov 21 '18 at 23:31
|
show 1 more comment
2
You don’t have a method namedisOpen
, just a field.
– Logan
Nov 21 '18 at 23:23
1
remove parenthesis inlock1.isOpen()
?
– Kartik
Nov 21 '18 at 23:28
I tried that and I don't get any errors, however my program doesn't function the way I want it to. I just need some 1 on 1 help tbh
– Joshua Gentile
Nov 21 '18 at 23:29
1
Also, for good design, you should make your class fields in Lock private and write getter and setter methods for them for other classes to use.
– Grant Foster
Nov 21 '18 at 23:29
2
@JoshuaGentile you can get 100 on 1 help here; all you have to do is update your question with specific issues you are having; explain them to the best of your ability
– Kartik
Nov 21 '18 at 23:31
2
2
You don’t have a method named
isOpen
, just a field.– Logan
Nov 21 '18 at 23:23
You don’t have a method named
isOpen
, just a field.– Logan
Nov 21 '18 at 23:23
1
1
remove parenthesis in
lock1.isOpen()
?– Kartik
Nov 21 '18 at 23:28
remove parenthesis in
lock1.isOpen()
?– Kartik
Nov 21 '18 at 23:28
I tried that and I don't get any errors, however my program doesn't function the way I want it to. I just need some 1 on 1 help tbh
– Joshua Gentile
Nov 21 '18 at 23:29
I tried that and I don't get any errors, however my program doesn't function the way I want it to. I just need some 1 on 1 help tbh
– Joshua Gentile
Nov 21 '18 at 23:29
1
1
Also, for good design, you should make your class fields in Lock private and write getter and setter methods for them for other classes to use.
– Grant Foster
Nov 21 '18 at 23:29
Also, for good design, you should make your class fields in Lock private and write getter and setter methods for them for other classes to use.
– Grant Foster
Nov 21 '18 at 23:29
2
2
@JoshuaGentile you can get 100 on 1 help here; all you have to do is update your question with specific issues you are having; explain them to the best of your ability
– Kartik
Nov 21 '18 at 23:31
@JoshuaGentile you can get 100 on 1 help here; all you have to do is update your question with specific issues you are having; explain them to the best of your ability
– Kartik
Nov 21 '18 at 23:31
|
show 1 more comment
1 Answer
1
active
oldest
votes
The issue is that you are not closing the locks once you open them successfully. Change your calls to this:-
lock1.close();
lock2.close();
lock1.open(111); // opens lock1
lock2.open(222); // opens lock2111
lock1.close(); // NEW ADDITION
lock2.close(); // NEW ADDITION
lock1.open(123); // fails to open
lock1.open(456); // fails to open
lock1.open(789); // fails - prints “Alarm”
To further improve the code, reset numAttempts = 0
in your open()
when this.key == key
. And remove this.key != key
because it is always true
due to the previous if
condition.
so do I remove the parenthesis in isOpen?
– Joshua Gentile
Nov 22 '18 at 0:20
yes, parenthesis are used when you call a method.. butisOpen
is a field/variable in your code
– Kartik
Nov 22 '18 at 0:21
so wait, if I remove this.key != key, what do I do with the isOpen in the else if? do I just remove the whole line?
– Joshua Gentile
Nov 22 '18 at 0:25
just leave it there, so it will look likeelse if (!isOpen)
– Kartik
Nov 22 '18 at 0:26
okay, so I compile it without any errors now but when I run the program. when I type in the correct input, it comes up with false and ALARM. I want it to say that the lock is now open and then end the program
– Joshua Gentile
Nov 22 '18 at 0:29
|
show 7 more comments
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53421830%2fjava-the-method-isopen-is-undefined-for-the-type-lock-also-need-some-help-fi%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
The issue is that you are not closing the locks once you open them successfully. Change your calls to this:-
lock1.close();
lock2.close();
lock1.open(111); // opens lock1
lock2.open(222); // opens lock2111
lock1.close(); // NEW ADDITION
lock2.close(); // NEW ADDITION
lock1.open(123); // fails to open
lock1.open(456); // fails to open
lock1.open(789); // fails - prints “Alarm”
To further improve the code, reset numAttempts = 0
in your open()
when this.key == key
. And remove this.key != key
because it is always true
due to the previous if
condition.
so do I remove the parenthesis in isOpen?
– Joshua Gentile
Nov 22 '18 at 0:20
yes, parenthesis are used when you call a method.. butisOpen
is a field/variable in your code
– Kartik
Nov 22 '18 at 0:21
so wait, if I remove this.key != key, what do I do with the isOpen in the else if? do I just remove the whole line?
– Joshua Gentile
Nov 22 '18 at 0:25
just leave it there, so it will look likeelse if (!isOpen)
– Kartik
Nov 22 '18 at 0:26
okay, so I compile it without any errors now but when I run the program. when I type in the correct input, it comes up with false and ALARM. I want it to say that the lock is now open and then end the program
– Joshua Gentile
Nov 22 '18 at 0:29
|
show 7 more comments
The issue is that you are not closing the locks once you open them successfully. Change your calls to this:-
lock1.close();
lock2.close();
lock1.open(111); // opens lock1
lock2.open(222); // opens lock2111
lock1.close(); // NEW ADDITION
lock2.close(); // NEW ADDITION
lock1.open(123); // fails to open
lock1.open(456); // fails to open
lock1.open(789); // fails - prints “Alarm”
To further improve the code, reset numAttempts = 0
in your open()
when this.key == key
. And remove this.key != key
because it is always true
due to the previous if
condition.
so do I remove the parenthesis in isOpen?
– Joshua Gentile
Nov 22 '18 at 0:20
yes, parenthesis are used when you call a method.. butisOpen
is a field/variable in your code
– Kartik
Nov 22 '18 at 0:21
so wait, if I remove this.key != key, what do I do with the isOpen in the else if? do I just remove the whole line?
– Joshua Gentile
Nov 22 '18 at 0:25
just leave it there, so it will look likeelse if (!isOpen)
– Kartik
Nov 22 '18 at 0:26
okay, so I compile it without any errors now but when I run the program. when I type in the correct input, it comes up with false and ALARM. I want it to say that the lock is now open and then end the program
– Joshua Gentile
Nov 22 '18 at 0:29
|
show 7 more comments
The issue is that you are not closing the locks once you open them successfully. Change your calls to this:-
lock1.close();
lock2.close();
lock1.open(111); // opens lock1
lock2.open(222); // opens lock2111
lock1.close(); // NEW ADDITION
lock2.close(); // NEW ADDITION
lock1.open(123); // fails to open
lock1.open(456); // fails to open
lock1.open(789); // fails - prints “Alarm”
To further improve the code, reset numAttempts = 0
in your open()
when this.key == key
. And remove this.key != key
because it is always true
due to the previous if
condition.
The issue is that you are not closing the locks once you open them successfully. Change your calls to this:-
lock1.close();
lock2.close();
lock1.open(111); // opens lock1
lock2.open(222); // opens lock2111
lock1.close(); // NEW ADDITION
lock2.close(); // NEW ADDITION
lock1.open(123); // fails to open
lock1.open(456); // fails to open
lock1.open(789); // fails - prints “Alarm”
To further improve the code, reset numAttempts = 0
in your open()
when this.key == key
. And remove this.key != key
because it is always true
due to the previous if
condition.
answered Nov 21 '18 at 23:49
KartikKartik
4,52231537
4,52231537
so do I remove the parenthesis in isOpen?
– Joshua Gentile
Nov 22 '18 at 0:20
yes, parenthesis are used when you call a method.. butisOpen
is a field/variable in your code
– Kartik
Nov 22 '18 at 0:21
so wait, if I remove this.key != key, what do I do with the isOpen in the else if? do I just remove the whole line?
– Joshua Gentile
Nov 22 '18 at 0:25
just leave it there, so it will look likeelse if (!isOpen)
– Kartik
Nov 22 '18 at 0:26
okay, so I compile it without any errors now but when I run the program. when I type in the correct input, it comes up with false and ALARM. I want it to say that the lock is now open and then end the program
– Joshua Gentile
Nov 22 '18 at 0:29
|
show 7 more comments
so do I remove the parenthesis in isOpen?
– Joshua Gentile
Nov 22 '18 at 0:20
yes, parenthesis are used when you call a method.. butisOpen
is a field/variable in your code
– Kartik
Nov 22 '18 at 0:21
so wait, if I remove this.key != key, what do I do with the isOpen in the else if? do I just remove the whole line?
– Joshua Gentile
Nov 22 '18 at 0:25
just leave it there, so it will look likeelse if (!isOpen)
– Kartik
Nov 22 '18 at 0:26
okay, so I compile it without any errors now but when I run the program. when I type in the correct input, it comes up with false and ALARM. I want it to say that the lock is now open and then end the program
– Joshua Gentile
Nov 22 '18 at 0:29
so do I remove the parenthesis in isOpen?
– Joshua Gentile
Nov 22 '18 at 0:20
so do I remove the parenthesis in isOpen?
– Joshua Gentile
Nov 22 '18 at 0:20
yes, parenthesis are used when you call a method.. but
isOpen
is a field/variable in your code– Kartik
Nov 22 '18 at 0:21
yes, parenthesis are used when you call a method.. but
isOpen
is a field/variable in your code– Kartik
Nov 22 '18 at 0:21
so wait, if I remove this.key != key, what do I do with the isOpen in the else if? do I just remove the whole line?
– Joshua Gentile
Nov 22 '18 at 0:25
so wait, if I remove this.key != key, what do I do with the isOpen in the else if? do I just remove the whole line?
– Joshua Gentile
Nov 22 '18 at 0:25
just leave it there, so it will look like
else if (!isOpen)
– Kartik
Nov 22 '18 at 0:26
just leave it there, so it will look like
else if (!isOpen)
– Kartik
Nov 22 '18 at 0:26
okay, so I compile it without any errors now but when I run the program. when I type in the correct input, it comes up with false and ALARM. I want it to say that the lock is now open and then end the program
– Joshua Gentile
Nov 22 '18 at 0:29
okay, so I compile it without any errors now but when I run the program. when I type in the correct input, it comes up with false and ALARM. I want it to say that the lock is now open and then end the program
– Joshua Gentile
Nov 22 '18 at 0:29
|
show 7 more comments
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53421830%2fjava-the-method-isopen-is-undefined-for-the-type-lock-also-need-some-help-fi%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
2
You don’t have a method named
isOpen
, just a field.– Logan
Nov 21 '18 at 23:23
1
remove parenthesis in
lock1.isOpen()
?– Kartik
Nov 21 '18 at 23:28
I tried that and I don't get any errors, however my program doesn't function the way I want it to. I just need some 1 on 1 help tbh
– Joshua Gentile
Nov 21 '18 at 23:29
1
Also, for good design, you should make your class fields in Lock private and write getter and setter methods for them for other classes to use.
– Grant Foster
Nov 21 '18 at 23:29
2
@JoshuaGentile you can get 100 on 1 help here; all you have to do is update your question with specific issues you are having; explain them to the best of your ability
– Kartik
Nov 21 '18 at 23:31