Question: public class ArrayListLab { /** * Main method. * * @param args command line arguments * @throws IOException the IOException */ public static void main(
public class ArrayListLab { /** * Main method. * * @param args command line arguments * @throws IOException the IOException */ public static void main( String[] args ) throws IOException { ArrayListControl ac = new ArrayListControl(); ac.run(); } // method main } // class ArrayListLab **************************************************
public class ArrayListModel { // --------------------------------------------------------------------- // Declarations // --------------------------------------------------------------------- private ArrayList< Integer > model; /** * constructor - init model object. */ public ArrayListModel() { model = new ArrayList<>(); } // constructor model /**************************** public Methods *************************/ /** * add - add an int to the collection (convert to Integer). * * @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. 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 the int from the object at the index given */ public int get( int index ) { int valToReturn = -1; if ( index >= 0 && index < size() ) { valToReturn = model.get( index ).intValue(); } return valToReturn; } // method get /** * size - return the size of the ArrayList. * * @return int */ public int size() { return model.size(); } // method size /******************************* private methods **********************/ /** * append - add to the end of the list. * * @param newInt (the object 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 object to insert) */ private void insert( int newInt ) { if ( newInt < model.get( model.size() - 1 ).intValue() ) { int index = 0; 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 boolean */ 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 ArrayListModel
*************************************************
public class ArrayListView { // --------------------------------------------------------------------- // Declarations // --------------------------------------------------------------------- private BufferedReader reader; // the reader object /** * constructor ArrayListView. * * @throws IOException the IOException */ public ArrayListView() throws IOException { reader = new BufferedReader( new InputStreamReader( System.in ) ); } /**************************** public Methods *************************/ /** * displayToScreen - display the content of the parameter to the screen. * * @param s the String to display */ public void display( String s ) { System.out.print( s ); } // method display /** * getInput - get user input. * * @return the String entered * @throws IOException the IOException */ public String getInput() throws IOException { return reader.readLine(); } // method getInput } // class ArrayListView
******************************************************
public class ArrayListControl { // --------------------------------------------------------------------- // Declarations // --------------------------------------------------------------------- private ArrayListModel model; // the model object private ArrayListView ui; // the UserInterface (view) object /** * constructor - init model & view objects and begin program. * * @throws IOException the IOException */ public ArrayListControl() throws IOException { model = new ArrayListModel(); ui = new ArrayListView(); } // constructor /***************************** public methods **************************/ /** * run - run the program. * * @throws IOException the IOException */ public void run() throws IOException { displayHeader(); enterIntegers(); displayList(); } // run /*************************** private methods **************************/ /** * displayHeader - show greeting. */ private void displayHeader() { ui.display( "***************************************************** " ); ui.display( "* ArrayList Lab * " ); ui.display( "***************************************************** " ); } // method displayHeader /** * displayList - Display the list and print the number of used slots. */ private void displayList() { ui.display( " " ); // print blank line if ( model.size() > 0 ) { for ( int i = 0; i < model.size(); i++ ) { ui.display( model.get( i ) + " " ); } // end for } ui.display( " " ); // add to end of prior with extra line // print stats at end ui.display( "Used slots = " + model.size() + " " ); } // method displayList /** * enterIntegers - enter integers and add to the ArrayListModel. * * @throws IOException the IOException */ private void enterIntegers() throws IOException { int newInt = Integer.MAX_VALUE; // the integer object to add String userString = ""; // the value entered by the user do { ui.display( "Please enter a valid int value. Press 'Q' " + "to quit: " ); userString = ui.getInput(); // if the user did not enter q if ( !isQuit( userString ) ) { if ( isValidInt( userString ) ) { newInt = Integer.parseInt( userString ); model.add( newInt ); } // end if } // end if } while ( !isQuit( userString ) ); // continue as long as the // user doesn't quit } // method enterIntegers /** * isQuit - return true if the user entered 'q'. * * @param s the String to test * @return true if the user entered 'q' */ private boolean isQuit( String s ) { return s.length() == 1 && s.toLowerCase().charAt( 0 ) == 'q'; } // method isQuit /** * isValidInt - return true if the user has entered valid int. * * @param s the String to test * @return true if the int is valid */ private boolean isValidInt( String s ) { boolean isValid = false; // initialize return value to false try { Integer.parseInt( s ); // not assigning here, just testing isValid = true; // valid int, so set to true } catch ( NumberFormatException e ) { System.out.println( "Invalid Entry. Please try again." ); } // end catch return isValid; } // method isValidInt } // class ArrayListControl
*****************************************************
In this version, we will rename the ArrayListModel to IntegerSet. (Do NOT use the Java Set class). This class will behave as a set. Much of what we did for the earlier ArrayListModel can be carried forward. One change in particular will be important: the add() method must ensure uniqueness. That is, there can be only one copy of any particular Integer object in the set. Thus you must check to make sure that the object you are adding is not already there.
In addition, we want to implement two new "set" methods: one for intersection and the other to determine if one set is a subset of the other.
The new intersection() method will take an IntegerSet object as a parameter and will return an IntegerSet as a result. The method will locate all Integer objects that are common to both sets (the current set and the incoming set) and write those to a third IntegerSet that will be returned by the method.
public IntegerSet intersection( IntegerSet incoming )
The subset() method will accept an IntegerSet as a parameter and will return a boolean value that indicates whether or not the incoming set is a subset of the current set. In other words, if every element of the incoming IntegerSet is also contained in the current IntegerSet, then the method will return true. Otherwise it will return false.
public boolean subset( IntegerSet incoming )
You will need also to modify the ArrayListControl to test your new methods. This requires that you create two IntegerSet objects to successfully test your program.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
