So I am taking a Java class with a teacher who is not good at teaching us what we need to do the homework.
This assignment, we are supposed to make a Dynamic Array of Integers class with Constructors, Methods, and required features. Are are not allowed to use ArrayLists I've got some of them written, a couple are not working and I don't know how to do the others.
Here are the requirements:
- private int array[] field. You MUST store the data internally in a regular partially-filled array of integers. Please DO NOT USE ArrayList. The size of the allocated array is its capacity and will be discussed below.
private int size field. This variable stores the number of “occupied” elements in the array. Set to 0 in the constructor.
Constructor with parameter. The parameter defines the capacity of initial array. Allocates array of given capacity, sets size field to zero. In case the parameter given to constructor is less than 0, IllegalArgumentException is being thrown.
No-argument constructor. Allocates array of size 10, sets size field to 0.
Copy constructor. The constructor takes an object of type DynamibcArray as a parameter and copies it into the object it creates. The constructor throws IllegalArgumentException if the object that was passed to copy from is null.
int getSize() returns the size – a number of occupied elements in the array.
int [] toArray() accessor returns the array. Make sure you DO NOT return the private array field. Instead, allocate memory for the new array, copy your array field into that new object, and return the new array.
public void push(int num) adds a new element to the end of the array and increments the size field. If the array is full, you need to increase the capacity of the array: a. Create a new array with the size equal to double the capacity of the original one. b. Copy all the elements from the array field to the new array. c. Add the new element to the end of the new array. d. Use new array as an array field.
public int pop() throws RuntimeException removes the last element of the array and returns it. Decrements the size field. If the array is empty a RuntimeException with the message “Array is empty” must be thrown. At this point check the capacity of the array. If the capacity is 4 times larger than the number of occupied elements (size), it is time to shrink the array:
a. Create a new array with the size equal to half of the capacity of the original one. b. Copy all the elements from the array field to the new array. c. Use new array as an array field.
int get(int index) throws IndexOutOfBoundsException returns element of the array with the requested index. If the index provided is too large or negative, the IndexOutOfBoundsException is thrown with the message “Illegal index”.
int indexOf(int key) returns the index of the first occurrence of the given number. Returns -1 when the number is not found.
void add(int index, int num) throws IndexOutOfBoundsException adds a new element (passed as parameter num) to the location of the array specified by index parameter. If the index is larger than size of the array or less than 0, IndexOutOfBoundsException is thrown. When adding the element into the middle of the array, you’ll have to shift all the elements to the right to make room for the new one. If the array is full and there is no room for a new element, the array must be doubled in size. Please follow the steps listed in the push() method description to double the capacity of the array.
int remove ( int index) throws IndexOutOfBoundsException removes the element at the specified position in this array. When the element is removed from the middle of the array, all the elements must be shifted to close the gap created by removed element. If the index value passed into the method is more or equal to the size or less than 0 the IndexOutOfBoundsException must be thrown. At this point check the capacity of the array. If the capacity is 4 times larger than the number of occupied elements (size), it is time to shrink the array.
Here is the class with the methods that I've made so far.
/**
*
* @author Lisa Hergert
*/
public class DynamicArray {
private int array[];
private int size;
/*
* Constructor
* @param capacity - integer
* throws an IllegalArgumentExeception if capacity is less than 0
*/
public DynamicArray (int capacity) {
if (capacity < 0) {
throw new IllegalArgumentException("Size cannot be less than 0.");
}
}
/*
* no-arg constructor
*/
public DynamicArray () {
array = new int [10];
size = 0;
}
/*
* Copies the array to a new one
* @param - array [] - integer array
*/
public DynamicArray (int array[]) {
int arrayCopy [] = new int [array.length];
for (int i = 0; i < array.length; i++) {
arrayCopy[i] = array[i];
}
}
/*
* getSize returns the size.
* @return - size
*/
public int getSize () {
return size;
}
/*
* @param array[] - integer
* @return array
*/
public int [] toArray (int array[]) {
return array;
}
/*
* @param num - integer
*/
public void push (int num) {
}
/*
* @return pop - integer
*/
public int pop() throws RuntimeException {
return pop();
}
/*
* @param index - integer
*/
public int get(int index) throws IndexOutOfBoundsException {
if (index >= size || index < 0)
throw new IndexOutOfBoundsException("Illegal Index");
return array[index];
}
public int indexOf(int key) {
int index = 0;
return index;
}
public void add(int index, int num) throws IndexOutOfBoundsException {
if (index >= size || index < 0)
throw new IndexOutOfBoundsException("Illegal Index");
int oldValue = array[index];
array[index] = num;
return oldValue;
}
public int remove(int index) throws IndexOutOfBoundsException {
}
}
I am asking for help or even advice on where I can look up what I need to do. I I am going to go to the tutoring lab on campus and see if they can help me understand what the teacher has not taught properly, but I would like to try and work on it a little on my own before then.
Here is one of a few problems.
/*
* @return pop - integer
*/
public int pop() throws RuntimeException {
return pop();
}
I know that is not right. Even if you don't want to show me the code, can you maybe give me a recommendation on a site I can look up what I need to do, the requirements for this one are #9.
Thank you for your help. I wish I had the knowledge to do this without help, but that is not what has occurred, so I have to use outside sources. This site has been very helpful in the past.
public int pop() throws RuntimeException { return pop();this will just keep calling itself until you get a StackOverflow