Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
This repository was archived by the owner on May 9, 2024. It is now read-only.
/OOP-lab05Public archive
NotificationsYou must be signed in to change notification settings

manuandru/OOP-lab05

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 
 
 

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.

Using sets

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.

Important 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:

  • ATreeSet<String> allows new elements to be added by using theadd method becauseString implementsComparable<String>;

  • conversely, trying to add elements of typeMyType to aTreeSet<MyType>, in the hypothesis thatMyTypedoes 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 afor-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.

Custom comparators

  • Follow the comments init.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 aComparator(interface documentation).

Warehouse

  • ImplementWharehouseImpl.java andProductImpl.java by following the contracts described in their relative interfaces.

  • Refer to the code documentation for details about the implementation

  • RunUseWarehouse to test the program

Warehouse, reuse

  • 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 themain method ofUseWarehouse.java initializing correctly the variables of typeProduct andWarehouse.

Utilities

  • Implement the functions in the classUtilities.java.

  • Verify their behavior by usingUseUtilities.

About

No description or website provided.

Topics

Resources

Stars

Watchers

Forks

Languages


[8]ページ先頭

©2009-2025 Movatter.jp