Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Question 2 (a) [8 marks] Recall that a class invariant (sometimes called an implementation invariant) is used to define which instance variable values are legal.

image text in transcribed

image text in transcribedimage text in transcribed

Question 2 (a) [8 marks] Recall that a class invariant (sometimes called an implementation invariant) is used to define which instance variable values are legal. Such a class invariant must be .established by the constructors of the class (i.e. when a constructor is invoked from a state satisfying its precondition, then it must be terminate in a state satisfying the class invariant). preserved by each of the methods of the class when they are called from states satisfying their preconditions (i.e. when these methods are invoked from a state that satisfies their precondition and the class invariant, then they will terminate in a state satisfying the class invariant.) . Provide a class invariant for the IntegerSet class that is strong enough to show that both methods size and get are correct with respect to their specifications in the IntegerSet class (i.e. when these methods are invoked from a state that satisfies their precondition and the class invariant, then they will terminate in a state satisfying their post-condition.) The class invariant can be expressed in a combination of English and Java notation. Question 2 (b) [4 marks] Provide a correct implementation of the following method remove for the IntegerSet class. The implementation of the whole class IntegerSet should remain correct after the addition of your remove-method implementation. (Your method should preserve the class invariant from the answer to part (a) of this question.) Removes an integer from the set *@ensure if x is contained in the set, then it is removed from the set, otherwise the set is unchanged by this operation. public void remove (Integer x) Question 2 (a) [8 marks] Recall that a class invariant (sometimes called an implementation invariant) is used to define which instance variable values are legal. Such a class invariant must be .established by the constructors of the class (i.e. when a constructor is invoked from a state satisfying its precondition, then it must be terminate in a state satisfying the class invariant). preserved by each of the methods of the class when they are called from states satisfying their preconditions (i.e. when these methods are invoked from a state that satisfies their precondition and the class invariant, then they will terminate in a state satisfying the class invariant.) . Provide a class invariant for the IntegerSet class that is strong enough to show that both methods size and get are correct with respect to their specifications in the IntegerSet class (i.e. when these methods are invoked from a state that satisfies their precondition and the class invariant, then they will terminate in a state satisfying their post-condition.) The class invariant can be expressed in a combination of English and Java notation. Question 2 (b) [4 marks] Provide a correct implementation of the following method remove for the IntegerSet class. The implementation of the whole class IntegerSet should remain correct after the addition of your remove-method implementation. (Your method should preserve the class invariant from the answer to part (a) of this question.) Removes an integer from the set *@ensure if x is contained in the set, then it is removed from the set, otherwise the set is unchanged by this operation. public void remove (Integer x)

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Introductory Relational Database Design For Business With Microsoft Access

Authors: Jonathan Eckstein, Bonnie R. Schultz

1st Edition

1119329418, 978-1119329411

More Books

Students also viewed these Databases questions