The list below is supposed to be result of my program. Though, for #2 I get “Homer opens the door”. I dont know if im staring right at the error. I’ve been studying the code for a while and being sleepy definitely doesnt help. Any help is appreciated!
- //Bart locks the door.
- //Homer tries to open the door, but can’t because its locked.
- //Homer unlocks the door.
- //Homer opens the door.
- //Bart tries to open the door, but can’t because its already open.
- //Marge closes the door.
- //Homer tries to close the door, but can’t because it is already
closed. - //Lisa opens the door.
- //Abraham tries to lock the door, but can’t because its open.
- //Marge closes the door.
- //Abraham locks the door.
- //Bart tries to open the door, but can’t because its locked.
- //Lisa tries to open the door, but can’t because its locked.
- //Homer tries to open the door, but can’t because its locked.
- //Abraham unlocks the door.
- //Homer opens the door.
-
//Marge closes the door.
//in a separate method: bart.lockDoor(); homer.openDoor(); homer.unlockDoor(); homer.openDoor(); bart.openDoor(); marge.closeDoor(); homer.closeDoor(); lisa.openDoor(); abraham.unlockDoor(); marge.closeDoor(); abraham.lockDoor(); bart.openDoor(); lisa.openDoor(); homer.openDoor(); abraham.unlockDoor(); homer.openDoor(); marge.closeDoor(); boolean locked; boolean open; public void lockDoor() { if(locked == true) out.println( name + " tries to lock the door, but can't because its already locked."); else if(open == false) { locked = true; out.println( name + " locks the door."); } } public void unlockDoor() { if(open == true) out.println( name + " tries to unlock the door, but can't because its open."); else if(locked == false) out.println( name + " tries to unlock the door, but can't because its already unlocked."); else if(locked == true) { out.println( name + " unlocks the door."); locked = false; } } public void openDoor() { if(open == true) out.println( name + " tries to open the door, but can't because its already open."); else if( locked == true ) out.println( name + " tries to open the door, but can't because its locked."); else if(locked == false) { out.println( name + " opened the door."); open = true; } } public void closeDoor() { if(open == false) out.println( name + " tries to close the door, but can't because its already closed."); else { if(open == true) { out.println( name + " closed the door."); open = false; } } }
You don’t specify language or a complete program, but it looks like the problem is that each person has (effectively) their own door, independent of each other person’s door, as each person has their own locked/open flags to track their door.
If you want there to only be a single door, you need to get all the people to share a single locked and single open flag. Depending on the language you are using, you may be able to do that by making the flags
static. Alternately, you could put them in a separate ‘door’ object and have all the ‘people’ objects refer that ‘door’