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
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.
****************************************************************************************
public class IntegerSet { // --------------------------------------------------------------------- // Declarations // --------------------------------------------------------------------- private ArrayList< Integer > model; /** * constructor - init model object. */ public IntegerSet() { model = new ArrayList<>(); } // constructor model /**************************** public Methods *************************/ /** * add - add an int to the collection (convert to Integer). * * **MLN 9/19/2016 - added uniqueness condition (using contains() ) * * @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 ( !model.contains( new Integer( newInt ) ) ) { 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 sets. * * **MLN 9/19/2016 - new for HW08 * * @param other - the incoming set * @return the intersection of 2 sets */ public IntegerSet intersection( IntegerSet other ) { IntegerSet result = new IntegerSet(); if ( other != null ) { for ( int i = 0; i < other.size(); i++ ) { if ( model.contains( other.get( i ) ) ) { result.add( other.get( i ) ); } } } return result; } // method intersection /** * 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 set. * * **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( IntegerSet other ) { boolean isSubset = true; if ( other != null && other.size() > 0 ) { for ( int i = 0; i < other.size() && isSubset; i++ ) { if ( !model.contains( 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 } // class IntegerSet Step by Step Solution
There are 3 Steps involved in it
To accomplish the requirements you need to create the IntegerBag class by modifying the IntegerSet class Heres how you can approach this task step by step Step 1 Create the IntegerBag class Copy the I... View full answer
Get step-by-step solutions from verified subject matter experts
