Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Thanks for updating q 1 2 5 7 1 8 7 6 2 based on my feedback, the solution provided is much more suitable. I
Thanks for updating q based on my feedback, the solution provided is much more suitable. Id like some further help for the next part of the question where I need to make a public Safe class to make use of the SafeState class:
A Safe object will have a state and possibly contain some items; however, because it is quite a small safe, it can only contain a limited number of items.
a Next, create a public class called Safe in the same BlueJ project, to simulate the behaviour of a real safe. You will develop this class in the following parts.
Edit the class comment so that it includes your own name and the date and a description of the class Safe. Remove the sample field, constructor, and method.
marks
b In the class Safe add two private fields:
o state, which is of type SafeState, and will be used to control the state of the safe.
o maxItems, an int representing the maximum number of items that can be stored in the safe.
Also add a public field for testing purposes:
o contents, which is an ArrayList of String, used to keep a record of items in the Safe.
Remember that you will require an import statement above your class in order to use an ArrayList.
marks
c Add a public constructor that receives one parameter of type int called aMaxItems. In the constructor body:
o state should be initialised to a new SafeState object.
o contents should be set to an empty collection of the appropriate type.
o maxItems should be set to the number of items the safe can hold, received as a parameter.
marks
In the following parts, add public methods to the Safe class using the given signatures. You will need to choose your own sensible formal parameter names.
d Add the method addToContentsString whose argument is a single item that could be added to the safe contents. If the safe currently contains less than maxItems, then the item is added to the contents, otherwise nothing happens. The method does not return any value.
marks
e Add the method removeFromContentsString whose argument is a single item that could be removed from the safe contents. The method does not return any value.
If the safe currently contains the item in question in its contents, then that item is removed and the message
Removed item
is printed. Otherwise, the message
Item item not in safe
is displayed.
marks
f Add the method display which simply prints out all the items currently stored in the safe contents, in the order they are encountered in the contents, with one item displayed per line of output. The method does not return a value.
marks
g Add the method empty The method does not return a value. This method removes all the items from the safe, one at a time. After each item is removed, the method prints
Removed item
where item is the item that was removed at that step.
marks
h Add the method openString which receives a code to attempt to open the safe. The method makes uses of the field state to attempt to open the safe using the received code. The method returns true if the safe is now open, and otherwise returns false.
marks
I. Add the method lockString which receives a code to attempt to lock the safe. The method makes uses of the field state to attempt to lock the safe using the received code. The method returns true if the safe is now locked, and otherwise returns false.
marks
j Go back and check that you have provided multiline comments for your class, methods and constructor. We do not expect you to use all of the features of comments illustrated in the book at this stage of your study but be sure that your comments explain the purpose of the code and identify which part of the TMA question you are answering in each case.
Also check that you have used a consistent style across the whole class. See the M Code conventions and design document for more advice on coding style.
marks
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started