Java exam

elpsy69
javaprogram2.zip

Iterator2.java

Iterator2.java

public   interface   Iterator < T >   {
     boolean  hasNext ();

    T next ();
}

java exam Programm 2.docx

Programm 2 :

Q26:

For this question, you are going to write a program that transforms a given two-dimensional array (Matrix) of objects (generic type E) into a linked grid-like structure. You will then implement some operations as well as an iterator on the resulting linked structure. Below, we illustrate the transformation for a two-dimensional array of integer objects (ie Integer [] []).

Each node in the grid is an instance of the Node (nested) class, shown below. Each Node instance points to two other Node instances: the node immediately to its right (right) and the node immediately below (down). Grid lines end with a right null pointer; columns end with a null pointer down, as shown above. The grid has a perfectly rectangular shape; there are no missing nodes in the grid structure.

Complete the implementation of the LinkedGrid class by writing the methods requested in the questions that follow.

The first part of the class is below. Otherwise the complete class is in the attached file in prohramme2: LinkedGrig.java.

/*Class LinkedGrid */

import java.util.NoSuchElementException;

public class LinkedGrid<E> {

/* Nested Node class */

        public static class Node<T> {

                       private T data;

                       private Node<T> right, down;

                       Node(T data, Node<T> right, Node<T> down) {

                                       this.data = data;

                                       this.right = right;

                                       this.down = down;

                       }

 

                       public T getData() { return data;}

                       public Node<T> getRight(){ return right; }

                       public Node<T> getDown(){ return down;}

        }

 

/*Instance variables*/

        private Node<E> topLeft; //first element of the grill

        private int rowCount, columnCount; // number of rows and number of columns

 

/* The rest is in the attached file in program2*/

 

}

Question 1) Complete the int getRowCount () method which returns the number of rows of the grid in the space below

/*Instance methods*/

/* Returns the number of rows */

        public int getRowCount() {

                       Votre ligne du code vient ici      

        }

Note for files provided attached in program 2:

You are provided with three files attached in program2: LinkedGrid.java, the Iterator.java interface, and the Q2Test.java test code to test your programs.

We have provided you with a Template implementation that includes: (1) the declaration of the Node class (shown above), (2) the signatures of the methods you need to implement in Q26-Q33, (3) the skeleton code (y including the Iterator interface) for the LinkedGridIterator class in Q33, (4) the full implementation of the constructor for a single row grid (LinkedGrid (E [] array)), (5) the full implementation of the toString method () for LinkedGrid, and (6) a Q1Test class that allows you to test your implementation.

Q27:

For the LinkedGrid class from question 26, Implement the method in the space below,

int getColumnCount(): returns the number of columns in the grid

 

/* Returns the number of columns in the grid */

        public int getColumnCount () {

                       Your line of code comes here

        }

Q28:

For the LinkedGrid class from question 26, Implement the method,

boolean isEmpty(): renvoie true si la grille est vide

     /* Returns true if the grid is empty */

        public boolean isEmpty() {

                       Your line of code comes here      

        }

Q29:

For the LinkedGrid class from question 26, Implement the method,

Node<E> getTopLeft(): returns the first element of the grid

 

/* Returns the first element of the grid */

        public Node<E> getTopLeft(){

                       Your line of code comes here

        }

Q30:

For the LinkedGrid class from question 26, Implement the method,

E getElementAt (int row, int column): This method returns the object stored in row i and column j.

For example, calling getElementAt (1, 2) on our illustrative integer grid will return an Integer instance with the value 5.

/* Returns the object stored at row i and column j */        

public E getElementAt(int row, int column) {

                       if (row < 0 || row >=  rowCount || column < 0 || column >= columnCount)

                                       throw new IllegalArgumentException("The both parameters have to be within range");

 

                       Node<E> current = topLeft;

 

                       Your piece of the code comes here      

                       return current.data;

        }

Q31:

For the LinkedGrid class from question 26, Implement the method,

void addFirstRow(E[] array);

/* create and add the first row of the grid */

private void addFirstRow(E[] array) {

                       if (!isEmpty())

                                       throw new IllegalStateException("Grid must be empty to add a first row");

 

                        topLeft = new Node<E>(array[0], null, null);

                       Node<E> current = topLeft;

                       Your piece of the code comes here               

}

When this method is called (by the LinkedGrid constructor), the first row of the grid is created and the topLeft instance variable in LinkedGrid is made to point to the first node of that row. The expected behavior of the method is illustrated in the figure below. Additionally, the method should set rowCount instance variables and columnCount in LinkedGrid (index: rowCount should be set to 1 and columnCount should be set to array.length).

Q32:

For the LinkedGrid class of question 26, complete the method

void addRow(E[] array) ; Add a line at the bottom of the grid

When called, this method adds a new row at the bottom of an existing grid. This behavior is illustrated in the figure below. Note that the addRow (E [] array) method can be called iteratively, with each call adding a row to the bottom of the grid.

To create the entire grid in our illustration, addRow must be called twice, once with [3; 4; 5] as parameter then with [6; 7; 8] as a parameter. Note that the first row (ie [0; 1; 2]) must have been added beforehand by calling addFirstRow (array E []) expanded in Q31. Calling addRow (array E []) should increase the columnCount instance variable by one.

/* Add a line at the bottom of the grid */

public void addRow(E[] array) {

                       if (array == null)

                                 throw new NullPointerException("array cannot be null");

                        if (rowCount == 0)

                                throw new IllegalStateException("Need to add first row first");

                        if (array.length != this.columnCount)

                               throw new IllegalArgumentException("array must contain " + this.columnCount + " elements");

 

                       Node<E> prev = topLeft;

                       while(prev.down != null)

                                       prev = prev.down;

                       Node<E> current = new Node<E>(array[0], null, null);

                       prev.down = current;

                       Your piece of the code comes here           

}

Q33/

Class LinkedGridIterator:

Complete the LinkedGridIterator () parameterless constructor of the LinkedGridIterator class for initialization (of currentIterator, headRow).

//constructor 

      public LinkedGridIterator() {

                            Your line of code comes here     

        }

LinkedGrid.java

LinkedGrid.java

import  java . util . NoSuchElementException ;

public   class   LinkedGrid < E >   {
     public   static   class   Node < T >   {
         private  T data ;
         private   Node < T >  right ,  down ;

         Node ( T data ,   Node < T >  right ,   Node < T >  down )   {
             this . data  =  data ;
             this . right  =  right ;
             this . down  =  down ;
         }

         public  T getData ()   {   return  data ;}
         public   Node < T >  getRight (){   return  right ;   }
         public   Node < T >  getDown (){   return  down ;}
     }

     private   Node < E >  topLeft ;
     private   int  rowCount ,  columnCount ;

     public   LinkedGrid ( E []  array )   {
         if   ( array  ==   null )
             throw   new   NullPointerException ( "array cannot be null" );
         if   ( array . length  ==   0 )
             throw   new   IllegalArgumentException ( "array must contain elements" );

            addFirstRow ( array );
  
     }

     private   void  addFirstRow ( E []  array )   {

         if   ( ! isEmpty ())
             throw   new   IllegalStateException ( "Grid must be empty to add a first row" );

         // Add your code here

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

     public   void  addRow ( E []  array )   {
         if   ( array  ==   null )
             throw   new   NullPointerException ( "array cannot be null" );
         if   ( rowCount  ==   0 )
             throw   new   IllegalStateException ( "Need to add first row first" );
         if   ( array . length  !=   this . columnCount )
             throw   new   IllegalArgumentException ( "array must contain contain "   +   this . columnCount  +   " elements" );

         // Add your code here

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


     }

     public   LinkedGrid ( E [][]  array )   {

              if   ( array  ==   null )
                  throw   new   NullPointerException ( "array cannot be null" );

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


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

     public   Node < E >  getTopLeft (){
         // Add your code here
     }

     public  E getElementAt ( int  row ,   int  column )   {
         if   ( row  <   0   ||  row  >=   rowCount  ||  column  <   0   ||  column  >=  columnCount )
             throw   new   IllegalArgumentException ( "The row and column parameters both have to be within range" );

         // Add your code here

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

     }

     public   String  toString ()   {
         StringBuffer  buffer  =   new   StringBuffer ();

         for   ( int  i  =   0 ;  i  <  rowCount ;  i ++ )   {
             for   ( int  j  =   0 ;  j  <  columnCount ;  j ++ )   {
                buffer . append ( getElementAt ( i ,  j ));
                 if   ( <  columnCount  -   1 )
                    buffer . append ( ", " );
             }
             if   ( <  rowCount  -   1 )
                buffer . append ( System . lineSeparator ());
         }

         return  buffer . toString ();

     }

     private   class   LinkedGridIterator   implements   Iterator < E >   {

         private   Node < E >  currentIterator ,  headRow ;
 
         public   LinkedGridIterator ()   {
             // Add your code here
         }

         public  E next ()   {
           throw   new   UnsupportedOperationException (   "not implemented yet!"   );
         }

         public   boolean  hasNext (){
             throw   new   UnsupportedOperationException (   "not implemented yet!"   );
   }

  }

  public   Iterator < E >  iterator ()   {
   return   new   LinkedGridIterator ();
  }
}

Q2Test.java

Q2Test.java

public   class   Q2Test   {

  public   static   void  test (){

   Integer []  integerFirstRow  =   {   0 ,   1 ,   2   };
   Integer []  integerSecondRow  =   {   3 ,   4 ,   5   };
   Integer []  integerThirdRow  =   {   6 ,   7 ,   8   };

   LinkedGrid < Integer >  integerGrid  =   new   LinkedGrid < Integer > ( integerFirstRow );
  integerGrid . addRow ( integerSecondRow );
  integerGrid . addRow ( integerThirdRow );


   System . out . println ( "==== Integer grid with "   +  integerGrid . getRowCount ()   +  
    " rows x "   +  integerGrid . getColumnCount ()   +   " columns ====" );
   System . out . println ( "[Test LinkedGrid(E[] array) / addFirstRow(...) and addRow(...)]" );
   System . out . println ( "getTopLeft().getData(): "   +  integerGrid . getTopLeft (). getData ());
   System . out . println ( "getTopLeft().getRight().getRight().getDown().getData(): "   +  integerGrid . getTopLeft (). getRight (). getRight (). getDown (). getData ());
   System . out . println ( "getTopLeft().getDown().getDown().getData(): "   +  integerGrid . getTopLeft (). getDown (). getDown (). getData ());
   System . out . println ( "getTopLeft().getDown().getRight().getDown().getRight().getData(): "   +  integerGrid . getTopLeft (). getDown (). getRight (). getDown (). getRight (). getData ());
   System . out . println ();

   System . out . println ( "[Test getElementAt(...)] Print integer grid via toString() which uses getElementAt(...)" );
   System . out . println ( integerGrid );
   System . out . println ();

   System . out . println ( "[Test LinkedGridIterator] Grid via an iterator" );
   Iterator < Integer >  iIterator  =  integerGrid . iterator ();

   StringBuffer  iBuffer  =   new   StringBuffer ();
  
   System . out . println ();
}
  public   static   void  main ( String []  args )   {
  test ();
 
  }
}




/**************************SORTIE QUE VOUS DEVEZ AVOIR :**********************
==== Integer grid with 3 rows x 3 columns ====
[Test LinkedGrid(E[] array) / addFirstRow(...) and addRow(...)]
getTopLeft().getData(): 0
getTopLeft().getRight().getRight().getDown().getData(): 5
getTopLeft().getDown().getDown().getData(): 6
getTopLeft().getDown().getRight().getDown().getRight().getData(): 8

[Test getElementAt(...)] Print integer grid via toString() which uses getElementAt(...)
0, 1, 2
3, 4, 5
6, 7, 8

[Test LinkedGridIterator] Grid via an iterator

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