Tough assignment

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • TexasNewbie
    New Member
    • Dec 2006
    • 23

    Tough assignment

    I have to write a method , RemoveAll that takes three parameters: an array of integers the length of an array and an integer. Say RemoveItem. The method should find and delete all occurences of removeItem in the array.

    I have told the instructor that I am not sure exactly what he wants me to do and I have looked at all the instances of removeItems in the script and still I am clueless.

    Can someone explain what I should be doing IN ENGLISH?
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Originally posted by TexasNewbie
    I have to write a method , RemoveAll that takes three parameters: an array of integers the length of an array and an integer. Say RemoveItem. The method should find and delete all occurences of removeItem in the array.

    I have told the instructor that I am not sure exactly what he wants me to do and I have looked at all the instances of removeItems in the script and still I am clueless.

    Can someone explain what I should be doing IN ENGLISH?
    After we finish wruting this you should see that there is no need for the length of the array to be passed in as well. Now we tackle the philosophical aspects first. Suppose you have the following array
    {1, 2, 3, 4} and are asked to remove 2. What should be the result? Is it {1, 0, 3, 4} or {1, 3, 4}? It is probably {1, 3, 4} right. Now java arrays don't shrink or stretch so we will need to create a new array to hold the numbers with the occurrences removed. While we are working on the array we need to know which index has had it's value removed. For this we need something more than an int[] because just setting 0 won't work. We might be asked to remove 0 itself! So here is the deal, we are going to make use of an Integer array in our function. Here is how the first part looks like

    Code:
     
    public int[] removeAll(int[] numbers, int length, int removeItem) {
      Integer[] items = new Integer[numbers.length];
      for(int i = 0; i < numbers.length;i++) {
       items[i] = numbers[i];
      }
     }
    Do you understand any of this?

    Comment

    • TexasNewbie
      New Member
      • Dec 2006
      • 23

      #3
      okay, so far I am with you. he also put in here that I can assume that the array is unsorted. which I am assuming is that the integers are not in recognizable order and that they are random. So I guess that what needs to be created first is the three parameters, is that correct?

      Comment

      • dmjpro
        Top Contributor
        • Jan 2007
        • 2476

        #4
        try this code .............
        void removeAll(int [] elements,int length,int remove_item)
        {
        Vector l_temp = new Vector();
        if(elements.len gth < length) return; //Wrong length specification
        for(int i=0;i<length;i+ +)
        l_temp.add(new Interger(elemen ts[i]));
        Vector l_temp1 = new Vector();
        l_temp1.add(new Integer(remove_ intem));
        l_temp.removeAl l(l_temp1);
        }

        Thanks ............... ........
        I am waiting for ur reply....

        Comment

        • dmjpro
          Top Contributor
          • Jan 2007
          • 2476

          #5
          a llittle bit of change here ..........
          void int [] removeAll(int [] elements,int length,int remove_item)
          {
          Vector l_temp = new Vector();
          if(elements.len gth < length) return; //Wrong length specification
          for(int i=0;i<length;i+ +)
          l_temp.add(new Interger(elemen ts[i]));
          Vector l_temp1 = new Vector();
          l_temp1.add(new Integer(remove_ intem));
          l_temp.removeAl l(l_temp1);
          int l_temp2 = new int[l_temp.size()]
          for(int i=0;i<l_temp.si ze();i++)
          l_temp2[i] = l_temp.elementA t(i);
          return l_temp2;
          }

          Comment

          • r035198x
            MVP
            • Sep 2006
            • 13225

            #6
            Originally posted by dmjpro
            a llittle bit of change here ..........
            void int [] removeAll(int [] elements,int length,int remove_item)
            {
            Vector l_temp = new Vector();
            if(elements.len gth < length) return; //Wrong length specification
            for(int i=0;i<length;i+ +)
            l_temp.add(new Interger(elemen ts[i]));
            Vector l_temp1 = new Vector();
            l_temp1.add(new Integer(remove_ intem));
            l_temp.removeAl l(l_temp1);
            int l_temp2 = new int[l_temp.size()]
            for(int i=0;i<l_temp.si ze();i++)
            l_temp2[i] = l_temp.elementA t(i);
            return l_temp2;
            }
            The idea is not to use the vector class' removeAll method but to write one's own version of removeAll like this:


            Code:
             
            public static int[] removeAll(int[] numbers, int length, int removeItem) {
              Integer[] items = new Integer[numbers.length];
              int removed = 0;
              for(int i = 0; i < numbers.length;i++) {
               if(numbers[i] == removeItem) {
            	items[i] = null;
            	removed++;
               }
               else {
            	items[i] = numbers[i];
               }
              }
              numbers = new int[numbers.length - removed];
              int index = 0;
              for(int i = 0; i < items.length;i++) {
               if(items[i] == null) {
               }
               else {
            	numbers[index++] = items[i];
               }
              }
              return numbers;
             }

            Comment

            • TexasNewbie
              New Member
              • Dec 2006
              • 23

              #7
              This is the code that I came up with using what I got out of the instruction. It will not compile and honestly I have tried to change it every which way I can. I have looked at all your responses and have tried to impliment them into the program with no success. I just can't get it to compile


              import java.io.*;

              import java.util.*;

              public class Assignment5
              {
              static BufferedReader keyboard = new
              BufferedReader( new InputStreamRead er(System.in));

              public static void main(String[] args) throws IOException
              {
              int[] list = new int[20]; //start of array


              IntClass length;

              int item, index;

              StringTokenizer tokenizer;


              System.out.prin tln("Enter 12 integers in one line");

              tokenizer = new StringTokenizer (keyboard.readL ine());

              for(int i = 0; i < 12; i++)
              list[i] = Integer.parseIn t(tokenizer.nex tToken());

              length = new IntClass(12);

              print(list, length.getNum() );

              System.out.prin t("Enter item to be removed: "); //item to be removed
              System.out.flus h();

              item = Integer.parseIn t(keyboard.read Line());

              System.out.prin tln();

              System.out.prin t("Enter the position of the item to be removed.\n" + "(Position of the first element is 1): ");
              System.out.flus h();

              index = Integer.parseIn t(keyboard.read Line());

              System.out.prin tln();

              insertAt(list,l ength, item, index);

              print(list, length.getNum() );
              }


              public static void insertAt(int list[], IntClass len, int item, int index) //simular to what was suggested
              {
              if(index <= 0 || index > len.getNum())
              {
              System.out.prin tln("Position of the item to be removeded is out of range");
              if(index <= 0)
              System.out.prin tln("Position of the item to be removeded must be positive");
              else
              System.out.prin tln("The size of the list is " + list.length);
              }
              else
              {
              if(index > len.getNum())
              {
              System.out.prin tln("Item will be removed from the list "
              + "at position " + (len.getNum() + 1));
              list[len.getNum()] = item;
              len.setNum(len. getNum() + 1);
              }
              else
              {
              for(int i = len.getNum() - 1; i >= index - 1; i--)
              list[i + 1] = list[i];

              list[index - 1] = item;

              len.setNum(len. getNum() + 1);
              }
              }
              }

              public static void print(int[] list, int len)
              {
              for(int i = 0; i < len; i++)
              System.out.prin t(list[i] + " ");
              System.out.prin tln();
              }
              }

              Comment

              • TexasNewbie
                New Member
                • Dec 2006
                • 23

                #8
                after some munipulation I have an error on line 33 that states array diminsion missing

                line 33 print(list, length.getNum(1 2));

                Any clues

                Comment

                • r035198x
                  MVP
                  • Sep 2006
                  • 13225

                  #9
                  Originally posted by TexasNewbie
                  This is the code that I came up with using what I got out of the instruction. It will not compile and honestly I have tried to change it every which way I can. I have looked at all your responses and have tried to impliment them into the program with no success. I just can't get it to compile


                  import java.io.*;

                  import java.util.*;

                  public class Assignment5
                  {
                  static BufferedReader keyboard = new
                  BufferedReader( new InputStreamRead er(System.in));

                  public static void main(String[] args) throws IOException
                  {
                  int[] list = new int[20]; //start of array


                  IntClass length;

                  int item, index;

                  StringTokenizer tokenizer;


                  System.out.prin tln("Enter 12 integers in one line");

                  tokenizer = new StringTokenizer (keyboard.readL ine());

                  for(int i = 0; i < 12; i++)
                  list[i] = Integer.parseIn t(tokenizer.nex tToken());

                  length = new IntClass(12);

                  print(list, length.getNum() );

                  System.out.prin t("Enter item to be removed: "); //item to be removed
                  System.out.flus h();

                  item = Integer.parseIn t(keyboard.read Line());

                  System.out.prin tln();

                  System.out.prin t("Enter the position of the item to be removed.\n" + "(Position of the first element is 1): ");
                  System.out.flus h();

                  index = Integer.parseIn t(keyboard.read Line());

                  System.out.prin tln();

                  insertAt(list,l ength, item, index);

                  print(list, length.getNum() );
                  }


                  public static void insertAt(int list[], IntClass len, int item, int index) //simular to what was suggested
                  {
                  if(index <= 0 || index > len.getNum())
                  {
                  System.out.prin tln("Position of the item to be removeded is out of range");
                  if(index <= 0)
                  System.out.prin tln("Position of the item to be removeded must be positive");
                  else
                  System.out.prin tln("The size of the list is " + list.length);
                  }
                  else
                  {
                  if(index > len.getNum())
                  {
                  System.out.prin tln("Item will be removed from the list "
                  + "at position " + (len.getNum() + 1));
                  list[len.getNum()] = item;
                  len.setNum(len. getNum() + 1);
                  }
                  else
                  {
                  for(int i = len.getNum() - 1; i >= index - 1; i--)
                  list[i + 1] = list[i];

                  list[index - 1] = item;

                  len.setNum(len. getNum() + 1);
                  }
                  }
                  }

                  public static void print(int[] list, int len)
                  {
                  for(int i = 0; i < len; i++)
                  System.out.prin t(list[i] + " ");
                  System.out.prin tln();
                  }
                  }
                  The method that I posted worked on my compiler. Here is how to test it

                  Code:
                   
                  import java.util.*;
                  public class Test {
                   public static void main(String... args) {
                    int[] a = {4,5,6,7, 5, 6, 4};
                    int[]b = removeAll(a, 7, 4);
                    System.out.println(Arrays.toString(b));
                   }
                   public static int[] removeAll(int[] numbers, int length, int removeItem) {
                    Integer[] items = new Integer[numbers.length];
                    int removed = 0;
                    for(int i = 0; i < numbers.length;i++) {
                     if(numbers[i] == removeItem) {
                  	items[i] = null;
                  	removed++;
                     }
                     else {
                  	items[i] = numbers[i];
                     }
                    }
                    numbers = new int[numbers.length - removed];
                    int index = 0;
                    for(int i = 0; i < items.length;i++) {
                     if(items[i] == null) {
                     }
                     else {
                  	numbers[index++] = items[i];
                     }
                    }
                    return numbers;
                   }
                  }
                  Try it and post any error messages that you are getting with it.

                  Comment

                  Working...