Hi, I'm having trouble getting age 10 occurrences and then removing age 10 from the list. Then I want to be able to print the list without displaying age 10 in it. I trying to use my bag class implementation to do this successfully. My bag class is below. Any help is appreciated.
Current Output:
Original List of Ages:
5
3
2
10
Age 10 occurs 0
Age 10 is Removed From List
5
3
2
10
Expected Output:
Original List of Ages:
5
3
2
10
2
10
Age 10 occurs 2
Age 10 is Removed From List
5
3
2
2
Program:
import java.util.Scanner;
import edu.colorado.collections.IntArrayBag;
import java.io.* ;
public class BagDemonstration
{
private static Scanner stdin = new Scanner(System.in);
public static void main(String[ ] args) throws IOException
{
IntArrayBag ages = new IntArrayBag( );
getAges(ages);
}
public static void getAges(IntArrayBag ages) throws IOException
{
int userInput;
int ageArr[]=new int[100];
int zcount = 0;
int j = 0;
do
{
System.out.println("Type the ages of your family members: ");
userInput = stdin.nextInt( );
ages.add(userInput);
if (userInput != -1)
{
ageArr[j++] = userInput;
}
} while (userInput != -1);
System.out.println("Original List of Ages:");
for(int p=0;p<j;p++)
{
System.out.println(ageArr[p]);
}
System.out.println("Age 10 occurs ");
System.out.println("Age 10 is Removed From List");
for (int p=0; p<j; ++p)
{
if (ages.countOccurrences(userInput) == 10)
{
zcount++;
System.out.println(zcount);
ages.remove(userInput);
}
System.out.println(ageArr[p]);
}
}
}
Bag Class:
package edu.colorado.collections;
public class IntArrayBag implements Cloneable
{
private int[ ] data;
private int manyItems;
public IntArrayBag( )
{
final int INITIAL_CAPACITY = 10;
manyItems = 0;
data = new int[INITIAL_CAPACITY];
}
public IntArrayBag(int initialCapacity)
{
if (initialCapacity < 0)
throw new IllegalArgumentException
("The initialCapacity is negative: " + initialCapacity);
data = new int[initialCapacity];
manyItems = 0;
}
public void add(int element)
{
if (manyItems == data.length)
{ // Ensure twice as much space as we need.
ensureCapacity((manyItems + 1)*2);
}
data[manyItems] = element;
manyItems++;
}
public void addMany(int... elements)
{
if (manyItems + elements.length > data.length)
{ // Ensure twice as much space as we need.
ensureCapacity((manyItems + elements.length)*2);
}
System.arraycopy(elements, 0, data, manyItems, elements.length);
manyItems += elements.length;
}
public void addAll(IntArrayBag addend)
{
// If addend is null, then a NullPointerException is thrown.
// In the case that the total number of items is beyond
// Integer.MAX_VALUE, there will be an arithmetic overflow and
// the bag will fail.
ensureCapacity(manyItems + addend.manyItems);
System.arraycopy(addend.data, 0, data, manyItems, addend.manyItems);
manyItems += addend.manyItems;
}
public IntArrayBag clone( )
{
IntArrayBag answer;
try
{
answer = (IntArrayBag) super.clone( );
}
catch (CloneNotSupportedException e)
{
throw new RuntimeException
("This class does not implement Cloneable");
}
answer.data = data.clone( );
return answer;
}
public int countOccurrences(int target)
{
int answer;
int index;
answer = 0;
for (index = 0; index < manyItems; index++)
if (target == data[index])
answer++;
return answer;
}
public void ensureCapacity(int minimumCapacity)
{
int[ ] biggerArray;
if (data.length < minimumCapacity)
{
biggerArray = new int[minimumCapacity];
System.arraycopy(data, 0, biggerArray, 0, manyItems);
data = biggerArray;
}
}
public int getCapacity( )
{
return data.length;
}
public boolean remove(int target)
{
int index;
for (index = 0; (index < manyItems) && (target != data[index]); index++)
;
if (index == manyItems)
return false;
else
{
manyItems--;
data[index] = data[manyItems];
return true;
}
}
public int size( )
{
return manyItems;
}
public void trimToSize( )
{
int[ ] trimmedArray;
if (data.length != manyItems)
{
trimmedArray = new int[manyItems];
System.arraycopy(data, 0, trimmedArray, 0, manyItems);
data = trimmedArray;
}
}
public static IntArrayBag union(IntArrayBag b1, IntArrayBag b2)
{
IntArrayBag answer = new IntArrayBag(b1.getCapacity( ) + b2.getCapacity( ));
System.arraycopy(b1.data, 0, answer.data, 0, b1.manyItems);
System.arraycopy(b2.data, 0, answer.data, b1.manyItems, b2.manyItems);
answer.manyItems = b1.manyItems + b2.manyItems;
return answer;
}
}