Create a program called IntegerBag and copy the code from the IntegerSet into it
ID: 3890694 • Letter: C
Question
Create a program called IntegerBag and copy the code from the IntegerSet into it (but keep the IntegerSet).
Modify the add() method to allow duplicates (we're taking it back to the way it was before we made the IntegerSet.
Create a remove() method that accepts an int as its single parameter and returns a boolean value to indicate the success or failure of the method. This method should delete the first instance of the Integer object that it finds in the IntegerBag.containing the int specified. The method should return true if the deletion was successful and falseotherwise (if the int was not found). You can use one of the remove() methods of the ArrayList class for this.
Create a removeAll() method that accepts an int as its single parameter and returns a boolean value to indicate the success or failure of the method. This method should delete all Integer objects containing the int that it finds in the IntegerBag. The method should return true if the deletion was successful and false otherwise (if nothing was deleted).
create a multiplicity( int value ) method that returns the number of instances for the intObj that was sent via the parameter as an int. So, if there are three instances for the number 5, the method should return 3.
create a countDistinct() method that accepts no parameters and returns the number of unique items in the IntegerBag as an int
creates a distinctItems() method that accepts no parameters and returns an IntegerSetcontaining the unique items in the IntegerBag.
Modify the intersection(), difference(), and subset() methods to handle IntegerBags. The union() method should work as is.
****************************************************************************************
Explanation / Answer
Given below is the modified code. Please use this new class along with IntegerSet. It makes use of IntegerSet as well. Hope it helps. If any issues , please post a comment, I will fix them. If it works well, please don't forget to rate the answer. Thank you.
import java.util.ArrayList;
public class IntegerBag {
// ---------------------------------------------------------------------
// Declarations
// ---------------------------------------------------------------------
private ArrayList< Integer > model;
/**
* constructor - init model object.
*/
public IntegerBag() {
model = new ArrayList<>();
} // constructor model
/**************************** public Methods *************************/
/**
* add - add an int to the collection (convert to Integer).
*
* allow duplicates
*
* @param newInt the int to add
*/
public void add( int newInt ) {
// 1. Test to see if list is empty or if the new item should be at
// the end. Ensure it is not there first
if ( size() == 0 || isLast( newInt ) ) {
append( newInt );
} else {
insert( newInt );
}
} // method add
/**
* get - return a single ArrayList element.
*
* @param index - the index of the item to get
* @return Integer
*/
public int get( int index ) {
int valToReturn = -1;
if ( index >= 0 && index < size() ) {
valToReturn = model.get( index ).intValue();
}
return valToReturn;
} // method get
/**
* intersection - return the intersection of 2 bags.
*
*
*
* @param other - the incoming bag
* @return the intersection of 2 bags
*/
public IntegerBag intersection( IntegerBag other ) {
IntegerBag result = new IntegerBag();
if ( other != null ) {
for ( int i = 0; i < other.size(); i++ ) {
//find the number of common occurences of the current item
if(!result.model.contains(other.get(i)))
{
int count1 = other.multiplicity(other.get(i));
int count2 = multiplicity(other.get(i));
int min = count1 < count2 ? count1 : count2;
for(int j = 0; j < min; j++)
result.add(other.get(i));
}
}
}
return result;
} // method intersection
public IntegerBag difference( IntegerBag other ) {
IntegerBag result = new IntegerBag();
if ( other != null ) {
for ( int i = 0; i < model.size(); i++ ) {
if(!result.model.contains(other.get(i)))
{
//find the difference in counts
int count1 = multiplicity(model.get(i));
int count2 = other.multiplicity(model.get(i));
int diff = count1 - count2;
for(int j = 0; j < diff; j++)
result.add(model.get(i));
}
}
}
return result;
} // method difference
public IntegerBag union( IntegerBag other ) {
IntegerBag result = new IntegerBag();
if ( other != null ) {
for ( int i = 0; i < model.size(); i++ ) {
if(!result.model.contains(other.get(i)))
{
//find the difference in counts
int count1 = multiplicity(model.get(i));
int count2 = other.multiplicity(model.get(i));
int sum = count1 + count2;
for(int j = 0; j < sum; j++)
result.add(model.get(i));
}
}
}
return result;
} // method union
/**
* size - return the size of the ArrayList.
*
* @return int
*/
public int size() {
return model.size();
} // method size
/**
* subset - returns true if incoming is a subset of the current bag.
*
* **MLN 9/19/2016 - new for HW08
*
* @param other - the set to test
* @return true if the incoming set is a subset of the current set
*/
public boolean subset( IntegerBag other ) {
boolean isSubset = true;
if ( other != null && other.size() > 0 ) {
for ( int i = 0; i < other.size() && isSubset; i++ ) {
if ( multiplicity(other.get( i )) < other.multiplicity(other.get(i))) {
isSubset = false;
}
}
}
return isSubset;
}
/******************************* private methods **********************/
/**
* append - add to the end of the list.
*
* @param newInt the int to append
*/
private void append( int newInt ) {
model.add( new Integer( newInt ) );
} // method append
/**
* insert - insert an Integer into the list at its proper position.
*
* @param newInt the int to insert
*/
private void insert( int newInt ) {
if ( newInt < model.get( model.size() - 1 ).intValue() ) {
int index = 0;
// locate the insert point
while ( newInt > ( model.get( index ) ).intValue() ) {
index++;
}
model.add( index, new Integer( newInt ) ); // insert Integer at
// index
} else {
append( newInt );
}
} // method insert
/**
* isLast - return true if the object is bigger than the last object in the
* list.
*
* @param comparableInt - an int to compare
* @return true if this is greater than the last value
*/
private boolean isLast( int comparableInt ) {
boolean isLast = true;
if ( model.size() > 0 ) {
isLast = comparableInt >= model.get( model.size() - 1 ).intValue();
}
return isLast;
} // method isLast
public boolean remove(int n)
{
for ( int i = 0; i < model.size(); i++ ) {
if(model.get(i) == n)
{
model.remove(i);
return true;
}
}
return false;
}
public boolean removeAll(int n)
{
boolean removed = false;
for ( int i = 0; i < model.size();) {
if(model.get(i) == n)
{
model.remove(i);
removed = true;
}
else
i++;
}
return removed;
}
public int multiplicity(int n)
{
int count = 0;
for ( int i = 0; i < model.size(); i++) {
if(model.get(i) == n)
count++;
}
return count;
}
public int countDistinct()
{
IntegerSet distinct = distinctItems();
return distinct.size();
}
public IntegerSet distinctItems()
{
IntegerSet set = new IntegerSet(); //make a set
for ( int i = 0; i < model.size(); i++)
set.add(model.get(i));
return set;
}
} // class IntegerBag
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.