:: Enseignements :: ESIPE :: IMAC2 :: IMAC2 2020-2021 :: Object oriented programming in Java ::
![[LOGO]](http://igm.univ-mlv.fr/ens/resources/mlv.png) |
#9.en Package, Data structure, implementation relation
|
Exercice 1 - Linked Lists
The aim of this exercice is to implement a linked list.
For the rest of the exercise, all of the classes created
should be in the fr.umlv.data package.
Source files (.java) must be in the directory
src and destination files (.class) must be
in the classes directory.
If needed, configure your Eclipse using
Window>Preferences>Java>Build Path and
project>Properties>Java>Build Path.
We will first create a linked list of integers.
-
Create a class Link in the package fr.umlv.data
corresponding to a link in the linked list storing integers.
Under no circumstances should the user of the class manipulate links himself.
What should be the visibility of the class fr.umlv.data.Link as well
that the visibility of its fields?
Write a test main in this class creating
two links containing the values 13 and 144.
-
Create a class fr.umlv.data.LinkedLink which will allow
to handle a list chained by its first link.
-
add(int value) that adds an element at the beginning of the list.
-
toString() that create a String representing the list.
To test the class fr.umlv.data.LinkedLink ,
create a class Main in the package fr.umlv.data.main.
Exercice 2 - Linked list (the sequel)
-
Implement int get (int index) which returns the
index -th value of the chained list.
What should you do if the index is invalid?
Check that your establishment respects the adage blow early, blow often ,
otherwise change the location!
-
In order to re-use the list in different codes,
change the classes fr.umlv.data.LinkedLink and fr.umlv.data.Link
for a more generic implementation based on Object .
-
In the Main class, create a list containing character strings (at least 2)
and write a test displaying the length of the second string in the list.
-
Explain why we have to add a cast in the main method
and why as a Java developer, we don't like casts.
Exercice 3 - LinkedLink generification
The purpose of this exercise is to "genericize" the classes
fr.umlv.data.LinkedLink and fr.umlv.data.Link
-
Explain what is the point of using a type configured here?
-
Configure the class fr.umlv.data.LinkedLink so that it
be generic.
-
Modify the class fr.umlv.data.main.Main accordingly.
-
In the fr.umlv.data.LinkedLink class, implement the boolean contains (Object o) method indicating whether or not an object is contained in the linked list.
Why does contains take an Object as a parameter and not a T or an E ?
© Université de Marne-la-Vallée