In: Computer Science
1) Submit your completed dynamic array with template/generic data type (DArray.java) implementation.
2) Complete the template DArray (if you have not done so) and this attached Iterator class. Make sure to document and test your code thoroughly.
import java.util.Arrays;
public class DArray
{
private final double GROW_FACTOR = 0.5;// array size growing
rate
//attributes
private int size;
private int buffer[]; //the actual array
//constructors
public DArray()
{
this.size=10;
}
public DArray(int size) throws Exception
{
if(size < 0)
{
throw new Exception("Not a valid range");
}
this.size = size;//the actual array size
int bufferSize = (int) Math.ceil(this.size + this.size *
GROW_FACTOR);
this.buffer = new int[bufferSize]; //create the buffer
}
//methods
/**
*
* @return the actual size of the array
*/
public int length()
{
return this.size;
}
/**
*
* @return the max length of the dynamic array
*/
public int maxLength()
{
return this.buffer.length;
}
/**
*
* @param index - the location of the element in the array
* @return the element at the given location/index
*/
public int elementAt(int index) throws Exception
{
if(index < 0 || index >= maxLength())
throw new Exception("Index out of bound");
return this.buffer[index];
}
public void set(int index ,int value)
{
if(index>maxLength())
{
resize(index);
set(index,value);
}
else
buffer[index]=value;
}
public void resize(int size)
{
buffer = Arrays.copyOf(buffer, buffer.length + size);
}
public int getFirst()
{
return buffer[0];
}
public int getLast()
{
return buffer[maxLength()-1];
}
//////////////////////////////////////////////////////////
//driver to test the code
public static void main(String[] args) throws Exception
{
DArray a = new DArray(10);
System.out.println(a.length());
System.out.println(a.maxLength());
a.set(0, 10); //set value 10 into index 0
a.set(100, 50);//set value 50 to index 100
System.out.println("Element at 100 postion
is:"+a.elementAt(100));
a.resize(2);
System.out.println("New length of the array:"+a.maxLength());
System.out.println("First element is "+a.getFirst());
System.out.println("Last element is "+a.getLast());
}//end of main
}//end of DArray class
Code
import java.util.Arrays;
public class DArray<E>
{
private final double GROW_FACTOR = 0.5;// array size growing
rate
//attributes
private int size;
private E[]buffer; //the actual array
//constructors
public DArray()
{
this.size=10;
}
// constructor
public DArray(Class<E> type, int size)throws Exception
{
if(size < 0)
{
throw new Exception("Not a valid range");
}
this.size = size;//the actual array size
int bufferSize = (int) Math.ceil(this.size + this.size *
GROW_FACTOR);
// Creates a new array with the specified type and length at
runtime
this.buffer = (E[]) java.lang.reflect.Array.newInstance(type,
bufferSize);
}
//methods
/**
*
* @return the actual size of the array
*/
public int length()
{
return this.size;
}
/**
*
* @return the max length of the dynamic array
*/
public int maxLength()
{
return this.buffer.length;
}
/**
*
* @param index - the location of the element in the array
* @return the element at the given location/index
*/
public E elementAt(int index) throws Exception
{
if(index < 0 || index >= maxLength())
throw new Exception("Index out of bound");
return this.buffer[index];
}
public void set(int index ,E value)
{
if(index>maxLength())
{
resize(index);
set(index,value);
}
else
buffer[index]=value;
}
public void resize(int size)
{
buffer = Arrays.copyOf(buffer, buffer.length + size);
}
public E getFirst()
{
return buffer[0];
}
public E getLast()
{
return buffer[maxLength()-1];
}
//////////////////////////////////////////////////////////
//driver to test the code
public static void main(String[] args) throws Exception
{
System.out.println("\nInteger array......");
// create an Integer array of given length
DArray<Integer> a = new DArray(Integer.class,
10);
System.out.println(a.length());
System.out.println(a.maxLength());
a.set(0, 10); //set value 10 into index 0
a.set(100, 50);//set value 50 to index 100
System.out.println("Element at 100 postion
is:"+a.elementAt(100));
a.resize(2);
System.out.println("New length of the array:"+a.maxLength());
System.out.println("First element is "+a.getFirst());
System.out.println("Last element is "+a.getLast());
System.out.println("\n\nString array......");
// create an String array of given length
DArray<String> stringArray = new DArray(String.class,
10);
System.out.println(stringArray.length());
System.out.println(stringArray.maxLength());
stringArray.set(0, "Jay"); //set value 10 into index 0
stringArray.set(100, "Vijay");//set value Vijay to index 100
System.out.println("Element at 100 postion
is:"+stringArray.elementAt(100));
stringArray.resize(2);
System.out.println("New length of the string
array:"+stringArray.maxLength());
System.out.println("First element is
"+stringArray.getFirst());
System.out.println("Last element is "+stringArray.getLast());
}//end of main
}//end of DArray class
output
If you have any query regarding the code please ask me in the
comment i am here for help you. Please do not direct thumbs down
just ask if you have any query. And if you like my work then please
appreciates with up vote. Thank You.