Design and implement a queue

degree of difficulty: 1

A queue is an abstract data type for adding and removing elements. The first element added to a queue ist the first element that is removed (first-in-first-out, FIFO). Queues can be used, for instance, to manage processes of an operating system: the first process added to the waiting queue is reactivated prior to all other processes (with the same priority).

Design an interface Queue, with methods to add and remove elements (integers). Furthermore, a methode to check whether the queue is empty or not should exist.

Implement the queue with an array. If the array becomes to small to hold all added elements, create a new larger (double the size of instance) array and copy all elements of the small array to the new one.

Thoroughly test your implementation with small and large queues.


Compare chemical elements with the abstract data type Comparable and Comparator

Before you start this exercises, please design and implement chemical elements first.

degree of difficulty: 2

In the Java Collection Framework two abstract data types exist to check whether one object is larger, equal, or less to another object: Comparable und Comparator. Comparable is implemented by the class of the objects to compare. In that case only one ordering exist for these objects. Sometimes it is desirable to implement different orderings for the same class. In this case you can implement for each ordering the interface Comparator in a seperated class.

Comparable defines the method int compareTo(Object o) which returns 0 if the object is equal to o, a negative value if o is less, and a positive value if it is larger the objekt on which this method has been invoked. Follow the link and carefully read the specification of Comparable. The class String for instance implements Comparable.

Comparator defines the method int compareTo(Object o1, Object o2). It returns 0 if o1 is equals to o2, a negative value if o1 is less o2, and positive if o1 is larger o2.

One specific ordering for chemical elements is used to write structural formula of chemical compounds (H2O, NaCl): the elements are ordered according their electronegativity (in ascending order). Implement this ordering as a Comparable and as a Comparator.

Test you implementation with Arrays.sort() to sort an array of chemical elements.

Another ordering is the Hill system:

For instance: ClNa instead NaCl.

Is it possbile to implement this ordering as a Comparable or Comparator to, for instance, sort an array of chemical elements in their Hill order? If so, given an implementation. Make sure to ingore the letter case (see String API).

Solution Comparator and Comparable