0% found this document useful (0 votes)
6 views4 pages

Lp2A - Td2 Array, Tree, Collections, List : Exercise 1

The document outlines exercises for developing systems to manage students, company staff, and media collections using various data structures in Java. It includes tasks such as creating classes for students and employees, implementing methods for searching and sorting, and managing collections with ArrayLists, TreeSets, and HashSets. Additionally, it emphasizes the use of UML class diagrams and Java code implementation for each exercise.

Uploaded by

badisbenmlouka
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views4 pages

Lp2A - Td2 Array, Tree, Collections, List : Exercise 1

The document outlines exercises for developing systems to manage students, company staff, and media collections using various data structures in Java. It includes tasks such as creating classes for students and employees, implementing methods for searching and sorting, and managing collections with ArrayLists, TreeSets, and HashSets. Additionally, it emphasizes the use of UML class diagrams and Java code implementation for each exercise.

Uploaded by

badisbenmlouka
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

LP2A – TD2

Array, Tree, Collections, List …

Exercise 1
We wish to develop a system for managing groups of students. To do this, we will
create a class that will handle an array that will contain information about students:
- Last name,
- First name,
- Age,
- …

In addition to traditional methods (getter, setter, constructors, etc.), this class


should have many other methods:
§ Find a student from its name,
§ Calculate the average age of students in a group,
§ Return the students who have a given age or null if no student has asked age.
Questions:
§ Propose a class organization to do this (UML Class Diagram).
§ Write the Java code for these classes.
§ Modify your program to use a Java data structure (an ArrayList) instead of the
array.
§ We now want to use another data structure to accelerate the search of a student
based on his last name. What type of data structure could be used?

Exercise 2
We want to develop a system for the management of the staff of a company. This
system must be able to manage the hierarchy between people. Thus, for a given
person, the system must be able to return a list of his subordinates and the name of its
leader.

To do that, we will construct an n-ary tree that will manage the information of people
(name, salary, etc.)

§ Propose a class organization to do this (UML Class Diagram).


§ Write the Java code for these classes. Your system must have the
following methods:
o A method that returns the list of subordinates,
o A method that returns the direct chief,
o A method that make a depth traversal of the tree and print people’s
information. To facilitate reading this, you shift to the right
information from people based on their hierarchical level.

LP2A – TD2 -1-


Exercise 3
We want to maintain a list of employee of a company with a TreeSet. This data
structure will allow us to sort the information in relation to a particular attribute. In
our example we will sort employees by their salary. To do that, we will have to
pass to the constructor of our TreeSet an object of type Comparator (see
abstract of JavaDoc following).

• Create a class Employee with attributes:


o Name
o Salary

• Create a class which implements Comparator interface


o Override compare method

• Create a class with a main method


o Create the TreeSet object
o Create many Employee
o Add these employee to the TreeSet
o Use TreeSet methods to perform the following operations:
§ Get a list of all employees sorted by increasing salary
§ Get the number of employees who earn more than a given salary §
Get a list of employees whose salary is between two given values

Extracted from JavaDoc :

public class TreeSet<E>

Type Parameters:
E - the type of elements maintained by this set
All Implemented Interfaces:
Serializable, Cloneable, Iterable<E>, Collection<E>, NavigableSet<E>, Set<E>,
SortedSet<E>

The elements are ordered using their natural ordering, or by a Comparator provided
at set creation time, depending on which constructor is used.

TreeSet(Comparator<? super E> comparator)


Constructs a new, empty tree set, sorted according to the specified comparator.
boolean add(E e)
Adds the specified element to this set if it is not already present.

E ceiling(E e)
Returns the least element in this set greater than or equal to the given element,
or null if there is no such element.

E floor(E e)
Returns the greatest element in this set less than or equal to the given element,
or null if there is no such element.

E higher(E e)
Returns the least element in this set strictly greater than the given element, or
null if there is no such element.

E lower(E e)
Returns the greatest element in this set strictly less than the given element, or
null if there is no such element.

LP2A – TD2 -2-


SortedSet<E> subSet(E fromElement, boolean fromInclusive, E toElement,
boolean toInclusive)
Returns a view of the portion of this set whose elements range from fromElement to
toElement.

SortedSet<E> subSet(E fromElement,E toElement)


Returns a view of the portion of this set whose elements range from fromElement,
inclusive, to toElement, exclusive.

SortedSet<E> tailSet(E fromElement)


Returns a view of the portion of this set whose elements are greater than or equal
to fromElement.

public interface Comparator<T>


A comparison function, which imposes a t o t a l o r d e r i n g on some collection of
objects. Comparators can be passed to a sort method (such as Collections.sort or
Arrays.sort) to allow precise control over the sort order. Comparators can also be
used to control the order of certain data structures (such as sorted sets or sorted
maps), or to provide an ordering for collections of objects that don't have a
natural ordering.

int compare(T o1,


T o2)
Compares its two arguments for order. Returns a negative integer, zero, or a
positive integer as the first argument is less than, equal to, or greater than the
second.

Exercise 4
We want to develop an application for classifying the DVD, CD, book, etc. To do this,
we will use a collection of type Set: HashSet.

Our system should be able to accept new books, CDs or DVDs and a function, call
periodically by the user, will allow to clean the structure. This function will ask, for
each book, CD or DVD, if it’s still exists. If this is not the case, the item will be removed
from the structure.

§ In a UML Class Diagram:


o Propose a class representing books, CDs and DVDs. For this class we will
use an enum to manage the type of the media (BOOK, CD or DVD).

o Create a class that handles the media of all types. This class must have the
following methods:
§ A method for adding a new media.
§ A method that displays on the screen a list of all the media content
from the structure according to their type.
§ A method that ask the user for each media, whether or not there
are still. If not the item is deleted from the HashSet.
§ Write the Java program.

Our application is now able to manage the loan of books, CDs or DVDs. To do that, we
will create a class to manage person who can borrow media. Our application will be able
to return a list of borrowed media for a given person, but also return for a media, if it is
taken or not and if so, by whom.

LP2A – TD2 -3-


§ Complete your class diagram and your program with a class representing
borrower.
§ Add loan management to the application. Which data structure can be used?

Finally we want to display the list of media, sorted by name.

§ Which data structure can be used to do this?


§ Modify your program to add this new functionality.

Preliminary information:
Scanner class can parse primitives types and string using regular expressions. For our
exercise, we’ll use it to allow us to retrieve easily the keystrokes of the users.

This example shows how to use the Scanner class to read the information entered by the
user:
Scanner sc = new Scanner(System.in);
int i = sc.nextInt();

In this example, we read an integer by the method nextInt(). But this class allows us to
read others types of data with methods:
nextFloat() : float
nextDouble() : double
nextLong() : long
nextLine() : String

public class HashSet<E>

Type Parameters:
E - the type of elements maintained by this set
All Implemented Interfaces:
Serializable, Cloneable, Iterable<E>, Collection<E>, Set<E>

HashSet()
Constructs a new, empty set; the backing HashMap instance has default
initial capacity (16) and load factor (0.75).

boolean add(E e)
Adds the specified element to this set if it is not already present.

LP2A – TD2 -4-

You might also like