Introduction

In this programming assignment you will apply about stacks to create a simple versioning system demo. Get the initial code below.

Description

A Keyedobject models objects with a key (a string) and some content (also a string). Key objects implement the HasKey interface. Therefore, the KeyObject class must override the HasKey's getKey method (which returns the keyed object's key).

A KeyedStack is a dynamic stack with a key (always of type string). All of the objects in a stack must implement the HasKey interface (like keyed objects) and they must share the same key. Therefore, KeyedStack's push method must check if the object to be pushed has the same key of all of all other objects already in the stack. Upon the first push, the stack must save the pushed object's key to help future key checking.

A keyed stack can be used to maintain multiple versions of an object. For example, to create a new version of an object one needs to get the object's current version (using the stack's peek method), clone that object's version, modify its content accordingly, and then push the new version onto the stack. A stack makes sense for this type of system because in a versioning application we are always interested in the latest version of the object, which is the last one to be pushed onto the stack.

Versioning System Demo

A StackOfVersionedObjects uses a dynamic stack to keep a collection of VersionedObjects that share the same key. The version numbers of the objects in the same stack increase as they move up the stack. Therefore, the latest version of an object is always the one that is at the top of the stack. When the stack becomes empty, no more objects are allowed to enter the stack. The figure below illustrates how a stack can be used for versioning control.

Figure: see image.

Imagine that we want to control different versions of an object that represents a shopping list, uniquely identified by the "shopping list" key. The objects must have a key in order to be versioned and the key must be the same. The stack is created with the first object which has the key "grocery list" and "bananas" as its content. Then later we decided to create a new version of the object by adding "strawberry" to the grocery list. First we peek the stack to get the latest version of the object. Then we clone that object (to start with a fresh copy). After modifying the clone's content (by adding strawberry to it), we push this new version onto the stack.

The stack "learns" the key of the objects pushed onto it after the first push. Then it uses the "learned" key to check future push attempts of new versions of the object. Remember, the key must be the same for all of the objects in the keyed stack. If we try to push a keyed object with a different key (e.g., "books to read") the stack must refuse the request by throwing an "Invalid key" error.

In this assignment you are asked to finish the TO-DOS that are embedded in the shared code for classes KeyedObject, KeyedStack, and VersioningSystem. Below is the desired output for VersioningSystem (use it to check if your code is correct).

(top) KeyedObject(key-'grocery list', content='banana'] (bottom)
(top) KeyedObject(key-'grocery list', content='banana, straweberrX"}
KeyedObject(key='grocery list', content='banana'} (bottom)
java.lang.Error: Invalid key!
KeyedObject(key='grocery list', content='banana, straweberry'
KeyedObject(key='grocery list', content='banana'}
(top) KeyedObject(key-'books to read', content='Mobi Dick'] (bottom)
Academic Honesty!
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly complete the form. Please provide a valid email address and we will get back to you within 24 hours. Payment is through PayPal, Buy me a Coffee or Cryptocurrency. We are a nonprofit organization however we need funds to keep this organization operating and to be able to complete our research and development projects.