0

I understand what Invariant is, but the problem i have is to identify it from a class. ill explain - when the teacher at class showing us a java code and asks us what is the invariant - here is when im falling off, i dont know to identify it.

again - i know what it is - just have diffuclty identify it. If you can explain or give a tip for me how to do it, ill be happy to hear. thank you.

edited: picture of exmaple added. what is the invariant here, but important for me is WHY.

picture

kaya3
  • 47,440
  • 4
  • 68
  • 97
  • Please edit your question and show some examples. – OldProgrammer Feb 07 '20 at 16:49
  • @OldProgrammer examples of what..? of simple classes code in java? – Lorin Sherry Feb 07 '20 at 17:01
  • Did you find this ? https://stackoverflow.com/questions/8902331/what-is-a-class-invariant-in-java – Hardik Mishra Feb 07 '20 at 17:06
  • An invariant is less something you identify and more something you strive for /decide on and enforce and describe /document if necessary. In your case, the invariant seems to be "the deck never contains invalid cards", ie only values between 1 and 11 because that's what's enforced. – daniu Feb 07 '20 at 17:22
  • @HardikMishra yes Hardik ive read it all.. and still its not clear enough. – Lorin Sherry Feb 07 '20 at 17:30
  • @daniu Thank you!!! i think i am in the way of understanding it completly. So when im being asking what is the invariant, i can ask myself what is illegeal in my class? what is the law of the code to run as it should be? – Lorin Sherry Feb 07 '20 at 17:31
  • @daniu the pre-con of addCard method is that it has to get an int number, and the post-con is that the method will check for me if the int is between 0-10? – Lorin Sherry Feb 07 '20 at 18:11
  • One precondition is "a valid card is offered to the deck". The post condition for this is "The deck will contain it afterwards". Another pre is "An invalid cards is offered", post "the deck will not contain it afterwards". Together those form the invariant. – daniu Feb 07 '20 at 18:32
  • @daniu thanks. can I ask you more questions in private? – Lorin Sherry Feb 08 '20 at 09:18

2 Answers2

0

An invariant holds true for all instances of the class, no matter what. Look in your example to find all those cases where for example, a property is initialized for every instance of the class.

zooes
  • 1,280
  • 3
  • 15
  • 21
  • Pushing a student to think for himself? *g I think he won't like this answer, because he wanted something to present to the teacher. ^^ – Melvin Feb 07 '20 at 17:22
  • @zooes so a constructor is an invariant? – Lorin Sherry Feb 07 '20 at 17:28
  • @LorinSherry - no, constructor is not the invariant, but the property inside the constructor could be. The stackoverflow link above describes it well. – zooes Feb 07 '20 at 17:46
  • @LorinSherry What do you think is happening inside the constructor for CardsTable? Specifically the instance property? – zooes Feb 07 '20 at 20:49
0

Unfortunately, there is no systematic way to look at a piece of code and work out its invariants. One technique is to think of ways that an instance's state might be invalid and then check that those can't happen; in that case, each rule for what makes a state "valid" is an invariant. But you need to use intuition and experience to do this.

For your example, this class has two invariants:

  • The stack only contains integers in the range 1 to 10 inclusive.
  • The element on the bottom of the stack is the integer 5.

It is straightforward to verify that the constructor establishes both invariants, and the addCard method preserves them:

  • The constructor only adds the number 5 which is in range, and addCard only adds numbers to the stack if they are within the range.
  • Numbers are never removed from the stack, and numbers are only added at the top of the stack, so the 5 at the bottom of the stack (which is put there by the constructor) will stay at the bottom of the stack.

It's important to note that the constructor and the addCard method are the only two ways to change the state of the instance or its stack, so we don't have to check whether any other code preserves the invariants. If the stack wasn't private then it would be different; other code would be able to put numbers onto the stack which are out of range (or null), or other code could remove the 5 from the bottom of the stack.

kaya3
  • 47,440
  • 4
  • 68
  • 97