:: Enseignements :: ESIPE :: IMAC2 :: IMAC2 2020-2021 :: Object oriented programming in Java ::
![[LOGO]](http://igm.univ-mlv.fr/ens/resources/mlv.png) |
#4.en Objects, delegation, simple structures, exceptions
|
Exercice 1 - Eclipse
From now on, we will use Eclipse (launch the command
eclipse in a terminal) as the environment for doing the labs.
Since it seems to be already the case, you can quickly pass on this exercice and go to the second one...
- Create a project called TP4.
- Modify TP4 project properties so that the classes (.java files) are generated in the sources directory.
- Do the same for all new project (Take a look at Window > Preferences).
- Check that the execution environment is Java-15, change if it is not the case.
- Check that the compilation environment is Java-15, change if it is not the case.
- Write a class called Main that displays Hello Eclipse.
- Home work:
- What does sysout + Ctrl + Space do in a main method?
- What does toStr + Ctrl + Space do in a class ?
- Define a foo attribute of type int . What does get + Ctrl + Space do. And set + Ctrl + Space ?
- In the Source menu, how to generate a constructor initializing the foo field?
- Select the name of the class, and then Alt + Shift + R , what do we get? Same question with the foo field.
- Write a = 2 + 3 + 4 , then select 2 + 3 then Alt + Shift + L .
- Write new Integer (2) , keeping the cursor after ')', press Ctrl + 1 , what happens?
- Declare a variable s of type String and click on String while holding down the Ctrl key. What is going on ?
- In the toString () method, what does Ctrl + Click do on super.toString () ?
- Select the foo field, then Ctrl + Shift + G . What is going on ?
- What is Ctrl + Shift + O used for?
- What is Ctrl + Shift + C used for?
Learn the shortcuts that we have just seen, this will allow you to save time during the noted labs.
Exercice 2 - Caddie in amazonia
The purpose of this exercise is to define
a class to represent
a shopping cart containing a set of books
(we will reuse the class Book of the previous TP).
We want that each ArrayShoppingCart could be able to define a maximum number of books to store in this shopping cart.
We are going to write a class ArrayShoppingCart using an array
to store books, its constructor as well as an add method which allows you to add
a book in the shopping cart.
For the whole exercise, you will write a main method in the ArrayShoppingCartTest class to test your code.
-
Is it correct storing the maximum number of books in a static field?
-
Write the constructor and the add () method
knowing that you are asked to store the books in
an array.
At first, do not try to deal with the case where
there are more books than the maximum expected.
Write, in the main method, a specific test to illustrate this case.
What is going on ?
How should the add method be modified?
-
Write a method to display the content of the shopping cart
with the number of books in the header, then one book per line.
Be careful that your code does not allocate too many objects!
-
Write a longestTitle method that returns the book
of the cart with the longest title.
We will return null if there is no longer title.
Exercice 3 - Free Caddy
The previous implementation forced the programmer to choose
in advance a maximum number of books to be stored in a shopping cart.
We wish to remove this limitation using the class
java.util.ArrayList rather than an basic array to store
the books in the shopping cart.
-
Create a class FreeShoppingCart , its constructor
as well as the add method.
How to remove the warning that the compiler is reporting
on the ArrayList.add method?
-
We now want to write the longestTitle method.
To do this, use an index that goes through the list and the method
ArrayList.get to get the books.
-
Write a new version of the longestTitle method
and using the ArrayList.iterator method, and then the methods
Iterator.hasNext () and Iterator.next on the iterator
returned.
-
Write a third version of the longestTitle method in
using a foreach loop (the Java for (:) ).
How does the compiler compile a foreach loop on a collection?
Use the command javap if you don't know!
javap -c MyMain.class
Write in the main method of a Test class
a foreach loop on an array
(for example, that of main arguments).
Compare the results with those of the previous question.
What can you conclude from this?
-
Write the method removeFirstOccurence which removes the first occurrence of a book in the shopping cart.
What is the complexity of this method, in the worst case?
-
Note that there is a remove method in the java.util.Iterator class,
how to use it to write removeFirstOccurence ?
What is the advantage compared to the previous code?
Write the corresponding code.
And now, what is the complexity in the worst case?
-
Conclude by indicating in which case we should use the foreach loop and
when should you explicitly use an iterator on a collection.
© Université de Marne-la-Vallée