Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Here is what you need to do. 1. Create a package in eclipse with the name cse360

ID: 3823855 • Letter: H

Question

Here is what you need to do. 1. Create a package in eclipse with the name cse360hw7. Copy the OrderedIntListjava file into this package. 2. Generate the initial set of JUnit test cases. All should have the default of fail. 3. Complete the code for your test cases. Create new methods as needed for the tests. Your tests should not have any if statements, loops or output. Use asserts to check the results. 4. Thoroughly test the code. Consider the following tests for insert. This is a minimum of 6 different tests. Each tests should be in its own method. a. insert into an empty list b. insert at beginning, at end and in middle c. insert duplicates d. insert causing array expansion 5. Make sure that all the code meets all coding and documentation standards. The junit test cases should include internal documentation while the OrderedIntListjava file contain both internal and external documentation. 6. Submit the following file for grading OrderedIntList Test java.

Explanation / Answer

Run the program by enableing assertion flag: java -ea OrderedIntList

OrderedIntList.java


public class OrderedIntList {
      
        private int[] list;
        private int count;
      
        /**
         * Default constructor for a size of 10
         */
                      
        OrderedIntList ()
        {
                list = new int[10];
                count = 0;
        }
      
        /**
         * Constructor to create an array of size specified
         *
         * @param newsize specified size of new array
         */
      
        OrderedIntList (int newsize)
        {
                list = new int[newsize];
                count = 0;
        }

        /**
         * size
         *
         * return the current size of the array, not the number of elements
         *
         * @return length
         */
        public int size () {
                return list.length;
        }
      
        // search for an item using the binary search algorithm
      
        private int search (int target, int low, int high) {
              
                if (low > high)
                        return -1;
                else {
                        int mid = (low + high) / 2;
                        if (list[mid] == target)
                                return mid;
                        else
                                if (list[mid] < target)
                                        return search (target, mid + 1, high);
                                else
                                        return search (target, low, mid - 1);
                        }
        }
      
        /**
         * insert
         *
         * If the array is full, then increase the size by 50%. Then insert the item
         * into the array at the correct ordered position. Do not allow duplicates.
         *
         * @param val
         */
        public void insert (int val) {
                if (count == list.length) {
                        int newsize = count + count / 2;
                        copyArray (newsize);
                }
                int pos = 0;
                while (pos < count && list[pos] < val)
                        pos++;
                if (pos == count || list[pos] != val) {
                        for (int index = count; index > pos; index--)
                                list[index] = list[index - 1];
                        list[pos] = val;
                        count++;
                }
        }
      
        // create a new array with the smaller size
      
        private void copyArray (int newsize) {
                int [] temp = new int [newsize];
              
                for (int index = 0; index < count; index++)
                        temp[index] = list[index];
              
                list = temp;
        }
      
        /**
         * delete
         *
         * Search for and remove the item from the array by moving all items
         * up in the array. If the list is less than half full, copy the memory
         * to a smaller array.
         *
         * If the item is not in array, do not make any changes.
         *
         * @param val integer value to remove from the array
         */
      
        public void delete (int val) {
                int pos = search (val, 0, count - 1);
              
                if (pos != -1) {
                        for (int index = pos + 1; index < count; index++)
                                list[index - 1] = list[index];
                        count--;
                      
                        int min = list.length / 2;
                        if (count < min) {
                                int newsize = list.length * 4 / 10;
                                copyArray (newsize);
                        }
                }
        }
      
        /** toString
         * return the contents of the array separated by a space
         */
      
        public String toString () {
              
                String str = "";
                if (count > 0) {
                        str = "" + list[0];
                        for (int index = 1; index < count; index++)
                                str += " " + list[index];
                }
                return str;
        }
      
        //Insert into the empty list
        public void InsertIntoEmptyList(int val)
        {
            list[0]=val;
            count++;
        }      
      
        //Insert at the begining of the list
        public void InsertAtBegin(int val)
        {
            int newsize = count +1;
            int [] temp = new int [newsize];
            temp[0]=val; //Insertion at the begining
            for (int index = 0; index < count; index++) //copy the remaining elements
                        temp[index+1] = list[index];
            list=temp;
            count=newsize;
        }
      
        //Insert at the end of the list
        public void InsertAtEnd(int val)
        {
           int newsize = count +1;
           int [] temp = new int [newsize];
           for (int index = 0; index < count; index++) //Copy all the elements first
                        temp[index] = list[index];
           temp[count]=val; //insertion at end
           count=newsize;
           list=temp;
        }      
      
        //Insert at the middle of the list
        public void InsertAtMiddle(int val)
        {
            int newsize = count +1;
            int index=count/2; //works for even and odd no of elements
            int [] temp = new int [newsize];        
            for(int i=0;i<index;i++) //copy all the elements till middle index
                temp[i]=list[i];
             temp[index]=val; //insert the at middle
             for(int i=index+1;i<newsize;i++) //copy remaing elements
                temp[i]=list[i-1];
             list=temp;
             count=newsize; //update the new size of the list
        }
     
        //Display the list
        public void displayList()
        {
          System.out.print("The list is : ");
          for(int i=0;i<count;i++)
           System.out.print(list[i]+" ");
        }
        public int check(int index,int val)
        {
            if(list[index]==val)
                return 1;
            else
                return 0;
        }      
        public static void main(String[] args)
        {
            OrderedIntList l=new OrderedIntList();
            l.InsertIntoEmptyList(5);
     
            assert (l.check(0,5)==1):"Element is not inserted into empty location";
          
            l.InsertAtBegin(22);
            assert (l.check(0,22)==1):"Element is not inserted at the begining location";
          
            l.InsertAtBegin(7);
            assert (l.check(0,7)==1):"Element is not inserted at the begining location";
          
            l.InsertAtEnd(33);
            assert (l.check(3,33)==1):"Element is not inserted at the end location";
          
            l.InsertAtMiddle(55);
            assert (l.check(3,55)==1):"Element is not inserted at the middle location";
          
            l.displayList();
        }      
}