Java exam

elpsy69
javaex.zip

Java exam.docx

Java exam:

Part 2 : Programming questions :

Two programs to be developed: Note that the two programs are independent; program 1 (Q21-25) and program 2 (Q26-33)

Files for testing (Q1Test.java and Q2Test.java) are provided to you and are attached in program 1 and program 2 respectively, along with the interfaces and sketches of the corresponding classes.

Program 1 :

Q 21:

For this question we develop a queue modeling using a simply linked list of objects (of generic type D). So there is an interface, named Queue, which is provided to you, as well as a simple, named, LinkedQueue implementation that you are going to make.

The LinkedQueue class realizes the Queue interface, it must provide an implementation for all the methods of the interface:

LinkedQueue () : constructeur sans paramètre

boolean isEmpty() : renvoie true si la file (LinkedQueue) est vide

void enqueue(D newElement): empiler la file (Insérer un élément (newElement ))

dequeue() : dépiler la file (retirer un élément et le renvoyer)

 

/* Classe LinkedQueue */

public class LinkedQueue<D> implements Queue<D> {

//nested class

    private static class Elem<T> {

        private T value;

        private Elem<T> next;

        private Elem(T value, Elem<T> next) {

            this.value = value;

            this.next = next;

        }

    }

 

   //Instance variables

   private Elem<D> front;

   private Elem<D> rear;

 

//...la suite est dans le programme LinkedQueue.java fourni ci-joint dans programme 1 

}

Question 1: For this LinkedQueue class, implement the parameterless constructor for initialization, in the space below

/* Constructor*/

    public LinkedQueue () ){

Note for the files provided attached in program 1:

THREE FILES ARE ATTACHED IN PROGRAM 1: Queue.java, LinkedQueue.java and Q1Test.java for testing.

We have provided you with an implementation that includes: (1) the declaration of the Elem class (shown above), (2) the signatures of the methods you need to implement in Q21-Q25, (3) the skeleton code (including the Queue interface) for the LinkedQueue class, (4) the full implementation of the toString () method for LinkedQueue, (5) the skeleton code of the UniquifiableLinkedQueue class for Q25, and a Q1Test class that allows you to test your implementation .

Q22 :

For the LinkedQueue class from question 21, implement the boolean isEmpty () method which returns true if the queue is empty, in the space below.

/ * returns true if the queue is empty * /

public  boolean isEmpty(){

Q23 :

For the LinkedQueue class from question 21, implement the void enqueue (D newElement) method to stack the queue.

/* stack the queue */

    public  void enqueue(D newElement){

        if(newElement == null) {

            throw new NullPointerException("no null object in my queue !");

        }

       

        Elem<D> newElem;

        newElem = new Elem<D>(newElement,null);

               Your piece of the code comes here      

    }

Q24:

For the LinkedQueue class from question 21, implement the D dequeue () method to unstack the queue.

/* unstack the queue (remove an item and send it back)*/

    public D dequeue() {

        if(isEmpty()) {

            throw new IllegalStateException("method called on an empty queue");

        }

 

        D returnedValue;

        returnedValue = front.value;

         Your piece of the code comes here    

 

    }

Q25:

For this question, you are going to develop a specialization (subclass) of LinkedQueue. The subclass, named UniquiableLinkedQueue, provides a single additional method, named uniquify ().

The uniquify () method does the following:

It returns a queue that is the same UniquifiableLinkedQueue instance that the method is called for, except that the returned queue does not contain any immediately consecutive duplicate items.

To illustrate what "immediately consecutive duplicate elements" are, consider the following example queue:

More precisely, two queue items are immediately consecutive duplicate items, when they are adjacent and have identical content. Having identical content for the (non-zero) queue elements elem1 and elem2 means that elem1.equals (elem2) is true. You can assume that all items in the queue are not null.

You complete the uniquify () method in the UniquifiableLinkedQueue class below, so that a single instance of immediately consecutive duplicate items would be kept. For example, if uniquify () is called on the example file above (an instance of UniquifiableLinkedQueue <String>), the result should be as shown in the figure below.

Note that uniquify () is not intended to handle non-consecutive duplicate items. In other words, the queue returned by uniquify () may have duplicate items.

You can make calls to the preceding enqueue and dequeue methods.

To test your implementation of uniquify (), you can use the provided Q1Test.java code. The output from executing Q1Test should be the same as that in the test code.

/*Class UniquifiableLinkedQueue*/

public class UniquifiableLinkedQueue <E> extends LinkedQueue<E> {

/*uniquify method*/

public Queue<E> uniquify ( ) {

                       Queue<E> result = new LinkedQueue<E>();

                       Queue<E> temp = new LinkedQueue<E>();

 

                       Your piece of the code comes here

                     

                    return(result);

        }

}

LinkedQueue.java

LinkedQueue.java

public   class   LinkedQueue < D >   implements   Queue < D >   {

     private   static   class   Elem < T >   {
         private  T value ;
         private   Elem < T >  next ;
         private   Elem ( T value ,   Elem < T >  next )   {
             this . value  =  value ;
             this . next  =  next ;
         }
     }

     private   Elem < D >  front ;
     private   Elem < D >  rear ;

     public   LinkedQueue (){
         // Add your code here
     }

     public    boolean  isEmpty (){
        // Add your code here      
     }

     public    void  enqueue ( D newElement ){

         if ( newElement  ==   null )   {
             throw   new   NullPointerException ( "no null object in my queue !" );
         }
        
         Elem < D >  newElem ;
        newElem  =   new   Elem < D > ( newElement , null );
         // Add your code here   
     }


     public  D dequeue ()   {

         if ( isEmpty ())   {
             throw   new   IllegalStateException ( "Dequeue method called on an empty queue" );
         }

        D returnedValue ;
        returnedValue  =  front . value ;

         // Add your code here   
     }

     public   String  toString ()   {
        
         String  result  =   "Front -> [" ;
         if ( ! isEmpty ()){
             Elem < D >  p  =  front ;
            result  +=  p . value ;
             while ( p . next  !=   null ){
                p  =  p . next ;
                result  +=   ", "   +  p . value ;
             }
         }
        result  +=   "] <- Rear" ;
         return  result ;
     }

}

Q1Test.java

Q1Test.java

public   class   Q1Test   {

     public   static   void  main ( String []  args )   {

         UniquifiableLinkedQueue < Integer >  integerQueue  =   new   UniquifiableLinkedQueue < Integer > ();
        integerQueue . enqueue ( 0 );
        integerQueue . enqueue ( 1 );
        integerQueue . enqueue ( 1 );
        integerQueue . enqueue ( 1 );
        integerQueue . enqueue ( 2 );
        integerQueue . enqueue ( 2 );
        integerQueue . enqueue ( 3 );
        integerQueue . enqueue ( 3 );
        integerQueue . enqueue ( 3 );
        integerQueue . enqueue ( 4 );

         System . out . println ( "Original Integer Queue: "   +  integerQueue );
         System . out . println ( "Integer Queue without immediately consecutive duplicates: "   +  integerQueue . uniquify ());
         System . out . println ( "Original Integer Queue (after uniquify): "   +  integerQueue );

         System . out . println ();

         UniquifiableLinkedQueue < String >  stringQueue  =   new   UniquifiableLinkedQueue < String > ();
        stringQueue . enqueue ( "a" );
        stringQueue . enqueue ( "a" );
        stringQueue . enqueue ( "b" );
        stringQueue . enqueue ( "b" );
        stringQueue . enqueue ( "b" );
        stringQueue . enqueue ( "c" );
        stringQueue . enqueue ( "d" );
        stringQueue . enqueue ( "d" );
        stringQueue . enqueue ( "d" );
        stringQueue . enqueue ( "e" );
        stringQueue . enqueue ( "e" );

         System . out . println ( "Original String Queue: "   +  stringQueue );         
         System . out . println ( "String Queue without immediately consecutive duplicates: "   +  stringQueue . uniquify ());
         System . out . println ( "Original String Queue (after uniquify): "   +  stringQueue );
        
         System . out . println ();
         System . out . println ( "---- Now, testing with some non-consecutive duplicates ----- " );
         System . out . println ();

        
         UniquifiableLinkedQueue < String >  stringQueueWithNonConsecutiveDuplicates  =   new   UniquifiableLinkedQueue < String > ();
        stringQueueWithNonConsecutiveDuplicates . enqueue ( "a" );
        stringQueueWithNonConsecutiveDuplicates . enqueue ( "b" );
        stringQueueWithNonConsecutiveDuplicates . enqueue ( "b" );
        stringQueueWithNonConsecutiveDuplicates . enqueue ( "c" );
        stringQueueWithNonConsecutiveDuplicates . enqueue ( "a" );
        stringQueueWithNonConsecutiveDuplicates . enqueue ( "d" );
        stringQueueWithNonConsecutiveDuplicates . enqueue ( "d" );
        stringQueueWithNonConsecutiveDuplicates . enqueue ( "e" );
        stringQueueWithNonConsecutiveDuplicates . enqueue ( "e" );
        stringQueueWithNonConsecutiveDuplicates . enqueue ( "d" );
        stringQueueWithNonConsecutiveDuplicates . enqueue ( "d" );
        stringQueueWithNonConsecutiveDuplicates . enqueue ( "d" );
        stringQueueWithNonConsecutiveDuplicates . enqueue ( "d" );
        stringQueueWithNonConsecutiveDuplicates . enqueue ( "b" );        
        
         System . out . println ( "Original String Queue: "   +  stringQueueWithNonConsecutiveDuplicates );         
         System . out . println ( "String Queue without immediately consecutive duplicates: "   +
          stringQueueWithNonConsecutiveDuplicates . uniquify ());
         System . out . println ( "Original String Queue (after uniquify): "   +  stringQueueWithNonConsecutiveDuplicates );

        
     }
}



/**************************SORTIE QUE VOUS DEVEZ AVOIR :**********************

Original Integer Queue: Front -> [0, 1, 1, 1, 2, 2, 3, 3, 3, 4] <- Rear
Integer Queue without immediately consecutive duplicates: Front -> [0, 1, 2, 3, 4] <- Rear
Original Integer Queue (after uniquify): Front -> [0, 1, 1, 1, 2, 2, 3, 3, 3, 4] <- Rear

Original String Queue: Front -> [a, a, b, b, b, c, d, d, d, e, e] <- Rear
String Queue without immediately consecutive duplicates: Front -> [a, b, c, d, e] <- Rear
Original String Queue (after uniquify): Front -> [a, a, b, b, b, c, d, d, d, e, e] <- Rear

---- Now, testing with some non-consecutive duplicates ----- 

Original String Queue: Front -> [a, b, b, c, a, d, d, e, e, d, d, d, d, b] <- Rear
String Queue without immediately consecutive duplicates: Front -> [a, b, c, a, d, e, d, b] <- Rear
Original String Queue (after uniquify): Front -> [a, b, b, c, a, d, d, e, e, d, d, d, d, b] <- Rear

*************************************************************/

Queue2.java

Queue2.java

public   interface   Queue < E >   {
     boolean  isEmpty ();
     void  enqueue ( E newElement );
    E dequeue ();
}

UniquifiableLinkedQueue.java

UniquifiableLinkedQueue.java

public   class   UniquifiableLinkedQueue < E >   extends   LinkedQueue < E >   {

     public   Queue < E >  uniquify ()   {
        
         // Add your code here

         throw   new   UnsupportedOperationException (   "not implemented yet!"   );
     }

}