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

#3.en Object, reference, equality, nullity, mutability


Exercice 1 - Book

We want to create a class Book representing a book with a title and the name of the author.
  1. Create a Book class containing the private attributes title and author.
  2. Then try this code in the main method of the class Book.
    	    var book = new Book();
    	    System.out.println(book.title + ' ' + book.author);
          
    Describe the result.
  3. Create a Main class (in a file named Main.java) and move the main of the Book class in the Main class.
    What is the issue? How can you solve it?
  4. Describe the 4 distinct accessibilities available in Java.
    Why should you always choose private for fields?
  5. What is an accessor?
    Since there is no reason to change the content of the fields of a book once it is created (at least for now), what are the accessors that we must use here?
  6. How to tell another developer that the value of the title (or author ) attribute shouldn't change?
    Why is it important?
  7. Add an constructor that initialize (a constructor initializes, it doesn't really build) the book with a title ( ptitle ) and an author ( pauthor ).
    Why does the main code no longer work? Change it.
  8. Modify the previous constructor so that the two parameters are called title and author . What's the problem ? What is the solution ?
  9. Write another constructor who just takes a title and no author.
    You will initialize the author attribute with the string "<no author>".
  10. How does the compiler know which constructor to call?
  11. How do you get the second constructor to call the first?

Exercice 2 - Liberty, Equality, Fraternity

  1.        var b1 = new Book("Da Java Code", "Duke Brown");
           var b2 = b1;
           var b3 = new Book("Da Java Code", "Duke Brown");
    
           System.out.println(b1 == b2);
           System.out.println(b1 == b3);
          
    What does the above code display?
    Why?
  2. Write a method in the class Book (it's up to you to find the exact name and signature of the method) which returns true if two books have the same name and same description.
    Be careful when comparing strings.
  3. The class java.util.ArrayList corresponds to a table that expands dynamically -- as needed.
    What is the indexOf method of ArrayList for? (RTFM)
  4. Run the following code:
         public static void main(String[] args){
           var b1 = new Book("Da Java Code", "Duke Brown");
           var b2 = b1;
           var b3 = new Book("Da Java Code", "Duke Brown");
    
           var list = new ArrayList();
           list.add(b1);
           System.out.println(list.indexOf(b2));
           System.out.println(list.indexOf(b3));
         }
         
    What is wrong with the results displayed on the console?
    Note: here, the compiler generates an warning at the add level. We will see in the next tutorials how to avoid it.
  5. Which method of Book is called by ArrayList.indexOf ? (RTFM again)
  6. Modify the class Book so that indexOf () on the ArrayList tests whether the two books have the same characteristics.
  7. Use the @Override ( java.lang.Override ) annotation on the method added to Book .
  8. What is the @Override annotation for?
  9. What does the code below show?
           var aBook = new Book(null, null);
           var anotherBook = new Book(null, null);
           var list = new ArrayList();
           list.add(aBook);
           System.out.println(list.indexOf(anotherBook));
          
    Where is the issue?
    Remember why a code should stop if it is misused by a developer.
    What should be done to correct the problem?
  10. Describe what is the good practice concerning the use of null .
  11. What is the java.util.Objects.requireNonNull method for? (RTFM)
    How do I use it here to prevent building a book with null attributes?

Exercice 3 - How to display a Book?

We would like to be able to display the characteristics of a book, using the following Java code:
      var book = new Book("Da Java Code", "Dan Duke");
      System.out.println(book);
    
Java can do this, as long as you put in the class Book a public String toString () method (the definition of this method is in the class java.lang.Object (RTFM!)) returning a character string, which we typically construct from the attributes of the object.
Reminder: in Java you can do a '+' between a String and no matter what, the result is the concatenation between the String and anything else seen as a character string.

  1. Write this method, to obtain for example the following display:
          Da Java Code by Dan Duke 
          
  2. Can we use the @Override annotation here?
  3. More difficult, we now want to display only the title of the book if the book is built with the constructor with one argument. Be careful: the display of new Book ("","<no author>") should display "<no author>"!
    If you see a solution that uses null , think harder :)

Exercice 4 - A sort of Sort [Homework]

  1. Write a swap method that swaps the values of two elements of an array. void swap (int [] array, int index1, int index2)
  2. Write an indexOfMin method which returns the index of the minimum value of an array.
  3. Modify the indexOfMin method by adding two indices indicating that we are looking for the index of the minimum not on the whole table, but on the part of the table between these two indices (the first included, the second excluded).
  4. Write the sort method which takes an integer array as a parameter and which sorts it using the indexOfMin and swap methods.