Lab 6d Adding To and Removing From an Integer List

advertisement
Lab 6d: Adding To and Removing From an Integer List
File [IntegerList6d.java] contains a Java class representing a list of integers. The following public methods are provided:


IntegerList(int size)- Creates a new list of size elements. Elements are initialized to 0.
void print()- Prints the array elements and indices.
File [IntegerListTest6d.java] contains a Java program that provides menu-driven testing for the IntegerList6d class. Copy both
files to your directory, and compile and run IntegerListTest6d to see how it works.
It is often necessary to add items to or remove items from a list. When the list is stored in an array, one way to do this is to
create a new array of the appropriate size each time the number of elements changes, and copy the values over from the old
array. However, this is rather inefficient. A more common strategy is to choose an initial size for the array and add elements
until it is full, then double its size and continue adding elements until it is full, and so on. (It is also possible to decrease the size
of the array if it falls under, say, half full, but we won't do that in this exercise.) The CDCollection class in Listing 7.8 of the text
uses this strategy—it keeps track of the current size of the array and the number of elements already stored in it, and method
addCD() calls increaseSize() if the array is full. Study that example.
1.
Add iNumValues variable – Add a class variable to the IntegerList6d class to hold the current number of integers in the
list.
2.
Add increaseSize() method - Create a method private void increaseSize() to the IntegerList6d class.
The method will:
a. Hold onto the existing array
b. Create a new array that is twice the size of the old array
c. Read the old array values back into the new array.
3.
Add addElement( int iNewVal ) method – Create a method public void addElement( int iNewVal
) to the IntegerList6d class. The purpose of the method is to add a new value to the end of the list. The method will:
a. Check to see if the array is full
i. If the array is full, call the increaseSize() method to double the size of the array before continuing.
b. Add the new value as the next value in the array.
c. Increment iNumValues.
4.
Add removeFirst( int iVal ) method – Create a method public void removeFirst( int iVal ) to
the IntegerList6d class. The purpose of the method is to remove the first instance of the value from the array. The
method will:
a. Search the array for the first instance of iVal in the array.
i. If it DOES NOT find the value, it will finish without doing anything (No Error)
ii. If it DOES find the value, it will
1. Remove it from the list
2. Shift the remaining values (if there are any) in the array down
3. Decrement iNumValues.
5.
Add removeAll( int iVal ) method – Create a method private void removeAll( int iVal ) to the
IntegerList6d class. The purpose of the method is to remove ALL instances of the value from the array.
a. HINT: Use the techniques you used in the previous method. There are multiple ways you can approach this.
IntegerList6d class
//
//
//
//
//
//
//
//
****************************************************************
IntegerList6d.java [Lab 6d]
Student Name: <name>
Define an IntegerList class with methods to create & fill
a list of integers.
****************************************************************
public class IntegerList6d
Page 1 of 5
One Dimensional Arrays
{
private int[] iArrList;
// ---------------------------------------------// TODO #1: Create variable iNumValues to hold number
// of integers in the array
// ------------------------------------------------------// Constructor: create a list of the given size
// ------------------------------------------------------public IntegerList6d( int size )
{
iArrList = new int[size];
}
// ---------------------------------------------// TODO #3: Create public method addElement( int iNewVal )
// 3a) Check to see if array is full
// 3ai) if it is full, call increaseSize()
// 3b) Add value to end of array
// 3c) Increment iNumValues
public void addElement( int iNewVal )
{
}
// ------------------------------------------------------// print array elements with indices
// ------------------------------------------------------public void print()
{
for ( int i = 0; i < iNumValues; i++ )
{
System.out.print( "[" + i + "] = *" + iArrList[i] + "* " );
if ( ( i + 1 ) % 5 == 0 )
{
System.out.println();
}
}
System.out.println();
}
// ---------------------------------------------// TODO #5: Create public method removeAll( int iVal )
// Remove all values of
public void removeAll( int iVal )
{
}
// ---------------------------------------------// TODO #4: Create public method removeFirst( int iVal )
// 4a) Search array for the value
// 4a.i) If it can't find it, don't do anything and return early
// 4a.ii) If it finds the value...
// 4a.ii.1) Remove it from list
// 4a.ii.2) Shift the remaining values
// 4a.ii.3) Decrement iNumValues
public void removeFirst( int iVal )
{
}
// ---------------------------------------------// TODO #2: Create private method increaseSize()
// 2a) Hold onto existing array iArrList
// 2b) Create a new array that is twice the size and assign to iArrList
// 2c) Copy values into new array
private void increaseSize()
{
}
}
Page 2 of 5
One Dimensional Arrays
Tester class
// ****************************************************************
// IntegerListTest6d.java [Lab 6d]
//
Student Name: <name>
//
// Provide a menu-driven tester for the IntegerList6d class.
//
// ****************************************************************
import java.util.Scanner;
public class IntegerListTest6d
{
static IntegerList6d iList6dHold = new IntegerList6d( 10 );
static Scanner scan = new Scanner( System.in );
public static int MAX_LIST = 5;
// -------------------------------------// Do what the menu item calls for
// -------------------------------------public static void dispatch( int choice )
{
int iVal;
switch ( choice )
{
case 0:
System.out.println( "Bye!" );
break;
case 1:
iList6dHold = new IntegerList6d( MAX_LIST );
System.out.println( "Created a new list starting with [" + MAX_LIST + "] values" );
break;
case 2:
System.out.println( "How many values need to be entered in?" );
iVal = scan.nextInt();
System.out.println( "Please enter in the new values: " );
for ( int i = 0; i < iVal; i++ )
{
iList6dHold.addElement( scan.nextInt() );
}
scan.nextLine();
break;
case 3:
System.out.println( "What value needs to be removed [removeFirst()]" );
iVal = scan.nextInt();
iList6dHold.removeFirst( iVal );
break;
case 4:
System.out.println( "What value needs to be removed [removeAll()]" );
iVal = scan.nextInt();
iList6dHold.removeAll( iVal );
break;
case 5:
iList6dHold.print();
break;
case 10:
printMenu();
break;
default:
System.out.println( "Sorry, invalid choice" );
}
}
// ------------------------------------------------------// Create a list, then repeatedly print the menu and do what the
// user asks until they quit
// ------------------------------------------------------public static void main( String[] args )
{
printMenu();
System.out.print( "\nEnter your choice [NOTE: 0 = quit, 10 = Show full menu, or other]: " );
int choice = scan.nextInt();
Page 3 of 5
One Dimensional Arrays
while ( choice != 0 )
{
dispatch( choice );
System.out.print( "\nEnter your choice [NOTE: 0 = quit, 10 = Show full menu, or other]: " );
choice = scan.nextInt();
}
}
// ---------------------------// Print the user's choices
// ---------------------------public static void printMenu()
{
System.out.println( "\n
Menu
" );
System.out.println( "
====" );
System.out.println( "0: Quit" );
System.out.println( "1: Create a new list (** do this first!! **)" );
System.out.println( "2: Add new values" );
System.out.println( "3: Remove the first value" );
System.out.println( "4: Remove all values" );
System.out.println( "5: Print the list" );
System.out.println( "10: Show full menu" );
}
}
Sample Output #1
Menu
====
0: Quit
1: Create a new list (** do this first!! **)
2: Add new values
3: Remove the first value
4: Remove all values
5: Print the list
10: Show full menu
Enter your choice [NOTE: 0 = quit, 10 = Show full menu, or other]: 1
Created a new list starting with [5] values
Enter your choice [NOTE: 0 = quit, 10 = Show full menu, or other]: 2
How many values need to be entered in?
7
Please enter in the new values:
1 2 3 4 5 4 6
Enter your choice [NOTE: 0 = quit, 10 = Show full menu, or other]: 5
[0] = *1* [1] = *2* [2] = *3* [3] = *4* [4] = *5*
[5] = *4* [6] = *6*
Enter your choice [NOTE: 0 = quit, 10 = Show full menu, or other]: 2
How many values need to be entered in?
3
Please enter in the new values:
7 7 7
Enter your choice [NOTE: 0 = quit, 10 = Show full menu, or other]: 5
[0] = *1* [1] = *2* [2] = *3* [3] = *4* [4] = *5*
[5] = *4* [6] = *6* [7] = *7* [8] = *7* [9] = *7*
Enter your choice [NOTE: 0 = quit, 10 = Show full menu, or other]: 3
What value needs to be removed [removeFirst()]
7
Enter your choice [NOTE: 0 = quit, 10 = Show full menu, or other]: 5
[0] = *1* [1] = *2* [2] = *3* [3] = *4* [4] = *5*
[5] = *4* [6] = *6* [7] = *7* [8] = *7*
Enter your choice [NOTE: 0 = quit, 10 = Show full menu, or other]: 4
What value needs to be removed [removeAll()]
4
Page 4 of 5
One Dimensional Arrays
Enter your choice [NOTE: 0 = quit, 10 = Show full menu, or other]: 5
[0] = *1* [1] = *2* [2] = *3* [3] = *5* [4] = *6*
[5] = *7* [6] = *7*
Enter your choice [NOTE: 0 = quit, 10 = Show full menu, or other]: 4
What value needs to be removed [removeAll()]
7
Enter your choice [NOTE: 0 = quit, 10 = Show full menu, or other]: 5
[0] = *1* [1] = *2* [2] = *3* [3] = *5* [4] = *6*
Enter your choice [NOTE: 0 = quit, 10 = Show full menu, or other]: 0
Page 5 of 5
One Dimensional Arrays
Download