UML - to do after Laboratory 2

Exercise 1

Use the UML tools installed in the laboratory machines:
  • Visual Paradigm,
  • Dia,
  • ArgoUML and
  • Microsoft Visio.
Model therein a class diagram with the primitives you learned.
Choose one of these UML tools to install in your computers and use in the project.

One class exercises - to do after Laboratory 3

Exercise 1

Note: Java offers the programmer a LinkedList (generic) class. The main purpose of this exercise is to understand the similarities and differences to languages like C when defining data structures.

Develop a package with the usual operations over lists of integer. This list should be in the form of a linked list. In this context, consider the following constructors:

  • a no-arg constructor, which builds an empty list;
  • a constructor which receives an integer and builds a list with this integer.

Besides constructors, provide four public methods:

  • insert: inserts an integer in the list – the linkage should maintain the insertion order, for instance, if we insert 1 followed by the insertion of 2 and followed by the insertion of 3 a linked list must be built which has as its first node 1, last node 3, and middle node 2.
  • remove: removes all occurrences of an integer and returns the number of integers removed.
  • length: returns the length of the list.
  • toString: overrides the toString method from Object to provide a textual description to the liked list as: "{integer_1,...,integer_n}".

a) Model in UML the class diagram for the described data type of linked list of integers (do not consider modeling the abstract data type as you have not learned yet how to define interfaces, nor generic types, in java).

b) Implement in Java the previous package (do not use any collection from Java). Test the package:

  • Add successively the following integers to the list: 1, 2 and 3. Verify that the list you obtain is the one expected: {1,2,3}.
  • Build two different lists with the same content. Compare them with the == operator and the equals method from Object. Justify the result.
  • Override the equals method from Object to return true iff two linked lists of integers have the same content. Which other method from Object should be also overridden? Justify. Override it coherently with equals. Repeat the comparisons and justify the differences.
  • Override the finalize method from Object to print to the terminal the textual description of the respective object. What does this method do? Who calls it and when is it called?
  • Create a linked list of integers and then make its reference to null. Call the garbage collector (just for illustration purposes, don't do this in your code as garbage collector runs periodically). What happens? Justify.