document.write('
Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. /**
  2.  *
  3. 1.  Write a class named as ArrayListNum  with the appropriate fields, constructor and the following methods:-
  4.  
  5. isEmpty();
  6.  
  7. isFull();
  8.  
  9. insertAtFront();
  10. -   Check the array whether it is empty or full
  11. -   If it is empty, proceed the insertion process
  12. -   If it is full, prompt a message  or perform error handling
  13. -   If it is not full and not empty, can we proceed with the insertion
  14. process?
  15. -   No! You have to shift the elements first from the left to the right of the
  16. array.
  17.  
  18. insertAtBack();
  19. -   Is it empty or full?
  20. -   If it is empty, proceed the insertion process.
  21. -   If it is full, prompt a message or perform error handling
  22. -   If it is not full and not empty? Can we proceed with the insertion
  23. process? Yes! But you have to know the last element by using a
  24. counter.
  25.  
  26. removeFromFront();
  27. -   Is it empty?
  28. -   If it is empty, prompt a message or perform error handling.
  29. -   If it is not empty, proceed the deletion process but you have to shift
  30.             the element to the left of the array.
  31.  
  32. removeFromBack();
  33. -   Is it empty?
  34. -   If it is empty, prompt a message or perform error handling.
  35. -   If it is not empty, proceed the deletion process.
  36. -   Decrease the size by 1.
  37.  
  38. display();
  39.  
  40.  
  41. Notes:      
  42. •   You are required to implement an array of objects.
  43. Use Integer class.
  44.  
  45.  *
  46.  * @author MUHAMMAD AZRI BIN JASNI @ ABDUL RANI
  47.  * @version 27 SEPTEMBER 2012
  48.  */
  49. public class ArrayListNum
  50. {
  51.     // instance variables
  52.     public int length;
  53.     protected final int maxSize = 100;// maxSize default at 100
  54.     public Integer [] list;
  55.    
  56.     /**
  57.      * Constructor for objects of class ArrayListNum
  58.      */
  59.     public ArrayListNum(){
  60.          this.length = 0;
  61.          list = new Integer[maxSize];
  62.     }
  63.     public ArrayListNum(int length)
  64.     {
  65.         this.length = length;
  66.         list = new Integer[maxSize];
  67.     }
  68.    
  69.     /**methods**/
  70.     public boolean isEmpty()
  71.     {
  72.         return (length==0);
  73.     }
  74.     public boolean isFull()
  75.     {
  76.         return (length==maxSize);
  77.     }
  78.    
  79.     public void insertAtFront(int a)
  80.     {
  81.         if (isFull())
  82.         {
  83.             System.err.println("Can\'t insert in full list.");
  84.         }
  85.         else
  86.         {
  87.             if (! isEmpty())
  88.             {
  89.                 for(int i = length; i > 0; i--)
  90.                     list[i] = list[i - 1];
  91.             }
  92.             //if (isEmpty())
  93.             //    list = new Integer[1];
  94.             list [0] = a;
  95.             length++;
  96.         }
  97.     }//end insertAtFront
  98.     public void insertAtBack(int a)
  99.     {
  100.     if (isFull())
  101.         {
  102.             System.err.println("Can\'t insert in full list.");
  103.         }
  104.         else
  105.         {
  106.             list [length] = a;
  107.             length++;
  108.         }
  109.      }//end insertAtBack
  110.      public void removeFromFront()
  111.      {
  112.         if (isEmpty())
  113.         {
  114.             System.err.println("Can\'t remove in empty list.");
  115.         }
  116.         else
  117.         {
  118.             for(int i = 0; i <length; i++)
  119.                 list[i] = list[i+1];
  120.             length--;
  121.         }
  122.     }
  123.     public void removeFromBack()
  124.     {
  125.     if (isEmpty())
  126.         {
  127.             System.err.println("Can\'t remove in empty list.");
  128.         }
  129.         else
  130.         {
  131.             list[length] = null;
  132.             length--;
  133.         }
  134.     }
  135.     public void display()
  136.     {
  137.         for(int i = 0; i <length; i++)
  138.         {
  139.             System.out.print(list[i]+" ");
  140.         }
  141.         System.out.println("");
  142.     }
  143.  
  144. }
');