SHARE
SPREAD
HELP

The Tradition of Sharing

Help your friends and juniors by posting answers to the questions that you know. Also post questions that are not available.


To start with, Sr2Jr’s first step is to reduce the expenses related to education. To achieve this goal Sr2Jr organized the textbook’s question and answers. Sr2Jr is community based and need your support to fill the question and answers. The question and answers posted will be available free of cost to all.

 

#
Authors:
Walter Savitch ,kenrick Mock
Chapter:
Arrays
Exercise:
Programming Projects
Question:9 | ISBN:9780132830317 | Edition: 5

Question

Enhance the definition of the class PartiallyFilledArray ( Display 6.5 ) in the following way: When the user attempts to add one additional element and there is no room in the array instance variable a, the user is allowed to add the element. The object creates a second array that is twice the size of the array a, copies values from the array a to the user’s new array, makes this array (or more precisely its reference) the new value of a, and then adds the element to this new larger array a. Hence, this new class should have no limit (other than the physical size of the computer) to how many numbers it can hold. The instance variable maxNumberOfElements remains and the method getMaxCapacity is unchanged, but these now refer to the currently allocated memory and not to an absolute upper bound. Write a suitable test program.


TextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbook

Answer

PROGRAM CODE:

public class PartiallyFilledArray {
    private int[] a;
    private int numberOfElements;

    // Constructor to initialize the PartiallyFilledArray
    public PartiallyFilledArray(int initialCapacity) {
        a = new int[initialCapacity];
        numberOfElements = 0;
    }

    // Method to add an element to the array
    public void addElement(int element) {
        if (numberOfElements == a.length) {
            // Create a new array with double the current size
            int[] newArray = new int[a.length * 2];

            // Copy elements from the old array to the new array
            for (int i = 0; i < numberOfElements; i++) {
                newArray[i] = a[i];
            }

            // Update the reference to the new array
            a = newArray;
        }

        // Add the element to the array and increment the number of elements
        a[numberOfElements] = element;
        numberOfElements++;
    }

    // Method to get the current number of elements in the array
    public int getNumberOfElements() {
        return numberOfElements;
    }

    // Method to get the maximum capacity of the dynamically growing array
    public int getMaxCapacity() {
        return a.length;
    }

    // Method to print the elements in the array
    public void displayElements() {
        System.out.print("Array elements: ");
        for (int i = 0; i < numberOfElements; i++) {
            System.out.print(a[i] + " ");
        }
        System.out.println();
    }

    // Main method for testing the class
    public static void main(String[] args) {
        // Create a PartiallyFilledArray object with an initial capacity of 5
        PartiallyFilledArray pfa = new PartiallyFilledArray(5);

        // Add elements to the array
        pfa.addElement(10);
        pfa.addElement(20);
        pfa.addElement(30);
        pfa.addElement(40);
        pfa.addElement(50);
        pfa.addElement(60);
        pfa.addElement(70);

        // Print the current number of elements and maximum capacity
        System.out.println("Number of elements: " + pfa.getNumberOfElements());
        System.out.println("Maximum capacity: " + pfa.getMaxCapacity());

        // Display the elements in the array
        pfa.displayElements();
    }
}

 

OUTPUT:

Number of elements: 7
Maximum capacity: 10
Array elements: 10 20 30 40 50 60 70 

 

0 0

Discussions

Post the discussion to improve the above solution.