java. Lock problem with an issue regarding attempts
Writing a program with a class named Lock that user inputs attmpt to open. If they guess the correct combination, they move on to the second lock. The user gets 3 attempts to correctly solve each lock. If guess incorectly 3 times in a row for each lock, they are met with an alarm
So far I have my program doing mostly what I want it to do, however, I only get one attempt per lock and when the user guesses the combo incorrectly, the program just goes on to lock number 2. Here is my code:
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 lock2
lock1.open(111);
lock2.open(222);
lock1.open(111);
lock2.open(222);
}
}
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)
{
System.out.println("Opened");
isOpen = true;
}
else if(!isOpen)
{
numAttempts++;
}
if(numAttempts == 3)
{
System.out.println("ALARM");//prints alarm when the combo is incorrect 3 times
}
}
}
java
add a comment |
Writing a program with a class named Lock that user inputs attmpt to open. If they guess the correct combination, they move on to the second lock. The user gets 3 attempts to correctly solve each lock. If guess incorectly 3 times in a row for each lock, they are met with an alarm
So far I have my program doing mostly what I want it to do, however, I only get one attempt per lock and when the user guesses the combo incorrectly, the program just goes on to lock number 2. Here is my code:
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 lock2
lock1.open(111);
lock2.open(222);
lock1.open(111);
lock2.open(222);
}
}
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)
{
System.out.println("Opened");
isOpen = true;
}
else if(!isOpen)
{
numAttempts++;
}
if(numAttempts == 3)
{
System.out.println("ALARM");//prints alarm when the combo is incorrect 3 times
}
}
}
java
You need to have a loop in your main method so that the user can guess several times but why is the user giving the answer (key) as input?
– Joakim Danielson
Nov 22 '18 at 12:49
The input is meant to check if the combo is right.
– Joshua Gentile
Nov 22 '18 at 12:56
If you see a way to fix it feel free to edit
– Joshua Gentile
Nov 22 '18 at 12:56
You didn't answer my question
– Joakim Danielson
Nov 22 '18 at 13:18
they aren't giving the answer key
– Joshua Gentile
Nov 22 '18 at 14:38
add a comment |
Writing a program with a class named Lock that user inputs attmpt to open. If they guess the correct combination, they move on to the second lock. The user gets 3 attempts to correctly solve each lock. If guess incorectly 3 times in a row for each lock, they are met with an alarm
So far I have my program doing mostly what I want it to do, however, I only get one attempt per lock and when the user guesses the combo incorrectly, the program just goes on to lock number 2. Here is my code:
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 lock2
lock1.open(111);
lock2.open(222);
lock1.open(111);
lock2.open(222);
}
}
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)
{
System.out.println("Opened");
isOpen = true;
}
else if(!isOpen)
{
numAttempts++;
}
if(numAttempts == 3)
{
System.out.println("ALARM");//prints alarm when the combo is incorrect 3 times
}
}
}
java
Writing a program with a class named Lock that user inputs attmpt to open. If they guess the correct combination, they move on to the second lock. The user gets 3 attempts to correctly solve each lock. If guess incorectly 3 times in a row for each lock, they are met with an alarm
So far I have my program doing mostly what I want it to do, however, I only get one attempt per lock and when the user guesses the combo incorrectly, the program just goes on to lock number 2. Here is my code:
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 lock2
lock1.open(111);
lock2.open(222);
lock1.open(111);
lock2.open(222);
}
}
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)
{
System.out.println("Opened");
isOpen = true;
}
else if(!isOpen)
{
numAttempts++;
}
if(numAttempts == 3)
{
System.out.println("ALARM");//prints alarm when the combo is incorrect 3 times
}
}
}
java
java
asked Nov 22 '18 at 12:39


Joshua GentileJoshua Gentile
54
54
You need to have a loop in your main method so that the user can guess several times but why is the user giving the answer (key) as input?
– Joakim Danielson
Nov 22 '18 at 12:49
The input is meant to check if the combo is right.
– Joshua Gentile
Nov 22 '18 at 12:56
If you see a way to fix it feel free to edit
– Joshua Gentile
Nov 22 '18 at 12:56
You didn't answer my question
– Joakim Danielson
Nov 22 '18 at 13:18
they aren't giving the answer key
– Joshua Gentile
Nov 22 '18 at 14:38
add a comment |
You need to have a loop in your main method so that the user can guess several times but why is the user giving the answer (key) as input?
– Joakim Danielson
Nov 22 '18 at 12:49
The input is meant to check if the combo is right.
– Joshua Gentile
Nov 22 '18 at 12:56
If you see a way to fix it feel free to edit
– Joshua Gentile
Nov 22 '18 at 12:56
You didn't answer my question
– Joakim Danielson
Nov 22 '18 at 13:18
they aren't giving the answer key
– Joshua Gentile
Nov 22 '18 at 14:38
You need to have a loop in your main method so that the user can guess several times but why is the user giving the answer (key) as input?
– Joakim Danielson
Nov 22 '18 at 12:49
You need to have a loop in your main method so that the user can guess several times but why is the user giving the answer (key) as input?
– Joakim Danielson
Nov 22 '18 at 12:49
The input is meant to check if the combo is right.
– Joshua Gentile
Nov 22 '18 at 12:56
The input is meant to check if the combo is right.
– Joshua Gentile
Nov 22 '18 at 12:56
If you see a way to fix it feel free to edit
– Joshua Gentile
Nov 22 '18 at 12:56
If you see a way to fix it feel free to edit
– Joshua Gentile
Nov 22 '18 at 12:56
You didn't answer my question
– Joakim Danielson
Nov 22 '18 at 13:18
You didn't answer my question
– Joakim Danielson
Nov 22 '18 at 13:18
they aren't giving the answer key
– Joshua Gentile
Nov 22 '18 at 14:38
they aren't giving the answer key
– Joshua Gentile
Nov 22 '18 at 14:38
add a comment |
2 Answers
2
active
oldest
votes
Here is my solution. I did some minor changes to Lock
where I removed any printing and added a member, isFail
, to keep track of when too many guesses has been done.
class Lock {
boolean isOpen;
boolean isFail;
int key;
int numAttempts = 0;
Lock(int key) {
isOpen = false;
isFail = false;
this.key = key;
}
public void open(int key) {
if (isOpen) {
return;
}
if(this.key == key) {
isOpen = true;
} else {
numAttempts++;
isFail = numAttempts == 3;
}
}
}
The major changes is in the GentCPT3
class where each guess for a lock is done in specific method and that method is called once for each lock. Note that I have hardcoded the values to guess since it is not clear from the question where they come from. Maybe using the Random
class here to generate two keys would be an option.
import java.io.*;
public class GentCPT3 {
public static void main (String args) throws IOException {
BufferedReader objReader = new BufferedReader(new InputStreamReader (System.in));
if (guess(objReader, new Lock (3))) {
guess(objReader, new Lock(7));
}
objReader.close();
}
private static boolean guess(BufferedReader objReader, Lock lock) throws IOException {
while (true) {
System.out.println("Guess value");
int key1 = Integer.parseInt(objReader.readLine());
lock.open(key1);
if (lock.isOpen) {
System.out.println("Success, you have unlocked the lock");
return true;
} else if (lock.isFail) {
System.out.println("Alarm, you have failed to unlocked the lock");
return false;
} else {
System.out.println("Incorrect guess, try again");
}
}
}
}
add a comment |
The problem that you have in your code is that you are never set the numAttempts to 0, this means that eventhough you have correctly unlocked the next time you used it you will have numAttempts. In your case I think it fits to reset the counter in Open() when it is success.
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 lock2
lock1.open(111);
lock2.open(222);
lock1.open(111);
lock2.open(222);
}
}
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)
{
System.out.println("Opened");
isOpen = true;
numAttempts = 0;
}
else if(!isOpen)
{
numAttempts++;
}
if(numAttempts == 3)
{
System.out.println("ALARM");//prints alarm when the combo is incorrect 3 times
}
}
}
Did you change anything in the code above?
– Joshua Gentile
Nov 22 '18 at 12:57
yes he did.numAttempts = 0;
in theopen()
method
– Vladimir Ilyich
Nov 22 '18 at 13:17
I changed it and it didn't really change much
– Joshua Gentile
Nov 22 '18 at 14:40
@JoshuaGentile probably I didn't get what the problem is. What does this mean? ` the program just goes on to lock number 2` ? Are you getting the message "ALARM" in the first attempt of lock1? I would recommend you to print the numAttempt variable as well.
– Brank Victoria
Nov 22 '18 at 14:45
regardless of if I get a correct answer for lock1, it immedietly goes to lock 2 and regardless of the answer, it either prints Opened like 10 times and one false. or it prints alarm twice with one false
– Joshua Gentile
Nov 22 '18 at 14:58
|
show 1 more comment
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%2f53431250%2fjava-lock-problem-with-an-issue-regarding-attempts%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
Here is my solution. I did some minor changes to Lock
where I removed any printing and added a member, isFail
, to keep track of when too many guesses has been done.
class Lock {
boolean isOpen;
boolean isFail;
int key;
int numAttempts = 0;
Lock(int key) {
isOpen = false;
isFail = false;
this.key = key;
}
public void open(int key) {
if (isOpen) {
return;
}
if(this.key == key) {
isOpen = true;
} else {
numAttempts++;
isFail = numAttempts == 3;
}
}
}
The major changes is in the GentCPT3
class where each guess for a lock is done in specific method and that method is called once for each lock. Note that I have hardcoded the values to guess since it is not clear from the question where they come from. Maybe using the Random
class here to generate two keys would be an option.
import java.io.*;
public class GentCPT3 {
public static void main (String args) throws IOException {
BufferedReader objReader = new BufferedReader(new InputStreamReader (System.in));
if (guess(objReader, new Lock (3))) {
guess(objReader, new Lock(7));
}
objReader.close();
}
private static boolean guess(BufferedReader objReader, Lock lock) throws IOException {
while (true) {
System.out.println("Guess value");
int key1 = Integer.parseInt(objReader.readLine());
lock.open(key1);
if (lock.isOpen) {
System.out.println("Success, you have unlocked the lock");
return true;
} else if (lock.isFail) {
System.out.println("Alarm, you have failed to unlocked the lock");
return false;
} else {
System.out.println("Incorrect guess, try again");
}
}
}
}
add a comment |
Here is my solution. I did some minor changes to Lock
where I removed any printing and added a member, isFail
, to keep track of when too many guesses has been done.
class Lock {
boolean isOpen;
boolean isFail;
int key;
int numAttempts = 0;
Lock(int key) {
isOpen = false;
isFail = false;
this.key = key;
}
public void open(int key) {
if (isOpen) {
return;
}
if(this.key == key) {
isOpen = true;
} else {
numAttempts++;
isFail = numAttempts == 3;
}
}
}
The major changes is in the GentCPT3
class where each guess for a lock is done in specific method and that method is called once for each lock. Note that I have hardcoded the values to guess since it is not clear from the question where they come from. Maybe using the Random
class here to generate two keys would be an option.
import java.io.*;
public class GentCPT3 {
public static void main (String args) throws IOException {
BufferedReader objReader = new BufferedReader(new InputStreamReader (System.in));
if (guess(objReader, new Lock (3))) {
guess(objReader, new Lock(7));
}
objReader.close();
}
private static boolean guess(BufferedReader objReader, Lock lock) throws IOException {
while (true) {
System.out.println("Guess value");
int key1 = Integer.parseInt(objReader.readLine());
lock.open(key1);
if (lock.isOpen) {
System.out.println("Success, you have unlocked the lock");
return true;
} else if (lock.isFail) {
System.out.println("Alarm, you have failed to unlocked the lock");
return false;
} else {
System.out.println("Incorrect guess, try again");
}
}
}
}
add a comment |
Here is my solution. I did some minor changes to Lock
where I removed any printing and added a member, isFail
, to keep track of when too many guesses has been done.
class Lock {
boolean isOpen;
boolean isFail;
int key;
int numAttempts = 0;
Lock(int key) {
isOpen = false;
isFail = false;
this.key = key;
}
public void open(int key) {
if (isOpen) {
return;
}
if(this.key == key) {
isOpen = true;
} else {
numAttempts++;
isFail = numAttempts == 3;
}
}
}
The major changes is in the GentCPT3
class where each guess for a lock is done in specific method and that method is called once for each lock. Note that I have hardcoded the values to guess since it is not clear from the question where they come from. Maybe using the Random
class here to generate two keys would be an option.
import java.io.*;
public class GentCPT3 {
public static void main (String args) throws IOException {
BufferedReader objReader = new BufferedReader(new InputStreamReader (System.in));
if (guess(objReader, new Lock (3))) {
guess(objReader, new Lock(7));
}
objReader.close();
}
private static boolean guess(BufferedReader objReader, Lock lock) throws IOException {
while (true) {
System.out.println("Guess value");
int key1 = Integer.parseInt(objReader.readLine());
lock.open(key1);
if (lock.isOpen) {
System.out.println("Success, you have unlocked the lock");
return true;
} else if (lock.isFail) {
System.out.println("Alarm, you have failed to unlocked the lock");
return false;
} else {
System.out.println("Incorrect guess, try again");
}
}
}
}
Here is my solution. I did some minor changes to Lock
where I removed any printing and added a member, isFail
, to keep track of when too many guesses has been done.
class Lock {
boolean isOpen;
boolean isFail;
int key;
int numAttempts = 0;
Lock(int key) {
isOpen = false;
isFail = false;
this.key = key;
}
public void open(int key) {
if (isOpen) {
return;
}
if(this.key == key) {
isOpen = true;
} else {
numAttempts++;
isFail = numAttempts == 3;
}
}
}
The major changes is in the GentCPT3
class where each guess for a lock is done in specific method and that method is called once for each lock. Note that I have hardcoded the values to guess since it is not clear from the question where they come from. Maybe using the Random
class here to generate two keys would be an option.
import java.io.*;
public class GentCPT3 {
public static void main (String args) throws IOException {
BufferedReader objReader = new BufferedReader(new InputStreamReader (System.in));
if (guess(objReader, new Lock (3))) {
guess(objReader, new Lock(7));
}
objReader.close();
}
private static boolean guess(BufferedReader objReader, Lock lock) throws IOException {
while (true) {
System.out.println("Guess value");
int key1 = Integer.parseInt(objReader.readLine());
lock.open(key1);
if (lock.isOpen) {
System.out.println("Success, you have unlocked the lock");
return true;
} else if (lock.isFail) {
System.out.println("Alarm, you have failed to unlocked the lock");
return false;
} else {
System.out.println("Incorrect guess, try again");
}
}
}
}
answered Nov 22 '18 at 18:33
Joakim DanielsonJoakim Danielson
9,4773724
9,4773724
add a comment |
add a comment |
The problem that you have in your code is that you are never set the numAttempts to 0, this means that eventhough you have correctly unlocked the next time you used it you will have numAttempts. In your case I think it fits to reset the counter in Open() when it is success.
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 lock2
lock1.open(111);
lock2.open(222);
lock1.open(111);
lock2.open(222);
}
}
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)
{
System.out.println("Opened");
isOpen = true;
numAttempts = 0;
}
else if(!isOpen)
{
numAttempts++;
}
if(numAttempts == 3)
{
System.out.println("ALARM");//prints alarm when the combo is incorrect 3 times
}
}
}
Did you change anything in the code above?
– Joshua Gentile
Nov 22 '18 at 12:57
yes he did.numAttempts = 0;
in theopen()
method
– Vladimir Ilyich
Nov 22 '18 at 13:17
I changed it and it didn't really change much
– Joshua Gentile
Nov 22 '18 at 14:40
@JoshuaGentile probably I didn't get what the problem is. What does this mean? ` the program just goes on to lock number 2` ? Are you getting the message "ALARM" in the first attempt of lock1? I would recommend you to print the numAttempt variable as well.
– Brank Victoria
Nov 22 '18 at 14:45
regardless of if I get a correct answer for lock1, it immedietly goes to lock 2 and regardless of the answer, it either prints Opened like 10 times and one false. or it prints alarm twice with one false
– Joshua Gentile
Nov 22 '18 at 14:58
|
show 1 more comment
The problem that you have in your code is that you are never set the numAttempts to 0, this means that eventhough you have correctly unlocked the next time you used it you will have numAttempts. In your case I think it fits to reset the counter in Open() when it is success.
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 lock2
lock1.open(111);
lock2.open(222);
lock1.open(111);
lock2.open(222);
}
}
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)
{
System.out.println("Opened");
isOpen = true;
numAttempts = 0;
}
else if(!isOpen)
{
numAttempts++;
}
if(numAttempts == 3)
{
System.out.println("ALARM");//prints alarm when the combo is incorrect 3 times
}
}
}
Did you change anything in the code above?
– Joshua Gentile
Nov 22 '18 at 12:57
yes he did.numAttempts = 0;
in theopen()
method
– Vladimir Ilyich
Nov 22 '18 at 13:17
I changed it and it didn't really change much
– Joshua Gentile
Nov 22 '18 at 14:40
@JoshuaGentile probably I didn't get what the problem is. What does this mean? ` the program just goes on to lock number 2` ? Are you getting the message "ALARM" in the first attempt of lock1? I would recommend you to print the numAttempt variable as well.
– Brank Victoria
Nov 22 '18 at 14:45
regardless of if I get a correct answer for lock1, it immedietly goes to lock 2 and regardless of the answer, it either prints Opened like 10 times and one false. or it prints alarm twice with one false
– Joshua Gentile
Nov 22 '18 at 14:58
|
show 1 more comment
The problem that you have in your code is that you are never set the numAttempts to 0, this means that eventhough you have correctly unlocked the next time you used it you will have numAttempts. In your case I think it fits to reset the counter in Open() when it is success.
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 lock2
lock1.open(111);
lock2.open(222);
lock1.open(111);
lock2.open(222);
}
}
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)
{
System.out.println("Opened");
isOpen = true;
numAttempts = 0;
}
else if(!isOpen)
{
numAttempts++;
}
if(numAttempts == 3)
{
System.out.println("ALARM");//prints alarm when the combo is incorrect 3 times
}
}
}
The problem that you have in your code is that you are never set the numAttempts to 0, this means that eventhough you have correctly unlocked the next time you used it you will have numAttempts. In your case I think it fits to reset the counter in Open() when it is success.
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 lock2
lock1.open(111);
lock2.open(222);
lock1.open(111);
lock2.open(222);
}
}
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)
{
System.out.println("Opened");
isOpen = true;
numAttempts = 0;
}
else if(!isOpen)
{
numAttempts++;
}
if(numAttempts == 3)
{
System.out.println("ALARM");//prints alarm when the combo is incorrect 3 times
}
}
}
answered Nov 22 '18 at 12:53


Brank VictoriaBrank Victoria
1,212615
1,212615
Did you change anything in the code above?
– Joshua Gentile
Nov 22 '18 at 12:57
yes he did.numAttempts = 0;
in theopen()
method
– Vladimir Ilyich
Nov 22 '18 at 13:17
I changed it and it didn't really change much
– Joshua Gentile
Nov 22 '18 at 14:40
@JoshuaGentile probably I didn't get what the problem is. What does this mean? ` the program just goes on to lock number 2` ? Are you getting the message "ALARM" in the first attempt of lock1? I would recommend you to print the numAttempt variable as well.
– Brank Victoria
Nov 22 '18 at 14:45
regardless of if I get a correct answer for lock1, it immedietly goes to lock 2 and regardless of the answer, it either prints Opened like 10 times and one false. or it prints alarm twice with one false
– Joshua Gentile
Nov 22 '18 at 14:58
|
show 1 more comment
Did you change anything in the code above?
– Joshua Gentile
Nov 22 '18 at 12:57
yes he did.numAttempts = 0;
in theopen()
method
– Vladimir Ilyich
Nov 22 '18 at 13:17
I changed it and it didn't really change much
– Joshua Gentile
Nov 22 '18 at 14:40
@JoshuaGentile probably I didn't get what the problem is. What does this mean? ` the program just goes on to lock number 2` ? Are you getting the message "ALARM" in the first attempt of lock1? I would recommend you to print the numAttempt variable as well.
– Brank Victoria
Nov 22 '18 at 14:45
regardless of if I get a correct answer for lock1, it immedietly goes to lock 2 and regardless of the answer, it either prints Opened like 10 times and one false. or it prints alarm twice with one false
– Joshua Gentile
Nov 22 '18 at 14:58
Did you change anything in the code above?
– Joshua Gentile
Nov 22 '18 at 12:57
Did you change anything in the code above?
– Joshua Gentile
Nov 22 '18 at 12:57
yes he did.
numAttempts = 0;
in the open()
method– Vladimir Ilyich
Nov 22 '18 at 13:17
yes he did.
numAttempts = 0;
in the open()
method– Vladimir Ilyich
Nov 22 '18 at 13:17
I changed it and it didn't really change much
– Joshua Gentile
Nov 22 '18 at 14:40
I changed it and it didn't really change much
– Joshua Gentile
Nov 22 '18 at 14:40
@JoshuaGentile probably I didn't get what the problem is. What does this mean? ` the program just goes on to lock number 2` ? Are you getting the message "ALARM" in the first attempt of lock1? I would recommend you to print the numAttempt variable as well.
– Brank Victoria
Nov 22 '18 at 14:45
@JoshuaGentile probably I didn't get what the problem is. What does this mean? ` the program just goes on to lock number 2` ? Are you getting the message "ALARM" in the first attempt of lock1? I would recommend you to print the numAttempt variable as well.
– Brank Victoria
Nov 22 '18 at 14:45
regardless of if I get a correct answer for lock1, it immedietly goes to lock 2 and regardless of the answer, it either prints Opened like 10 times and one false. or it prints alarm twice with one false
– Joshua Gentile
Nov 22 '18 at 14:58
regardless of if I get a correct answer for lock1, it immedietly goes to lock 2 and regardless of the answer, it either prints Opened like 10 times and one false. or it prints alarm twice with one false
– Joshua Gentile
Nov 22 '18 at 14:58
|
show 1 more comment
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%2f53431250%2fjava-lock-problem-with-an-issue-regarding-attempts%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
You need to have a loop in your main method so that the user can guess several times but why is the user giving the answer (key) as input?
– Joakim Danielson
Nov 22 '18 at 12:49
The input is meant to check if the combo is right.
– Joshua Gentile
Nov 22 '18 at 12:56
If you see a way to fix it feel free to edit
– Joshua Gentile
Nov 22 '18 at 12:56
You didn't answer my question
– Joakim Danielson
Nov 22 '18 at 13:18
they aren't giving the answer key
– Joshua Gentile
Nov 22 '18 at 14:38