Page 1 of 2 12 LastLast
Results 1 to 20 of 40
  1. #1
    SMHouston is offline Member
    Join Date
    Sep 2009
    Posts
    80
    Rep Power
    0

    Default Problem with finding the max element

    Ok, I have a program I set up and It asks the user to enter 8 integers, it then asks for the number they want deleted, then asks for the position of the num they want deleted. Everything there works fine.

    I put in a block of code to find the maximum element in the list and want it printed out after it shows them the list. Everything compiles right, but no matter what I do it always return the maximum element in the list is: 0.

    I've read and reread my book and the code looks right to me, its just not grabbing number.

    Here's the code in the class I have set up for determining the max element:

    Java Code:
    public void findMax(int[] array) 
    {
          int maxElement = array[0];
    	   for (int i=0; i<array.length; i++) 
    	   {
    	      if (array[i]>maxElement) 
    		{
    	         maxElement = array[i];
    	        }
    	   }
      	   
    }
    	
    	
    public int getMaxElement()
    {
    	return maxElement;
    }
    And here's the block of code I'm trying to get it to print out.

    Java Code:
    System.out.println();
    System.out.println("The maximum element in the list is " + intList.getMaxElement());
    System.out.println();
    forgoed putting the rest of the program up because it works fine.
    Last edited by SMHouston; 09-12-2009 at 01:31 AM.

  2. #2
    emceenugget is offline Senior Member
    Join Date
    Sep 2008
    Posts
    564
    Rep Power
    7

    Default

    uh... did you start by calling the actual findMax() method?
    Last edited by emceenugget; 09-12-2009 at 01:44 AM. Reason: typo...

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    the two maxElement variables are not the same -- the one in findMax, and the one in getMaxElement.

    Besides, why have two methods here? Make findMax return an int -- the maxElement it finds, and use it.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by emceenugget View Post
    uh... did you start by calling the actual findMax() method?
    I don't think it would matter if he did this or not.

  5. #5
    emceenugget is offline Senior Member
    Join Date
    Sep 2008
    Posts
    564
    Rep Power
    7

    Default

    good point. didn't notice the local declaration.

  6. #6
    SMHouston is offline Member
    Join Date
    Sep 2009
    Posts
    80
    Rep Power
    0

    Default

    Good call, but I think something else is wrong. Here's the error I'm getting:

    findMax(int[]) in ArrayListClass cannot be applied to ()
    intList.findMax();
    ^
    1 error

    Here's the full code, something else might be wrong and I'm just not seeing it.

    Java Code:
    public abstract class ArrayListClass
    {
        protected int length;           //to store the length of the list
        protected int maxSize;          //to store the maximum size of the list
        protected DataElement[] list;   //array to hold the list elements
    	 protected int maxElement;			//to store the maximum element value in the list
    	 protected int minElement;			//to store the minimum element value in the list
            
    		  //Default constructor
            //Creates an array of size 100
            //Postcondition: list points to the array, length = 0,
            //               and maxSize = 100
        public ArrayListClass()
        {
            maxSize = 100;
    
            length = 0;
            list = new DataElement[maxSize];
        }
    
            //Constructor with parameter
            //Creates an array of size specified by the parameter
            //size.
            //Postcondition: list points to the array, length = 0,
            //               and maxSize = size
        public ArrayListClass(int size)
        {
            if(size <= 0)
            {
                System.err.println("The array size must be positive. "
                                 + "Creating an array of size 100. ");
                 maxSize = 100;
            }
            else
                maxSize = size;
    
            length = 0;
            list = new DataElement[maxSize];
        }
    
             //copy constructor
        public ArrayListClass(ArrayListClass otherList)
        {
            maxSize = otherList.maxSize;
            length = otherList.length;
            list = new DataElement[maxSize];    //create the array
    
            for(int j = 0; j < length; j++)  //copy otherList
                list[j] = otherList.list[j].getCopy();
        }//end copy constructor
    
    
            //Method to determine whether the list is empty.
            //Postcondition: Returns true if the list is empty; 
            //               otherwise, returns false.
        public boolean isEmpty()
        {
            return (length == 0);
        }
    
            //Method to determine whether the list is full.
            //Postcondition: Returns true if the list is full; 
            //               otherwise, returns false.
        public boolean isFull()
        {
            return (length == maxSize);
        }
    
            //Method to return the number of elements in the list.
            //Postcondition: Returns the value of length.
        public int listSize()
        {
            return length;
        }
    
            //Method to return the maximum size of the list.
            //Postcondition: Returns the value of maxSize.
        public int maxListSize()
        {
            return maxSize;
        }
    
            //Method to output the elements of the list.
            //Postcondition: Elements of the list are output on the
            //standard output device.
        public void print()
        {
            for(int i = 0; i < length; i++)
                System.out.print(list[i] + " ");
            System.out.println();
        }
    
            //Method to determine whether item is the same as the item in 
            //the list at the position specified by location.
            //Postcondition: Returns true if list[location] is 
            //               same as location; otherwise, returns false.
        public boolean isItemAtEqual(int location, DataElement item)
        {
              return(list[location].equals(item));
        }
    
            //Method to insert insertItem in the list at the position
            //specified by location.
            //Postcondition: Starting at location, the elements of the list 
            //               are shifted to make room for the new item, 
            //               list[location] = insertItem;, and
            //               length++;    
            //     If the list is full or location is out of range,
            //     an appropriate message is displayed.
        public void insertAt(int location, DataElement insertItem)
        {
            if(location < 0 || location >= maxSize)
                System.err.println("The position of the item to be inserted "
                                  + "is out of range");
            else
                if(length >= maxSize)  //list is full
                    System.err.println("Cannot insert in a full list.");
                else
                {
                    for(int i = length; i > location; i--)
                        list[i] = list[i - 1];  //move the elements down
    
                    list[location] = insertItem.getCopy();  //insert the
                                            //item at the specified position
                    length++;   //increment the length
                }
        } //end insertAt
    
            //Method to inserts insertItem at the end of the list.
            //Postcondition: list[length] = insertItem; and length++;
            //           If the list is full, an appropriate
            //           message is displayed.
        public void insertEnd(DataElement insertItem)
        {
            if(length >= maxSize)  //the list is full
                System.err.println("Cannot insert in a full list.");
            else
            {
                  list[length] = insertItem.getCopy();  //insert the
                                                      //item at the end
                  length++; //increment the length
            }
        } //end insertEnd
    
            //Method to remove the item from the list at the position
            //specified by location.
            //Postcondition: The list element at list[location] is removed
            //    and length is decremented by 1.
            //    If location is out of range, an appropriate message
            //    is printed.
        public void removeAt(int location)
        {
            if(location < 0 || location >= length)
                System.err.println("The location of the item to be removed "
                                 + "is out of range.");
            else
            {
                for(int i = location; i < length - 1; i++)
                      list[i] = list[i+1];
    
                list[length - 1] = null;
    
                length--;
            }
        } //end removeAt
    
    
            //Method to retrieve  the element from the list at the
            //position specified by location.
            //Postcondition: A copy of the element at the position 
            //               specified by location is returned.
            //               If location is out of range, an 
            //               appropriate message is printed and 
            //               null is returned.
        public DataElement retrieveAt(int location)
        {
            if(location < 0 || location >= length)
            {
               System.err.println("The location of the item to be "
                                + "retrieved is out of range.");
               return null;
            }
            else
               return list[location].getCopy();
        } //end retrieveAt
    
            //Method to replace the element in the list at 
            //the position specified by location with repItem. 
            //Postcondition: list[location] = repItem
            //     If location is out of range, an appropriate
            //     message is printed.
        public void replaceAt(int location, DataElement repItem)
        {
            if(location < 0 || location >= length)
                  System.err.println("The location of the item to be replaced "
                                   + "is out of range.");
            else
               list[location].makeCopy(repItem);
        } //end replaceAt
    
              //Method to remove all the elements from the list.
              //Postcondition: length = 0
        public void clearList()
        {
            for(int i = 0; i < length; i++)
                list[i] = null;
    
              length = 0;
    
            System.gc();
        } //end clearList
    
            //Method to determine whether searchItem is in the list.
            //Postcondition: If searchItem is found, returns the location
            //               in the array where searchItem is found;
            //               otherwise, returns -1.
        public abstract int seqSearch(DataElement searchItem);
    
    
            //Method to insert insertItem in the list. 
            //However, first the list is searched to see whether
            //the item to be inserted is already in the list.
            //Postcondition: insertItem is inserted and length++
            //           If insertItem is already in the list or the list
            //           is full, an appropriate message is output.
        public abstract void insert(DataElement insertItem);
    
    
            //Method to remove an item from the list.
            //The parameter removeItem specifies the item to
            //be removed.
            //Postcondition: If removeItem is found in the list, it is
            //               removed from the list and length is
            //               decremented by one.
        public abstract void remove(DataElement removeItem);
    
            //Method to make a copy of the other list.
            //Postcondition: This list is destroyed and a copy of
            //               otherList is assigned to this list.
        public void copyList(ArrayListClass otherList)
        {
              if(this != otherList) //avoid self-assignment
              {
                  for(int j = 0; j < length; j++)  //destroy this list
                    list[j] = null;
                System.gc();
    
                    maxSize = otherList.maxSize;
                length = otherList.length;
                list = new DataElement[maxSize];    //create the array
    
                for(int j = 0; j < length; j++)     //copy otherList
                    list[j] = otherList.list[j].getCopy();
              }
         }
    	
    	 public void findMax(int array[]) 
    	 {
    	   int maxElement = array[0];
    	   for (int i=0; i<array.length; i++) 
    		{
    	      if (array[i]>maxElement) 
    			{
    	         maxElement = array[i];
    	      }
    	   }
      	   
        }
    	
    	
    	public int getMaxElement()
    	{
    		return maxElement;
    	}
    }
    Here's the section of my test program that is having hte problem:

    Java Code:
    intList.findMax();
    System.out.println();
    System.out.println("The maximum element in the list is " + intList.getMaxElement());
    System.out.println();
    Last edited by SMHouston; 09-12-2009 at 02:07 AM.

  7. #7
    emceenugget is offline Senior Member
    Join Date
    Sep 2008
    Posts
    564
    Rep Power
    7

    Default

    first fix the issue that fubarable mentioned. then think about the error (hint: you're calling a method whose signature doesn't exist).

  8. #8
    SMHouston is offline Member
    Join Date
    Sep 2009
    Posts
    80
    Rep Power
    0

    Default

    Do you mean by setting its default in the default constructor?
    Java Code:
    //Default constructor
    //Creates an array of size 100
    //Postcondition: list points to the array, length = 0,
    //               and maxSize = 100
        public ArrayListClass()
        {
            maxSize = 100;
            maxElement = 0;
            length = 0;
            list = new DataElement[maxSize];
        }
    I've tried several things here like maxElement = 0; etc and I still get the same error. Think I might of been starting at this thing to long :)
    I took out the intList.findMax(); and everything compiles right, the error is gone, however I still get 0 returned everytime no matter what values i put in the program.

    Think I fixed the problem fubar was talking about by making it one method

    Java Code:
    public int findMax(int array[]) 
    	 {
    	   int maxElement = array[0];
    	   for (int i=0; i<array.length; i++) 
    		{
    	      if (array[i]>maxElement) 
    			{
    	         maxElement = array[i];
    	      }
    	   }
      	  	return maxElement; 
        }
    Just not seeing what I'm missing here.
    Last edited by SMHouston; 09-12-2009 at 03:01 AM.

  9. #9
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    When you declare a method as accepting an array, you must pass it an array when calling it.

  10. #10
    SMHouston is offline Member
    Join Date
    Sep 2009
    Posts
    80
    Rep Power
    0

    Default

    Ok, I understand to a point what your saying.

    So I have my method in my class which accepts an array, when I call it in the main program I have to call it using an array aswell.

    Everytime I try to put something in there though I get this:

    findMax(int[]) in ArrayListClass cannot be applied to (UnorderedArrayList)

    Not sure why I can't use intList like my first method to print out the list they entered to start with, then take that list and find the max.
    Only thing I can think is I have my method in my class which figures out the max element wrong.
    Last edited by SMHouston; 09-12-2009 at 04:39 PM.

  11. #11
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Hmm,... I have to wonder....

    If what you are doing with the findMax method is trying to find the max of the elements held by the ArrayListClass object, then findMax perhaps shouldn't take any parameters since you will be trying to find the max of the array already held by the object.

  12. #12
    SMHouston is offline Member
    Join Date
    Sep 2009
    Posts
    80
    Rep Power
    0

    Default

    I've tried it numerous times without holding any parameters. I just get a bunch of errors with the variable array etc.

    So what your saying, it I should possibly declare a new array in my class , then make the method not hold any parameters and grab the intList values from the main program? Hope that makes sense

  13. #13
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by SMHouston View Post
    I've tried it numerous times without holding any parameters. I just get a bunch of errors with the variable array etc.
    This doesn't tell us much unfortunately. To help with specific problems, we will need to see code and error messages.

    So what your saying, it I should possibly declare a new array in my class , then make the method not hold any parameters and grab the intList values from the main program? Hope that makes sense
    No, I am not saying that. Your class already holds an array:

    DataElement[] list

    What I am asking is that when you are desiring the max element, are you desiring the max element that is held in this array that is already held by this class?

  14. #14
    SMHouston is offline Member
    Join Date
    Sep 2009
    Posts
    80
    Rep Power
    0

    Default

    If I understand you right, yes.

    I want the max element to be found out of the list the user provides in the main program, also the min, but I'm working on the getting the max first.

    This is what I've tried in a few different ways, keep getting the same error and not even sure if it works.

    Java Code:
     
    public void findMax(DataElement maxElement)
    {
    	   
    	for (int i=0; i < length; i++) 
    	{
    	      if (list[i] > maxElement) 
    	      {
    	         maxElement = list[i];
    	      }
    	}
      	  
    }
    		
    public int maxElem()
    {
    	return maxElement;
    }
    Tried to declare it just like the other methods in the class are. This is the error that I'm getting.

    Java Code:
    ArrayListClass.java:263: operator > cannot be applied to DataElement,DataElement
    	      if (list[i] > maxElement) 
    	                  ^
    1 error
    I've also tried it like this:

    Java Code:
    public void findMax(DataElement list, int maxElement)
    	 {
    	   
    	   for (int i=0; i < length; i++) 
    		{
    	      if (list[i] > maxElement) 
    			{
    	         maxElement = list[i];
    	      }
    	   }
      	  
        }
    		
    	 public int maxElem()
    	 {
    	 	return maxElement;
    	 }
    and get this for an error:

    Java Code:
    ArrayListClass.java:263: array required, but DataElement found
    	      if (list[i] > maxElement) 
    	              ^
    ArrayListClass.java:265: array required, but DataElement found
    	         maxElement = list[i];
    	                          ^
    Not real sure whats going on here, the other elements of my program came together nicely with minimal errors that were easily fixed, but this just won't seem to go for me.

    If I try it without any parameters at all I get :

    Java Code:
    ArrayListClass.java:265: incompatible types
    found   : DataElement
    required: int
    	         maxElement = list[i];
    Last edited by SMHouston; 09-12-2009 at 06:50 PM.

  15. #15
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Closer, but I wouldn't pass anything to this method since you're not trying to find if the elements are greater than one single element, you're instead trying to find the max element in the whole list. So instead of this:
    Java Code:
    public void findMax(DataElement maxElement)
    {
    Better would be
    Java Code:
    public void findMax()
    {
    Also, I wouldn't have two methods, findMax() and maxElement(), but would combine them into one method that returns the max element.

    So better still would be:
    Java Code:
    // assuming findMax returns an int
    public int findMax()
    {
    Also, it appears that your list array doesn't
    hold ints, but instead holds DataElement objects. This begs the question, just what is this class? Does it implement the Comparable<DataElement> interface? If not, how do you plan to compare one DataElement object with another? You can't simply use > or < to do this, you know.

    The best solution to these questions in my mind would be to have DataElement implement the Comparable<DataElement> interface, give DataElement an appropriate CompareTo(DataElement otherDataElement) method, and then in your findMax() method, call the compareTo to get the max element.

    So findMax method signature would then look like this:
    Java Code:
    public DataElement findMax()
    {

  16. #16
    SMHouston is offline Member
    Join Date
    Sep 2009
    Posts
    80
    Rep Power
    0

    Default

    Ok, what the program does is asks the user for 8 integers in one line.

    They input their integers, it then prints out a copy of their list, and tells them if they have duplicates in the list. After that it then asks the user to delete an integer which it does, tells them if the integer isn't in the list or not aswell. Then goes on to ask them to pick a location they would like to delete, tells them if the location is out of bound, which it does.

    It then uses a search function and they can search if an item is in their list or not, then the program ends. Everything is working properly except this little speed bump of finding the max element in the list.

    After rearranging the code like you suggested back into one method without any parameters I get the same incompatible data types over and over.

    Java Code:
    ArrayListClass.java:263: operator > cannot be applied to DataElement,int
    	      if (list[i] > maxElement) 
    	                  ^
    ArrayListClass.java:265: incompatible types
    found   : DataElement
    required: int
    	         maxElement = list[i];
    	                          ^
    ArrayListClass.java:268: incompatible types
    found   : int
    required: DataElement
    		return maxElement;
    		       ^
    3 errors
    Last edited by SMHouston; 09-12-2009 at 06:58 PM.

  17. #17
    SMHouston is offline Member
    Join Date
    Sep 2009
    Posts
    80
    Rep Power
    0

    Default

    I took out the maxelement part so you could see the working program:

    Program :

    Java Code:
    //Test Program Integer Array List
    
    import java.io.*;
    import java.util.*;
    
    public class Example3_1
    {
        static BufferedReader keyboard = new
               BufferedReader(new InputStreamReader(System.in));
    
        public static void main(String[] args) throws IOException
        {
            UnorderedArrayList intList
                              = new UnorderedArrayList(50);             //Line 1
            UnorderedArrayList temp =
                                new UnorderedArrayList();               //Line 2
    
            IntElement num = new IntElement();                          //Line 3
    		  
            int counter;                                                //Line 4
            int position;                                               //Line 5
    
            StringTokenizer tokenizer;                                  //Line 6
    
            System.out.println("Line 7: Processing the "
                             + "integer list");                         //Line 7
            System.out.print("Line 8: Enter 8 integers on the "
                            + "same line: ");                           //Line 8
            System.out.flush();                                         //Line 9
    
            tokenizer = new
                       StringTokenizer(keyboard.readLine());            //Line 10
    
            for(counter = 0; counter < 8; counter++)                    //Line 11
            {
                num.setNum(Integer.parseInt(tokenizer.nextToken()));    //Line 12
                intList.insert(num);                                    //Line 13
            }
    
            temp.copyList(intList);                                     //Line 14
    
            System.out.println();                                       //Line 15
            System.out.print("Line 16: The list you "
                           + "entered is: ");                           //Line 16
            intList.print();                                            //Line 17
            System.out.println();                                       //Line 18
    
    		  System.out.print("Line 19: Enter the num to "
                           + "be deleted: ");                           //Line 19
            System.out.flush();                                         //Line 20
            num.setNum(Integer.parseInt(keyboard.readLine()));          //Line 21
            System.out.println();                                       //Line 22
    
            intList.remove(num);                                        //Line 23
            System.out.println("Line 24: After removing "
                              + num
                              + " the list is:");                       //Line 24
            intList.print();                                            //Line 25
            System.out.println();                                       //Line 26
    
            System.out.print("Line 27: Enter the position of "
                           + "the num to be deleted: ");                //Line 27
            System.out.flush();                                         //Line 28
            position = Integer.parseInt(keyboard.readLine());           //Line 29
            System.out.println();                                       //Line 30
    
            intList.removeAt(position);                                 //Line 31
            System.out.println("Line 32: After removing the "
                              + "element at position "
                              + position
                              + ", intList:");                          //Line 32
    
            intList.print();                                            //Line 33
            System.out.println();                                       //Line 34
    
            System.out.print("Line 35: Enter the search "
                            +  "item: ");                               //Line 35
            System.out.flush();                                         //Line 36
    
            num.setNum(Integer.parseInt(keyboard.readLine()));          //Line 37
            System.out.println();                                       //Line 38
    
            if(intList.seqSearch(num) != -1)                            //Line 39
               System.out.println("Line 40: Item found in "
                                + "the list");                          //Line 40
            else                                                        //Line 41
               System.out.println("Line 42: Item not found");           //Line 42
    
            System.out.print("List 43: The list temp: ");               //Line 43
            temp.print();                                               //Line 44
            System.out.println();                                       //Line 45
        }
    }

    Java Code:
    Class:
    
    public abstract class ArrayListClass
    {
        protected int length;           //to store the length of the list
        protected int maxSize;          //to store the maximum size of the list
        protected DataElement[] list;   //array to hold the list elements
    	        
    		  //Default constructor
            //Creates an array of size 100
            //Postcondition: list points to the array, length = 0,
            //               and maxSize = 100
        public ArrayListClass()
        {
            maxSize = 100;
    		  length = 0;
            list = new DataElement[maxSize];
        }
    
            //Constructor with parameter
            //Creates an array of size specified by the parameter
            //size.
            //Postcondition: list points to the array, length = 0,
            //               and maxSize = size
        public ArrayListClass(int size)
        {
            if(size <= 0)
            {
                System.err.println("The array size must be positive. "
                                 + "Creating an array of size 100. ");
                 maxSize = 100;
            }
            else
                maxSize = size;
    
            length = 0;
            list = new DataElement[maxSize];
        }
    
             //copy constructor
        public ArrayListClass(ArrayListClass otherList)
        {
            maxSize = otherList.maxSize;
            length = otherList.length;
            list = new DataElement[maxSize];    //create the array
    
            for(int j = 0; j < length; j++)  //copy otherList
                list[j] = otherList.list[j].getCopy();
        }//end copy constructor
    
    
            //Method to determine whether the list is empty.
            //Postcondition: Returns true if the list is empty; 
            //               otherwise, returns false.
        public boolean isEmpty()
        {
            return (length == 0);
        }
    
            //Method to determine whether the list is full.
            //Postcondition: Returns true if the list is full; 
            //               otherwise, returns false.
        public boolean isFull()
        {
            return (length == maxSize);
        }
    
            //Method to return the number of elements in the list.
            //Postcondition: Returns the value of length.
        public int listSize()
        {
            return length;
        }
    
            //Method to return the maximum size of the list.
            //Postcondition: Returns the value of maxSize.
        public int maxListSize()
        {
            return maxSize;
        }
    
            //Method to output the elements of the list.
            //Postcondition: Elements of the list are output on the
            //standard output device.
        public void print()
        {
            for(int i = 0; i < length; i++)
                System.out.print(list[i] + " ");
            System.out.println();
        }
    
            //Method to determine whether item is the same as the item in 
            //the list at the position specified by location.
            //Postcondition: Returns true if list[location] is 
            //               same as location; otherwise, returns false.
        public boolean isItemAtEqual(int location, DataElement item)
        {
              return(list[location].equals(item));
        }
    
            //Method to insert insertItem in the list at the position
            //specified by location.
            //Postcondition: Starting at location, the elements of the list 
            //               are shifted to make room for the new item, 
            //               list[location] = insertItem;, and
            //               length++;    
            //     If the list is full or location is out of range,
            //     an appropriate message is displayed.
        public void insertAt(int location, DataElement insertItem)
        {
            if(location < 0 || location >= maxSize)
                System.err.println("The position of the item to be inserted "
                                  + "is out of range");
            else
                if(length >= maxSize)  //list is full
                    System.err.println("Cannot insert in a full list.");
                else
                {
                    for(int i = length; i > location; i--)
                        list[i] = list[i - 1];  //move the elements down
    
                    list[location] = insertItem.getCopy();  //insert the
                                            //item at the specified position
                    length++;   //increment the length
                }
        } //end insertAt
    
            //Method to inserts insertItem at the end of the list.
            //Postcondition: list[length] = insertItem; and length++;
            //           If the list is full, an appropriate
            //           message is displayed.
        public void insertEnd(DataElement insertItem)
        {
            if(length >= maxSize)  //the list is full
                System.err.println("Cannot insert in a full list.");
            else
            {
                  list[length] = insertItem.getCopy();  //insert the
                                                      //item at the end
                  length++; //increment the length
            }
        } //end insertEnd
    
            //Method to remove the item from the list at the position
            //specified by location.
            //Postcondition: The list element at list[location] is removed
            //    and length is decremented by 1.
            //    If location is out of range, an appropriate message
            //    is printed.
        public void removeAt(int location)
        {
            if(location < 0 || location >= length)
                System.err.println("The location of the item to be removed "
                                 + "is out of range.");
            else
            {
                for(int i = location; i < length - 1; i++)
                      list[i] = list[i+1];
    
                list[length - 1] = null;
    
                length--;
            }
        } //end removeAt
    
    
            //Method to retrieve  the element from the list at the
            //position specified by location.
            //Postcondition: A copy of the element at the position 
            //               specified by location is returned.
            //               If location is out of range, an 
            //               appropriate message is printed and 
            //               null is returned.
        public DataElement retrieveAt(int location)
        {
            if(location < 0 || location >= length)
            {
               System.err.println("The location of the item to be "
                                + "retrieved is out of range.");
               return null;
            }
            else
               return list[location].getCopy();
        } //end retrieveAt
    
            //Method to replace the element in the list at 
            //the position specified by location with repItem. 
            //Postcondition: list[location] = repItem
            //     If location is out of range, an appropriate
            //     message is printed.
        public void replaceAt(int location, DataElement repItem)
        {
            if(location < 0 || location >= length)
                  System.err.println("The location of the item to be replaced "
                                   + "is out of range.");
            else
               list[location].makeCopy(repItem);
        } //end replaceAt
    
              //Method to remove all the elements from the list.
              //Postcondition: length = 0
        public void clearList()
        {
            for(int i = 0; i < length; i++)
                list[i] = null;
    
              length = 0;
    
            System.gc();
        } //end clearList
    
            //Method to determine whether searchItem is in the list.
            //Postcondition: If searchItem is found, returns the location
            //               in the array where searchItem is found;
            //               otherwise, returns -1.
        public abstract int seqSearch(DataElement searchItem);
    
    
            //Method to insert insertItem in the list. 
            //However, first the list is searched to see whether
            //the item to be inserted is already in the list.
            //Postcondition: insertItem is inserted and length++
            //           If insertItem is already in the list or the list
            //           is full, an appropriate message is output.
        public abstract void insert(DataElement insertItem);
    
    
            //Method to remove an item from the list.
            //The parameter removeItem specifies the item to
            //be removed.
            //Postcondition: If removeItem is found in the list, it is
            //               removed from the list and length is
            //               decremented by one.
        public abstract void remove(DataElement removeItem);
    
            //Method to make a copy of the other list.
            //Postcondition: This list is destroyed and a copy of
            //               otherList is assigned to this list.
        public void copyList(ArrayListClass otherList)
        {
              if(this != otherList) //avoid self-assignment
              {
                  for(int j = 0; j < length; j++)  //destroy this list
                    list[j] = null;
                System.gc();
    
                    maxSize = otherList.maxSize;
                length = otherList.length;
                list = new DataElement[maxSize];    //create the array
    
                for(int j = 0; j < length; j++)     //copy otherList
                    list[j] = otherList.list[j].getCopy();
              }
         }
    	
    		
    }
    Just not sure where to go as like I said this part came together good, but for whatever reason I can't get the max out of that stored DataElement.

  18. #18
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

  19. #19
    SMHouston is offline Member
    Join Date
    Sep 2009
    Posts
    80
    Rep Power
    0

    Default

    Sorry about that, here is the other parts

    Java Code:
    public class UnorderedArrayList extends ArrayListClass
    {
    
        public UnorderedArrayList(int size)
        {
              super(size);
        }
    
        public UnorderedArrayList()
        {
            super();
        }
    
            //Copy constructor
        public UnorderedArrayList(UnorderedArrayList otherList)
        {
            super(otherList);
        }
    
            //Method to determine whether searchItem is in the list.
            //Postcondition: If searchItem is found, returns the location
            //               in the array where the searchItem is found;
            //               otherwise, returns -1.
        public int seqSearch(DataElement searchItem)
        {
              int loc;
              boolean found = false;
    
              for(loc = 0; loc < length; loc++)
                  if(list[loc].equals(searchItem))
                  {
                        found = true;
                        break;
                  }
    
              if(found)
                  return loc;
              else
                  return -1;
        } //end seqSearch
    
            //Method to insert insertItem at the end
            //of the list. However, first the list is searched to
            //see whether the item to be inserted is already in the list.
            //Postcondition: list[length] = insertItem and length++
            //           If insertItem is already in the list or the list
            //           is full, an appropriate message is output.
       public void insert(DataElement insertItem)
        {
            int loc;
    
            if(length == 0)          //list is empty
               list[length++] = insertItem.getCopy();  //insert acopy the item
                                                              // andincrement the length
            else
                  if(length == maxSize)
                    System.err.println("Cannot insert in a full list.");
                  else
                  {
                      loc = seqSearch(insertItem);
    
                      if(loc == -1)   //the item to be inserted
                                          //does not exist in the list
                            list[length++] = insertItem.getCopy();
                      else
                            System.err.println("The item to be inserted is already in "
                                             + "the list. No duplicates are allowed.");
                  }
        } //end insert
    
            //Method to remove an item from the list.
            //The parameter removeItem specifies the item to 
            //be removed.
            //Postcondition: If removeItem is found in the list, it is
            //               removed from the list and length is
            //               decremented by one.
        public void remove(DataElement removeItem)
        {
            int loc;
    
              if(length == 0)
                  System.err.println("Cannot delete from an empty list.");
              else
              {
                  loc = seqSearch(removeItem);
    
                  if(loc != -1)
                        removeAt(loc);
                  else
                        System.out.println("The item to be deleted is "
                                     + "not in the list.");
              }
        } //end remove
    }
    Java Code:
    public class IntElement extends DataElement
    {
        protected int num;
    
          //default constructor
        public IntElement()
        {
            num = 0;
        }
    
          //constructor with a parameter
        public IntElement(int x)
        {
            num = x;
        }
          //copy constructor
        public IntElement(IntElement otherElement)
        {
            num = otherElement.num;
        }
    
          //Method to set the value of the instance variable num.
          //Postcondition: num = x;
        public void setNum(int x)
        {
            num = x;
        }
    
          //Method to return the value of the instance variable num.
          //Postcondition: The value of num is returned.
        public int getNum()
        {
            return num;
        }
    
        public boolean equals(DataElement otherElement)
        {
            IntElement temp = (IntElement) otherElement;
            return (num == temp.num);
        }
    
        public int compareTo(DataElement otherElement)
        {
            IntElement temp = (IntElement) otherElement;
            return (num - temp.num);
        }
    
        public void makeCopy(DataElement otherElement)
        {
            IntElement temp = (IntElement) otherElement;
            num = temp.num;
        }
    
        public DataElement getCopy()
        {
            IntElement temp = new IntElement(num);
            return temp;
        }
    
        public String toString()
        {
            return String.valueOf(num);
        }
    }
    Java Code:
    public abstract class DataElement
    {
        public abstract boolean equals(DataElement otherElement);
          //Method to determine whether two objects contain the 
          //same data.
          //Postcondition: Returns true if this object contains the 
          //               same data as the object otherElement;
          //               otherwise, it returns false.
          
        public abstract int compareTo(DataElement otherElement);
          //Method to compare two objects.
          //Postcondition: Returns a value < 0 if this object is 
          //                    less than the object otherElement;
          //               Returns 0 if this object is the same as 
          //                    the object otherElement.
          //               Returns a value > 0 if this object is 
          //                  greater than the object otherElement.
          
        public abstract void makeCopy(DataElement otherElement);
          //Method to copy otherElement into this object.
          //Postcondition: The data of otherElement is copied into
          //               this object.
          
        public abstract DataElement getCopy();
          //Method to return a copy of this object.
          //Postcondition: A copy of this object is created and
          //               a reference of the copy is returned.
    }

  20. #20
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

Page 1 of 2 12 LastLast

Similar Threads

  1. How to access element in a CSV?
    By venkatteshb in forum New To Java
    Replies: 11
    Last Post: 08-17-2008, 12:37 PM
  2. how can we get the element of by using the hashtable
    By raj reddy in forum Web Frameworks
    Replies: 1
    Last Post: 05-06-2008, 02:45 PM
  3. ArrayList problem (finding largest no)
    By bugger in forum New To Java
    Replies: 3
    Last Post: 12-12-2007, 01:47 PM
  4. Max element in an Array
    By mew in forum New To Java
    Replies: 5
    Last Post: 12-03-2007, 06:26 PM
  5. a no such element exception
    By headlice1 in forum New To Java
    Replies: 1
    Last Post: 08-07-2007, 06:36 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •