473,320 Members | 1,961 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,320 software developers and data experts.

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?
Feb 1 '07 #1
8 1924
r035198x
13,262 8TB
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

Expand|Select|Wrap|Line Numbers
  1.  
  2. public int[] removeAll(int[] numbers, int length, int removeItem) {
  3.   Integer[] items = new Integer[numbers.length];
  4.   for(int i = 0; i < numbers.length;i++) {
  5.    items[i] = numbers[i];
  6.   }
  7.  }
  8.  
Do you understand any of this?
Feb 1 '07 #2
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?
Feb 1 '07 #3
dmjpro
2,476 2GB
try this code .............
void removeAll(int [] elements,int length,int remove_item)
{
Vector l_temp = new Vector();
if(elements.length < length) return; //Wrong length specification
for(int i=0;i<length;i++)
l_temp.add(new Interger(elements[i]));
Vector l_temp1 = new Vector();
l_temp1.add(new Integer(remove_intem));
l_temp.removeAll(l_temp1);
}

Thanks .......................
I am waiting for ur reply....
Feb 1 '07 #4
dmjpro
2,476 2GB
a llittle bit of change here ..........
void int [] removeAll(int [] elements,int length,int remove_item)
{
Vector l_temp = new Vector();
if(elements.length < length) return; //Wrong length specification
for(int i=0;i<length;i++)
l_temp.add(new Interger(elements[i]));
Vector l_temp1 = new Vector();
l_temp1.add(new Integer(remove_intem));
l_temp.removeAll(l_temp1);
int l_temp2 = new int[l_temp.size()]
for(int i=0;i<l_temp.size();i++)
l_temp2[i] = l_temp.elementAt(i);
return l_temp2;
}
Feb 1 '07 #5
r035198x
13,262 8TB
a llittle bit of change here ..........
void int [] removeAll(int [] elements,int length,int remove_item)
{
Vector l_temp = new Vector();
if(elements.length < length) return; //Wrong length specification
for(int i=0;i<length;i++)
l_temp.add(new Interger(elements[i]));
Vector l_temp1 = new Vector();
l_temp1.add(new Integer(remove_intem));
l_temp.removeAll(l_temp1);
int l_temp2 = new int[l_temp.size()]
for(int i=0;i<l_temp.size();i++)
l_temp2[i] = l_temp.elementAt(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:


Expand|Select|Wrap|Line Numbers
  1.  
  2. public static int[] removeAll(int[] numbers, int length, int removeItem) {
  3.   Integer[] items = new Integer[numbers.length];
  4.   int removed = 0;
  5.   for(int i = 0; i < numbers.length;i++) {
  6.    if(numbers[i] == removeItem) {
  7.     items[i] = null;
  8.     removed++;
  9.    }
  10.    else {
  11.     items[i] = numbers[i];
  12.    }
  13.   }
  14.   numbers = new int[numbers.length - removed];
  15.   int index = 0;
  16.   for(int i = 0; i < items.length;i++) {
  17.    if(items[i] == null) {
  18.    }
  19.    else {
  20.     numbers[index++] = items[i];
  21.    }
  22.   }
  23.   return numbers;
  24.  }   
Feb 1 '07 #6
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 InputStreamReader(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.println("Enter 12 integers in one line");

tokenizer = new StringTokenizer(keyboard.readLine());

for(int i = 0; i < 12; i++)
list[i] = Integer.parseInt(tokenizer.nextToken());

length = new IntClass(12);

print(list, length.getNum());

System.out.print("Enter item to be removed: "); //item to be removed
System.out.flush();

item = Integer.parseInt(keyboard.readLine());

System.out.println();

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

index = Integer.parseInt(keyboard.readLine());

System.out.println();

insertAt(list,length, 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.println("Position of the item to be removeded is out of range");
if(index <= 0)
System.out.println("Position of the item to be removeded must be positive");
else
System.out.println("The size of the list is " + list.length);
}
else
{
if(index > len.getNum())
{
System.out.println("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.print(list[i] + " ");
System.out.println();
}
}
Feb 1 '07 #7
after some munipulation I have an error on line 33 that states array diminsion missing

line 33 print(list, length.getNum(12));

Any clues
Feb 2 '07 #8
r035198x
13,262 8TB
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 InputStreamReader(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.println("Enter 12 integers in one line");

tokenizer = new StringTokenizer(keyboard.readLine());

for(int i = 0; i < 12; i++)
list[i] = Integer.parseInt(tokenizer.nextToken());

length = new IntClass(12);

print(list, length.getNum());

System.out.print("Enter item to be removed: "); //item to be removed
System.out.flush();

item = Integer.parseInt(keyboard.readLine());

System.out.println();

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

index = Integer.parseInt(keyboard.readLine());

System.out.println();

insertAt(list,length, 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.println("Position of the item to be removeded is out of range");
if(index <= 0)
System.out.println("Position of the item to be removeded must be positive");
else
System.out.println("The size of the list is " + list.length);
}
else
{
if(index > len.getNum())
{
System.out.println("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.print(list[i] + " ");
System.out.println();
}
}
The method that I posted worked on my compiler. Here is how to test it

Expand|Select|Wrap|Line Numbers
  1.  
  2. import java.util.*;
  3. public class Test {
  4.  public static void main(String... args) {
  5.   int[] a = {4,5,6,7, 5, 6, 4};
  6.   int[]b = removeAll(a, 7, 4);
  7.   System.out.println(Arrays.toString(b));
  8.  }
  9.  public static int[] removeAll(int[] numbers, int length, int removeItem) {
  10.   Integer[] items = new Integer[numbers.length];
  11.   int removed = 0;
  12.   for(int i = 0; i < numbers.length;i++) {
  13.    if(numbers[i] == removeItem) {
  14.     items[i] = null;
  15.     removed++;
  16.    }
  17.    else {
  18.     items[i] = numbers[i];
  19.    }
  20.   }
  21.   numbers = new int[numbers.length - removed];
  22.   int index = 0;
  23.   for(int i = 0; i < items.length;i++) {
  24.    if(items[i] == null) {
  25.    }
  26.    else {
  27.     numbers[index++] = items[i];
  28.    }
  29.   }
  30.   return numbers;
  31.  }
  32. }
Try it and post any error messages that you are getting with it.
Feb 2 '07 #9

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

Similar topics

23
by: Paul Rubin | last post by:
OK, I want to scan a file for lines matching a certain regexp. I'd like to use an assignment expression, like for line in file: if (g := re.match(pat, line)): croggle(g.group(1)) Since...
10
by: Andrew Koenig | last post by:
It has been pointed out to me that various C++ books disagree about the relative precedence of ?: and the assignment operators. In order to satisfy myself about the matter once and for all, I...
5
by: CoolPint | last post by:
It seems to me that I cannot assign objects of a class which has a constant data member since the data member cannot be changed once the constructor calls are completed. Is this the way it is meant...
16
by: Edward Diener | last post by:
Is there a way to override the default processing of the assignment operator for one's own __value types ? I realize I can program my own Assign method, and provide that for end-users of my class,...
9
by: Rick N. Backer | last post by:
I have an abstract base class that has char* members. Is an assignment operator necessary for this abstract base class? Why or why not? Thanks in advance. Ken Wilson Amer. Dlx. Tele,...
166
by: Graham | last post by:
This has to do with class variables and instances variables. Given the following: <code> class _class: var = 0 #rest of the class
6
by: Kennedy_f | last post by:
I did better in terms of score on this one than 291, but I found it much harder. Wordings of questions are difficult like the rest, but the DNS and CA scenarios were very tough to figure out. Take...
6
by: Neil Zanella | last post by:
Hello, I would like to know whether the following C fragment is legal in standard C and behaves as intended under conforming implementations... union foo { char c; double d; };
35
by: nagy | last post by:
I do the following. First create lists x,y,z. Then add an element to x using the augumented assignment operator. This causes all the other lists to be changed also. But if I use the assignment...
20
by: TimeHorse | last post by:
I would like to gauge interest in the following proposal: Problem: Assignment statements cannot be used as expressions. Performing a list of mutually exclusive checks that require data...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....

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.