Write a class to represent a dynamically sized array of inte

Write a class to represent a dynamically sized array of integers.Name it DynArray.

Class specifications:

Private data members:

data-a pointer to integer array data.

arrayize- an integer representing representing the array (allocated space).

datasize-an integer representing the amount of data stored in the array.

Public member functions:

default constructor: sets the array size to be 10 elements, the data size to be 0, and dynamically allocates the 10 element array space.

constructor receiving size: checks the size parameter value to make sure that it is a positive value. If the value is positive, it becomes the array size. If the value is not legal, the array size should be set to 0, and the dynamic allocation is performed.

copy constructor: receive one DynArray object as a parameter and make the new object a copy of that object with its own separately allocated memory space, but with the same sizes and same data.

destructor: Dynamically de-allocate the array space and set the pointer value to NULL.

setArraySize: This function receives a size value If the parameter value is greater thatn the existing array size, change the array size, allocate a new array space, and deallocate the old array space. If the size value is invalid (smaller than current size), do nothing.

getArraySize: Return a copy of the array size field.

getDataSize:Return a copy of the data size field.

addValue: Receives an integer value. If there is currently room in the allocated array space, then store the value into the array. If there is not space, then relocate the array to another location with twice as much space as before, and then store the parameter value into the array.

= operator: Receives one DynArray object as a parameter and makes the current object a copy of that parameter object with its own separately allocated array space. Make sure to de-allocate the current array space before dynamically allocating a new one.

print: writes out all the data stored in the dynamic array.

Solution

import java.util.Arrays;

public class DynamicArray

{

// The storage for the elements.

// The capacity is the length of this array.

private int[] data;

  

// The number of elements (logical size).

// It must be smaller than the capacity.

private int size;

  

// Constructs an empty DynamicArray

public DynamicArray()

{

    data = new int[16];

    size = 0;

}

  

// Constructs an empty DynamicArray with the

// specified initial capacity.

public DynamicArray(int capacity)

{

    if (capacity < 16)

    capacity = 16;

    data = new int[capacity];

    size = 0;

}

  

// Increases the capacity, if necessary, to ensure that

// it can hold at least the number of elements

// specified by the minimum capacity argument.

public void ensureCapacity(int minCapacity)

{

    int oldCapacity = data.length;

    if (minCapacity > oldCapacity)

    {

      int newCapacity = (oldCapacity * 2);

      if (newCapacity < minCapacity)

        newCapacity = minCapacity;

      data = Arrays.copyOf(data, newCapacity);

    }

}

  

// Returns the logical size

public int size()

{

    return size;

}

  

public boolean isEmpty()

{

    return size == 0;

}

  

// Checks if the given index is in range.

private void rangeCheck(int index)

{

    if (index >= size || index < 0)

      throw new IndexOutOfBoundsException(\"Index: \" +

            index + \", Size: \" + size);

}

  

// Returns the element at the specified position.

public int get(int index)

{

    rangeCheck(index);

    return data[index];

}

  

// Appends the specified element to the end.

public boolean add(int element)

{

    ensureCapacity(size + 1);

  data[size++] = element;

    return true;

}

  

// Removes all of the elements.

public void clear()

{

    size = 0;

}

  

// Replaces the element at the specified position

public int set(int index, int element)

{

    rangeCheck(index);

    int oldValue = data[index];

    data[index] = element;

    return oldValue;

}

  

public int capacity()

{

    return data.length;

}

}

Write a class to represent a dynamically sized array of integers.Name it DynArray. Class specifications: Private data members: data-a pointer to integer array d
Write a class to represent a dynamically sized array of integers.Name it DynArray. Class specifications: Private data members: data-a pointer to integer array d
Write a class to represent a dynamically sized array of integers.Name it DynArray. Class specifications: Private data members: data-a pointer to integer array d
Write a class to represent a dynamically sized array of integers.Name it DynArray. Class specifications: Private data members: data-a pointer to integer array d

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site