// Program Name:                 SortedIntList.java
// Course:                       CSE 1302J
// Student Name:                 Bradley Shedd
// Assignment Number:            Lab#4
// Due Date:                     09/22/2010
// ****************************************************************
// SortedIntList.java
//
// An (sorted) integer list class with a method to add an
// integer to the list and a toString method that returns the contents
// of the list with indices with a header.
//         
// ****************************************************************                      
    public class SortedIntList extends IntList{
  
      
public SortedIntList(int size){
       
//call IntList constructor
         super(size);
      }
   
   
   
   
//override add
       public void add(int toAdd)
      {
        
int i;   // define outside of loop - will be used in loop and last line
         for (i=0;i<numElements;i++) // look for position where value must be added
            if (toAdd < list[i])
              
break;
        
for (int j=numElements; j>i; j--) // move array elements up, from top down
            list[j] = list[j-1];    // to that position, i
         list[i] = toAdd;
         numElements++;   
// add new element to position i
      }
     
      
public String toString()
      {
        
return "Sorted List\n"+super.toString();
      }
   
  
   }

Homepage