Need help with this program

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • satan
    New Member
    • Oct 2006
    • 28

    #1

    Need help with this program

    The method remove of the class ArrayListClass removes only
    the first occurence of an element. Add the method removeAll as an abstract method to the class ArrayListClass that would remove all occurrences of a given element. Also, write the definition of the method removeAll for the class UnorderedArrayL ist and a program to test this method.

    This is what i've done so far


    public class UnorderedArrayL ist extends ArrayListClass
    {

    public UnorderedArrayL ist(int size)
    {
    super(size);
    }

    public UnorderedArrayL ist()
    {
    super();
    }

    //Copy constructor
    public UnorderedArrayL ist(UnorderedAr rayList 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(DataE lement searchItem)
    {
    int loc;
    boolean found = false;

    for(loc = 0; loc < length; loc++)
    if(list[loc].equals(searchI tem))
    {
    found = true;
    break;
    }

    if(found)
    return loc;
    else
    return -1;
    } //end seqSearch

    //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(DataElem ent removeItem)
    {
    int loc;

    if(length == 0)
    System.err.prin tln("Cannot delete from an empty list.");
    else
    {
    loc = seqSearch(remov eItem);

    if(loc != -1)
    removeAt(loc);
    else
    System.out.prin tln("The item to be deleted is "
    + "not in the list.");
    }
    } //end remove

    public void removeAll(DataE lement removeItem)
    {
    int loc;
    int locn;

    boolean found = false;

    if (length == 0)
    System.err.prin tln("Cannot delete from an empty list");
    else
    {
    loc = seqSearch(remov eItem);

    for (loc = 0; loc < length; loc++)
    if (list[loc].equals(removeI tem))
    {
    if (list[loc] == removeItem)
    {
    found = true;

    locn = seqSearch(remov eItem);

    for(locn = loc + 1; locn < length; locn++)
    list[locn-1] = list[locn];
    removeAt(loc);
    removeAt(locn);


    loc--;
    }
    else
    break;
    }

    if (!found)
    System.out.prin tln("The item to be deleted is " + " is not in the list");
    }
    }
    }
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Originally posted by satan
    The method remove of the class ArrayListClass removes only
    the first occurence of an element. Add the method removeAll as an abstract method to the class ArrayListClass that would remove all occurrences of a given element. Also, write the definition of the method removeAll for the class UnorderedArrayL ist and a program to test this method.

    This is what i've done so far


    public class UnorderedArrayL ist extends ArrayListClass
    {

    public UnorderedArrayL ist(int size)
    {
    super(size);
    }

    public UnorderedArrayL ist()
    {
    super();
    }

    //Copy constructor
    public UnorderedArrayL ist(UnorderedAr rayList 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(DataE lement searchItem)
    {
    int loc;
    boolean found = false;

    for(loc = 0; loc < length; loc++)
    if(list[loc].equals(searchI tem))
    {
    found = true;
    break;
    }

    if(found)
    return loc;
    else
    return -1;
    } //end seqSearch

    //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(DataElem ent removeItem)
    {
    int loc;

    if(length == 0)
    System.err.prin tln("Cannot delete from an empty list.");
    else
    {
    loc = seqSearch(remov eItem);

    if(loc != -1)
    removeAt(loc);
    else
    System.out.prin tln("The item to be deleted is "
    + "not in the list.");
    }
    } //end remove

    public void removeAll(DataE lement removeItem)
    {
    int loc;
    int locn;

    boolean found = false;

    if (length == 0)
    System.err.prin tln("Cannot delete from an empty list");
    else
    {
    loc = seqSearch(remov eItem);

    for (loc = 0; loc < length; loc++)
    if (list[loc].equals(removeI tem))
    {
    if (list[loc] == removeItem)
    {
    found = true;

    locn = seqSearch(remov eItem);

    for(locn = loc + 1; locn < length; locn++)
    list[locn-1] = list[locn];
    removeAt(loc);
    removeAt(locn);


    loc--;
    }
    else
    break;
    }

    if (!found)
    System.out.prin tln("The item to be deleted is " + " is not in the list");
    }
    }
    }
    First is your user name your real name?
    If not, I strongly suggest that you change it something more friendly.
    Second, when posting code, make use of the code tags. It makes the code more readable?
    Now for your problem, you have'nt specified where the problem is exactly. On which method do you need help?

    Comment

    • satan
      New Member
      • Oct 2006
      • 28

      #3
      Do u get problem with my user name
      Last edited by satan; Oct 11 '06, 06:20 AM. Reason: error

      Comment

      • r035198x
        MVP
        • Sep 2006
        • 13225

        #4
        Originally posted by satan
        Do u get problem with my user name
        Only if it's not your real name

        Comment

        • satan
          New Member
          • Oct 2006
          • 28

          #5
          I think u r in this forum to help people, not to talk about their name or user name

          Comment

          • r035198x
            MVP
            • Sep 2006
            • 13225

            #6
            Originally posted by satan
            I think u r in this forum to help people, not to talk about their name or user name
            There many ways of helping people besides giving them code

            Comment

            • satan
              New Member
              • Oct 2006
              • 28

              #7
              I did not ask u to give me codes. If u r unable to help me, u don't have to say somethin' instead of askin' about my name

              Comment

              • r035198x
                MVP
                • Sep 2006
                • 13225

                #8
                Originally posted by satan
                I did not ask u to give me codes. If u r unable to help me, u don't have to say somethin' instead of askin' about my name
                You also did not ask anything. That's why I asked you to specify the problem you are having with your code.

                Comment

                Working...