• Post Reply Bookmark Topic Watch Topic
  • New Topic

Change of state isn't recognized properly in Door object  RSS feed

 
Sada Abe
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,
I'm having to code a door object and I have got it to compile and run. The door should open if and only if the door is closed and unlocked. It will close only if it's open, and lock only if it's unlocked. It is initialized as being unlocked and open using boolean values. However, it is not registered when a door is already opened, closed, or locked, after calling a method. It always refers to the initialized values. I am not allowed to add additional methods or values. What should I do?
Sorry for the length, but the code looks simple enough. In the TestDoor,
It will output that it is already unlocked and open because that is what it's initialized as. It will close, but if it's closed again it will output the same message that, "The door is now closed." The same happens for locking it twice, instead of saying its already in that state. Opening the door after its closed and locked will say that's already open, referring to the initial value. The code was lengthier before, but I tried to make it as simple as possible. Any insight would be appreciated, thanks.
 
Campbell Ritchie
Marshal
Posts: 56518
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

You have some dangerous repetitions. Why do you have a locked field and isLocked? You need to delete one of them. You are also re‑declaring the fields in the constructor. You mustn't declare things which look like the fields in the constructor; you should simply initialise them. If you use this.inscription = in the constructor, call the parameter inscription too, so it will be obvious to users what it means.
Please reformat the code so there is one blank line between successive methods/constructors etc.
Please reformat methods like isLocked because you don't need those ifs.
 
Campbell Ritchie
Marshal
Posts: 56518
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please remove all those imports; you are not using any of them. You should not use any imported classes in this class, but you might need them elsewhere.
 
Sada Abe
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I had those locked/isLocked fields because I forgot how I was supposed to go about this kind of thing, but once you said 'declare' and 'initialize' it totally clicked. My professor provided the variable names like "String c," but I changed that since I agree with you. The imports are something my professor copy and pastes every time he opens a new file, so I copied him. The new code works perfectly: But I still have some questions...
Is the only purpose of isClosed and isLocked to return closed and locked, so that when their values are changed in other methods, it stays that way? The prompt says that isClosed() should return true if the Door is closed; this is why I thought I should have the if-else statement to return true when closed is true. Did I just misinterpret the wording?

Also, the use of 'this.' isn't really necessary here, right? Because variables of the same name won't be used elsewhere? Thank you for the help!
 
Campbell Ritchie
Marshal
Posts: 56518
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In that constructor, yes, you must use this. in line 9.
The fields locked and closed already are either true or false, so those isXXX methods simply return true or false as appropriate.
In the lock() method you don't need else if. You simply need if‑else. Probably similarly for unlock().
Unless you have been told otherwise the getInscription method should return the inscription and nothing else.

That is a great improvement on the previous code; well done
 
Campbell Ritchie
Marshal
Posts: 56518
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you really want
else if (!closed && !locked)
in line 28? If the door is already open, does it matter whether it is locked or not? You said you can close the door when it is locked, so you can presumably lock a door which is open without closing it. I think you may be able to change the final else if in the open() and close() method to plain simple else if you go through all the possible combinations.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!