- Notifications
You must be signed in to change notification settings - Fork0
manuandru/OOP-lab05
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
Ask the teacher for a discussion about the solutionupon the completion of each single exercise, unless explicitly differently said in the exercise text.
Observeit.unibo.oop.lab05.ex1.UseCollection
, and use it as example to complete the exercise init.unibo.oop.lab05.ex1.UseSet
.Before proceeding, please read and make sure you understand the following notes.
Natural Ordering: Elements in aTreeSet
are sorted bynatural order, i.e., the typeX
used withinTreeSet<X>
must implement the interfaceComparable<? super X>
. As such, for instance:
A
TreeSet<String>
allows new elements to be added by using theadd
method becauseString
implementsComparable<String>
;conversely, trying to add elements of type
MyType
to aTreeSet<MyType>
, in the hypothesis thatMyType
does not implementComparable<MyType>
, would raise a run-time error of typeClassCastException
(details on exceptions and error handling will be provided in the next lessons).
Concurrent modifications: All the iterators created from instances of theTreeSet<E>
class (actually, iterators created by any of the collections provided with the JDK standard library) arefail-fast.
If the collection instance on which the iterator is operating is modified while an iteration is in progress, the iterator will produce a run-time error (aConcurrentModificationException
, to be precise).The reason is that the iterator may end up in an inconsistent state: it is iterating over a collection, but the collection has changed! What is the next element? And, in case of an unordered collection (such as anHashSet
), how to make sure we are not visiting the same element again?Since those error may generate inconsistencies which lead to bugs very hard to spot and reproduce, Java takes the conservative stance of halting when such a behavior is detected.Note that more "permissive" languages (such as Javascript) don't enforce the same policy, and leave to the implementor the burden of dealing with inconsistent iteration states.
- try to write a
for
-each cycle iterating overTreeSet
(which, internally, generates and uses anIterator
), and within the cycle try to remove an nelement from theTreeSet
. What happens?
The correct way to remove elements from a collections while iterating it is to use theremove()
method ofIterator
.This requires a reference to the iterator, and, as such, cannot be used from within afor
-each cycle.
Follow the comments in
it.unibo.oop.lab05.ex2.UseSetWithOrder
, and create a program that sorts aTreeSet<String>
using a customComparator<String>
(to be created separately, from scratch).Refer to thejava documentation to understand how to create a
Comparator
(interface documentation).
Implement
WharehouseImpl.java
andProductImpl.java
by following the contracts described in their relative interfaces.Refer to the code documentation for details about the implementation
Run
UseWarehouse
to test the program
This exercise is an extension of the previous: reuse as much as possible of the previously produced (working) code.
In order to run tests, complete the
main
method ofUseWarehouse.java
initializing correctly the variables of typeProduct
andWarehouse
.
Implement the functions in the class
Utilities.java
.Verify their behavior by using
UseUtilities
.