473,395 Members | 1,437 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

need assistance

28
I need to call the recursive binarySearch method from my OrderedArrayList class into my main program

Expand|Select|Wrap|Line Numbers
  1. public abstract class ArrayListClass
  2. {
  3.     protected int length;           //to store the length of the list
  4.     protected int maxSize;          //to store the maximum size of the list
  5.     protected DataElement[] list;   //array to hold the list elements
  6.  
  7.  
  8.         //Default constructor
  9.         //Creates an array of size 100
  10.         //Postcondition: list points to the array, length = 0,
  11.         //               and maxSize = 100
  12.     public ArrayListClass()
  13.     {
  14.         maxSize = 100;
  15.  
  16.         length = 0;
  17.         list = new DataElement[maxSize];
  18.     }
  19.  
  20.         //Constructor with parameter
  21.         //Creates an array of size specified by the parameter
  22.         //size.
  23.         //Postcondition: list points to the array, length = 0,
  24.         //               and maxSize = size
  25.     public ArrayListClass(int size)
  26.     {
  27.         if(size <= 0)
  28.         {
  29.             System.err.println("The array size must be positive. "
  30.                              + "Creating an array of size 100. ");
  31.              maxSize = 100;
  32.         }
  33.         else
  34.             maxSize = size;
  35.  
  36.         length = 0;
  37.         list = new DataElement[maxSize];
  38.     }
  39.  
  40.          //copy constructor
  41.     public ArrayListClass(ArrayListClass otherList)
  42.     {
  43.         maxSize = otherList.maxSize;
  44.         length = otherList.length;
  45.         list = new DataElement[maxSize];    //create the array
  46.  
  47.         for(int j = 0; j < length; j++)  //copy otherList
  48.             list[j] = otherList.list[j].getCopy();
  49.     }//end copy constructor
  50.  
  51.  
  52.         //Method to determine whether the list is empty.
  53.         //Postcondition: Returns true if the list is empty;
  54.         //               otherwise, returns false.
  55.     public boolean isEmpty()
  56.     {
  57.         return (length == 0);
  58.     }
  59.  
  60.         //Method to determine whether the list is full.
  61.         //Postcondition: Returns true if the list is full;
  62.         //               otherwise, returns false.
  63.     public boolean isFull()
  64.     {
  65.         return (length == maxSize);
  66.     }
  67.  
  68.         //Method to return the number of elements in the list.
  69.         //Postcondition: Returns the value of length.
  70.     public int listSize()
  71.     {
  72.         return length;
  73.     }
  74.  
  75.         //Method to return the maximum size of the list.
  76.         //Postcondition: Returns the value of maxSize.
  77.     public int maxListSize()
  78.     {
  79.         return maxSize;
  80.     }
  81.  
  82.         //Method to output the elements of the list.
  83.         //Postcondition: Elements of the list are output on the
  84.         //standard output device.
  85.     public void print()
  86.     {
  87.         for(int i = 0; i < length; i++)
  88.             System.out.print(list[i] + " ");
  89.         System.out.println();
  90.     }
  91.  
  92.         //Method to determine whether item is the same as the item in
  93.         //the list at the position specified by location.
  94.         //Postcondition: Returns true if list[location] is
  95.         //               same as location; otherwise, returns false.
  96.     public boolean isItemAtEqual(int location, DataElement item)
  97.     {
  98.           return(list[location].equals(item));
  99.     }
  100.  
  101.         //Method to insert insertItem in the list at the position
  102.         //specified by location.
  103.         //Postcondition: Starting at location, the elements of the list
  104.         //               are shifted to make room for the new item,
  105.         //               list[location] = insertItem;, and
  106.         //               length++;
  107.         //     If the list is full or location is out of range,
  108.         //     an appropriate message is displayed.
  109.     public void insertAt(int location, DataElement insertItem)
  110.     {
  111.         if(location < 0 || location >= maxSize)
  112.             System.err.println("The position of the item to be inserted "
  113.                               + "is out of range");
  114.         else
  115.             if(length >= maxSize)  //list is full
  116.                 System.err.println("Cannot insert in a full list.");
  117.             else
  118.             {
  119.                 for(int i = length; i > location; i--)
  120.                     list[i] = list[i - 1];  //move the elements down
  121.  
  122.                 list[location] = insertItem.getCopy();  //insert the
  123.                                         //item at the specified position
  124.                 length++;   //increment the length
  125.             }
  126.     } //end insertAt
  127.  
  128.         //Method to inserts insertItem at the end of the list.
  129.         //Postcondition: list[length] = insertItem; and length++;
  130.         //           If the list is full, an appropriate
  131.         //           message is displayed.
  132.     public void insertEnd(DataElement insertItem)
  133.     {
  134.         if(length >= maxSize)  //the list is full
  135.             System.err.println("Cannot insert in a full list.");
  136.         else
  137.         {
  138.               list[length] = insertItem.getCopy();  //insert the
  139.                                                   //item at the end
  140.               length++; //increment the length
  141.         }
  142.     } //end insertEnd
  143.  
  144.         //Method to remove the item from the list at the position
  145.         //specified by location.
  146.         //Postcondition: The list element at list[location] is removed
  147.         //    and length is decremented by 1.
  148.         //    If location is out of range, an appropriate message
  149.         //    is printed.
  150.     public void removeAt(int location)
  151.     {
  152.         if(location < 0 || location >= length)
  153.             System.err.println("The location of the item to be removed "
  154.                              + "is out of range.");
  155.         else
  156.         {
  157.             for(int i = location; i < length - 1; i++)
  158.                   list[i] = list[i+1];
  159.  
  160.             list[length - 1] = null;
  161.  
  162.             length--;
  163.         }
  164.     } //end removeAt
  165.  
  166.  
  167.         //Method to retrieve  the element from the list at the
  168.         //position specified by location.
  169.         //Postcondition: A copy of the element at the position
  170.         //               specified by location is returned.
  171.         //               If location is out of range, an
  172.         //               appropriate message is printed and
  173.         //               null is returned.
  174.     public DataElement retrieveAt(int location)
  175.     {
  176.         if(location < 0 || location >= length)
  177.         {
  178.            System.err.println("The location of the item to be "
  179.                             + "retrieved is out of range.");
  180.            return null;
  181.         }
  182.         else
  183.            return list[location].getCopy();
  184.     } //end retrieveAt
  185.  
  186.         //Method to replace the element in the list at
  187.         //the position specified by location with repItem.
  188.         //Postcondition: list[location] = repItem
  189.         //     If location is out of range, an appropriate
  190.         //     message is printed.
  191.     public void replaceAt(int location, DataElement repItem)
  192.     {
  193.         if(location < 0 || location >= length)
  194.               System.err.println("The location of the item to be replaced "
  195.                                + "is out of range.");
  196.         else
  197.            list[location].makeCopy(repItem);
  198.     } //end replaceAt
  199.  
  200.           //Method to remove all the elements from the list.
  201.           //Postcondition: length = 0
  202.     public void clearList()
  203.     {
  204.         for(int i = 0; i < length; i++)
  205.             list[i] = null;
  206.  
  207.           length = 0;
  208.  
  209.         System.gc();
  210.     } //end clearList
  211.  
  212.         //Method to determine whether searchItem is in the list.
  213.         //Postcondition: If searchItem is found, returns the location
  214.         //               in the array where searchItem is found;
  215.         //               otherwise, returns -1.
  216.     public abstract int binarySearch(DataElement[]list, DataElement searchItem);
  217.  
  218.  
  219.         //Method to insert insertItem in the list.
  220.         //However, first the list is searched to see whether
  221.         //the item to be inserted is already in the list.
  222.         //Postcondition: insertItem is inserted and length++
  223.         //           If insertItem is already in the list or the list
  224.         //           is full, an appropriate message is output.
  225.     public abstract void insert(DataElement insertItem);
  226.  
  227.  
  228.         //Method to remove an item from the list.
  229.         //The parameter removeItem specifies the item to
  230.         //be removed.
  231.         //Postcondition: If removeItem is found in the list, it is
  232.         //               removed from the list and length is
  233.         //               decremented by one.
  234.  
  235.     public void copyList(ArrayListClass otherList)
  236.     {
  237.           if(this != otherList) //avoid self-assignment
  238.           {
  239.               for(int j = 0; j < length; j++)  //destroy this list
  240.                 list[j] = null;
  241.             System.gc();
  242.  
  243.                 maxSize = otherList.maxSize;
  244.             length = otherList.length;
  245.             list = new DataElement[maxSize];    //create the array
  246.  
  247.             for(int j = 0; j < length; j++)     //copy otherList
  248.                 list[j] = otherList.list[j].getCopy();
  249.           }
  250.     }
  251. }
  252.  
  253.  
Nov 21 '06 #1
0 1275

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: lagunasun | last post by:
not shitass responses. People like this david fellow, degrade the board. again, here is my msg, and thanks again for any assistance! I am going from learning c++ console apps to windows, and need...
0
by: Jawahar | last post by:
All I had posted this in the remote assistance group and could not get any replies so I thought that I could try in the developer group Thanks One of the issues we face when helping our remote...
2
by: CSDunn | last post by:
Hello, I need some assistance with error handling in an Access 2003 Project form. The project is has a data source connection to a SQL Server 2000 database. The main form is named...
0
by: Joe Ross | last post by:
(Apologies in advance if there is a better forum for asking advice on this topic). Our ASP.NET application occasionally starts spitting out OutOfMemory exceptions. When this happens, the memory...
0
by: mwalk66 | last post by:
I have been able to create a web based company phone book by retrieving the last name, first name, email address and telephone # from active directory. However I need serious assistance in...
1
by: Chris | last post by:
Hi, How can I post a web request like http://site.com/lookupvalue.aspx?id=xxxxx and then parse the request ex Home
0
by: serpius | last post by:
Hello Everyone, I am a beginner in VB.Net 2003 and am taking classes in beginning VB.net. To make a long story short, I am looking for real samples of coding in Console Apps. I am the type of...
46
by: Bruce W. Darby | last post by:
This will be my very first VB.Net application and it's pretty simple. But I've got a snag in my syntax somewhere. Was hoping that someone could point me in the right direction. The history: My...
1
by: DR | last post by:
What ports do i need to unblock on client and server (running msvsmon.exe) to debug remotely from my client box with visual studio 2005 pro? When I attach to remote process a connection shows up...
3
by: gaurav92K | last post by:
sir i am working in a company . there are many pc. i want to use remote assistance. i configure all group policy which are related remote assistance.and i enable service through remote in system...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.