Download presentation
Presentation is loading. Please wait.
Published byBrice Martin Modified over 8 years ago
1
Lab 2
2
Prepare: 1.Create a folder named lab2 inside the workspace. 2.Download RecordDB.java and Record.java from the lab document. 3.Change the file name RecordDB.java to DynamicRecordDB.java. 4.Launch Eclipse and create a new project named lab2. 5.Open DynamicRecordDB.java. 6. Subtitute "RecordDB" in DynamicRecordDB.java by "DynamicRecordDB" everywhere.
3
1. Change the type of maxRecords from static int to int. Change the default value of maxRecords from 200 to 1. 2. Change the declaration of recordList. private static int maxRecords = 200; private int numRecords = 0; private Record recordList[] = new Record[maxRecords]; private int maxRecords = 1; private int numRecords = 0; private Record recordList[];
4
3. Add two constructors: DynamicRecordDB() and DynamicRecordDB( int n ). // The default constructor; allocates an array of size 1. DynamicRecordDB( ) { recordList = new Record[ maxRecords ]; } // Allocates an array of size n. // If the user knows the maximum size of the employee // database, they might prefer to use this constructor DynamicRecordDB( int n ) { //update maxRecords to n maxRecords = n; //Create an array of size=maxRecords. recordList = new Record[ maxRecords ]; }
5
4. Modify insert method to double the size of recordList when overflow may occur. public void insert ( Record rec ) { // Check for overflow if ( numRecords == maxRecords – 1 ) { return;// Increase the size of the array maybe } …… Do nothing and return if overflow may occur. old version
6
public void insert (Record rec) { // Double the size of the array, if overflow may occur. if (numRecords == maxRecords-1) { //create of a new array of the double size Record[ ] tempRecord = new Record[ 2 * maxRecords ]; // Copy the contents of recordList to tempRecord for( int i = 0; i < maxRecords; i++) tempRecord[ i ] = recordList[ i ]; // Make recordList point to tempRecord recordList = tempRecord; //Change maxRecords to the new value maxRecords = 2 * maxRecords; //return; } ……. 1 2 3 4 5
7
Time of insertion operations
8
Average time of each insertion operation
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.