Lab 2 Prepare 1 Create a folder named
Lab 2
Prepare: 1. Create a folder named lab 2 inside the workspace. 2. Download Record. DB. java and Record. java from the lab document. 3. Change the file name Record. DB. java to Dynamic. Record. DB. java. 4. Launch Eclipse and create a new project named lab 2. 5. Open Dynamic. Record. DB. java. 6. Subtitute "Record. DB" in Dynamic. Record. DB. java by "Dynamic. Record. DB" everywhere.
1. Change the type of max. Records from static int to int. Change the default value of max. Records from 200 to 1. 2. Change the declaration of record. List. private static int max. Records = 200; private int num. Records = 0; private Record record. List[] = new Record[max. Records]; private int max. Records = 1; private int num. Records = 0; private Record record. List[];
3. Add two constructors: Dynamic. Record. DB() and Dynamic. Record. DB( int n ). // The default constructor; allocates an array of size 1. Dynamic. Record. DB( ) { record. List = new Record[ max. Records ]; } // Allocates an array of size n. // If the user knows the maximum size of the employee // database, they might prefer to use this constructor Dynamic. Record. DB( int n ) { //update max. Records to n max. Records = n; //Create an array of size=max. Records. record. List = new Record[ max. Records ]; }
4. Modify insert method to double the size of record. List when overflow may occur. old version public void insert ( Record rec ) { // Check for overflow if ( num. Records == max. Records – 1 ) { return; // Increase the size of the array maybe } …… Do nothing and return if overflow may occur.
public void insert (Record rec) { // Double the size of the array, if overflow may occur. if (num. Records == max. Records-1) { 1 //create of a new array of the double size Record[ ] temp. Record = new Record[ 2 * max. Records ]; 2 // Copy the contents of record. List to temp. Record for( int i = 0; i < max. Records; i++) temp. Record[ i ] = record. List[ i ]; 3 // Make record. List point to temp. Record record. List = temp. Record; 4 //Change max. Records to the new value max. Records = 2 * max. Records; 5 //return; } …….
Time of insertion operations
Average time of each insertion operation
- Slides: 8