Problem 3: Interfaces

1. Abstract functions

An abstract function is a mathematical entity that takes an input, performs some calculation and produces an output. Such a definition is called a lambda. In OOP, we can model abstract functions as an interface, called ILambda, that defines a method apply that takes an Object as input and returns an Object as output.
  1. Define the ILambda interface.
  2. Define two concrete lambda functions, called Lambda1 and Lambda2, for which the apply method should return: 
    - a String that concatenates “Lambda1 applied to ” with the textual description of the Object received as parameter, in the case of Lambda1;
    - a String that concatenates “Lambda2 applied to ” with the textual description of the Object received as parameter, in the case of Lambda2.
  3. Consider another interface, called ILogical, which represents objects with the ability to apply one of two possible lambdas. In OOP the ILogical interface defines a method select with three parameters: two ILambda’s and an Object. The select method calls either the 1st or the 2nd apply method with the given Object, and returns the Object returned in the call to the apply method. Define the ILogical interface.
  4. Define two concrete classes, called True and False, which implements ILogical and satisfy the following specification. When 
    - b.select(new Lambda1(), new Lambda2(), “hello”); 
    is executed with a variable b of type ILogical, the return value will be 
    - “Lambda1 applied to hello”, when b is of type True, and 
    - “Lambda2 applied to hello”, when b is of type False.
    Your code for True and False may not contain any String literals.
  5. Exemplify the call of select over a True object in a main method within a Main class.

2. The command pattern

Pretende-se oferecer um controlo remoto programável (CRP) com cinco botões. Dos cinco botões, apenas quatro são programáveis. O quinto botão serve para fazer uma operação global de undo. O objectivo é programar os primeiros quatro botões do CRP com operações de ligar e desligar luz (2 botões) e de abrir e fechar porta da garagem (2 botões). O quinto botão do CRP deverá reverter a última operação realizada. Assuma que a posição do quinto botão do CRP botão é diferenciada das restantes dada que o mesma não é programável.
  1. Considere uma interface IComando que oferece 2 métodos, executar e reverter. Os métodos não recebem qualquer parâmetro e devolvem void. Defina a interface IComando.
  2. Considere que existe uma classe Luz com dois métodos, ligar e desligar, que não recebem qualquer parâmetro e devolvem void; Luz tem ainda um construtor sem argumentos. Defina duas concretizações de IComando, chamadas LigarLuz e DesligarLuz.
  3. Considere que existe uma classe PortaGaragem com dois métodos, abrir e fechar, que não recebem qualquer parâmetro e devolvem void; PortaGaragem tem ainda um construtor sem argumentos. Defina duas concretizações de IComando, chamadas AbrirPortaGaragem e FecharPortaGaragem.     
  4. Defina a classe ControloRemoto que implementa o CRP com os 5 botões. Esta deve oferecer 3 métodos: 
    atribuir, que recebe um inteiro que identifica a posição do botão e o respectivo comando, e que deve gravar o respectivo comando na posição pretendida; 
    carregar, que recebe a posição do botão, e que deve executar a acção associada a esse botão; 
    reverter, que deve realizar a operação global de undo.
  5. Exemplifique a utilização de um objecto de tipo ControloRemoto num método main dentro duma classe Main, associando o ligar e desligar a luz e o abrir e fechar a porta da garagem aos botões disponíveis no CRP. Para esse efeito deve conseguir realização a seguinte sequência de acções: 
    - abrir a porta da garagem; 
    - fechar a porta da garagem; 
    - ligar a luz; 
    - fazer undo (para desligar a luz).    

3. Unlimited priority queue

Recall Laboratory 2. Implement UnlimitedPriorityQueue in order to provide a queue of persons (e.g., as in a supermarket).

Attachments