:: Enseignements :: ESIPE :: IMAC2 :: IMAC2 2020-2021 :: Object oriented programming in Java ::
[LOGO]

#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...
  1. Create a project called TP4.
  2. Modify TP4 project properties so that the classes (.java files) are generated in the sources directory.
  3. Do the same for all new project (Take a look at Window > Preferences).
  4. Check that the execution environment is Java-15, change if it is not the case.
  5. Check that the compilation environment is Java-15, change if it is not the case.
  6. Write a class called Main that displays Hello Eclipse.
  7. Home work:
    1. What does sysout + Ctrl + Space do in a main method?
    2. What does toStr + Ctrl + Space do in a class ?
    3. Define a foo attribute of type int . What does get + Ctrl + Space do. And set + Ctrl + Space ?
    4. In the Source menu, how to generate a constructor initializing the foo field?
    5. Select the name of the class, and then Alt + Shift + R , what do we get? Same question with the foo field.
    6. Write a = 2 + 3 + 4 , then select 2 + 3 then Alt + Shift + L .
    7. Write new Integer (2) , keeping the cursor after ')', press Ctrl + 1 , what happens?
    8. Declare a variable s of type String and click on String while holding down the Ctrl key. What is going on ?
    9. In the toString () method, what does Ctrl + Click do on super.toString () ?
    10. Select the foo field, then Ctrl + Shift + G . What is going on ?
    11. What is Ctrl + Shift + O used for?
    12. 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.

  1. Is it correct storing the maximum number of books in a static field?
  2. 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?
  3. 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!
  4. 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.

  1. 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?
  2. 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.
  3. 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.
  4. 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?
  5. 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?
  6. 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?
  7. Conclude by indicating in which case we should use the foreach loop and when should you explicitly use an iterator on a collection.