Question

In: Computer Science

1) Submit your completed dynamic array with template/generic data type (DArray.java) implementation. 2) Complete the template...

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

Solutions

Expert Solution

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.


Related Solutions

c++ You will implement a template class with the following members: An array of our generic...
c++ You will implement a template class with the following members: An array of our generic type. The size of this array should be determined by an integer argument to the constructor. An integer for storing the array size. A constructor capable of accepting one integer argument. The constructor should initialize the array and set the array size. A method, find Max, that returns the maximum value in the array. A method, find Min, that returns the minimum value in...
Name the data type,value type and array size / name of the followings. 1) 20.4 2)...
Name the data type,value type and array size / name of the followings. 1) 20.4 2) a=5 3) [2 3 8] 4) ' 7 ' 5) ' abc' 6) pi 7) b=true 8) 20+2i 9) [ 'a' 'b' 'c' ] 10) [ 'a' ; 'b' ] 11) [1 2 ;4 5;7 8] 12) [20,21,22,23] 13) c= [a;b] 14) c= [b:a] 15) d=[b:a ; b:a] 16) magic(3) 17 uint8(20) 18) int8(d)
Give a pseudo-code description for an array-based implementation of the deque ADT (Abstract Data Type). What...
Give a pseudo-code description for an array-based implementation of the deque ADT (Abstract Data Type). What is the running time for each operation?
Complete the implementation of the Die class. Note that the Die class uses an array to...
Complete the implementation of the Die class. Note that the Die class uses an array to represent the faces of a die. You need to complete the no-argument constructor and the two methods compareTo and equals. Code: import java.util.Arrays; import java.util.Objects; import java.util.Random; /* * NOTE TO STUDENTS: * The constructor that you need to complete can be found on line 47. * * The two methods you need to complete can be found at the end of this file....
Define the classes to complete dynamic array hierarchy with a concrete, abstract and interface class. public...
Define the classes to complete dynamic array hierarchy with a concrete, abstract and interface class. public class DArray { private int array[]; public DArray() { } private void expandArray() { } private void shrinkArray() { } } --------------------------------------------------------------- public abstract class ArrayBP { protected int numElements; protected int numAllocations; public abstract void storeAt(int item, int index) { } public abstract getFrom(int index) { } public abstract int len() { } public abstract void remove();{ } public abstract void removeAt(int index)...
Define a class template called genericStack for storing any data type in a static stack. Your...
Define a class template called genericStack for storing any data type in a static stack. Your class template must include a constructor, a destructor, push, pop, peek, isFull, and isEmpty functions (no display function is required). Write a simple main function in your program that demonstrates the class template with a stack of strings.
1. Create a class Point which has a template parameter of the type of internal data,...
1. Create a class Point which has a template parameter of the type of internal data, T, and a template parameter for the dimension of the Point(2D, 3D etc.). Store a statically allocated, internal array of type T with dimension n. Ensure to include any constructer(s),destructors, getters or setters you might need. 2. Create a template function which computes the Euclidean distance between 2 points. 3. Instantiate two Point and compute their distance. Instantiate two Point and compute their distance....
Implement in C++ Design a BookstoreManager class which creates a dynamic array of type Book (don’t...
Implement in C++ Design a BookstoreManager class which creates a dynamic array of type Book (don’t use vectors), and provide an implementation for the following operations on books in the array 1)isEmpty() returns true if the array is empty, otherwise false 2)isFull() returns true if the array is full, otherwise false 3)listSize() prints the number of books in the array 4)print() prints the content of the array 5)insert(Book) asks the user to enter new book info, and it adds the...
Implement in C++ Design a BookstoreManager class which creates a dynamic array of type Book (don’t...
Implement in C++ Design a BookstoreManager class which creates a dynamic array of type Book (don’t use vectors), and provide an implementation for the following operations on books in the array 1)isEmpty() returns true if the array is empty, otherwise false 2)isFull() returns true if the array is full, otherwise false 3)listSize() prints the number of books in the array 4)print() prints the content of the array 5)insert(Book) asks the user to enter new book info, and it adds the...
Problem 1. Complete the table below. Please use the spreadsheet for your convenience and to submit...
Problem 1. Complete the table below. Please use the spreadsheet for your convenience and to submit your responses. Given: total Fixed Costs = $60 (i.e., the fixed cost of $60 does not change with output). The price of the product is $55 Production Schedule for a widget firm Output (Q) Total Fixed Costs (TFC) Total Variable Cost               (VC) Total Cost (TC) Average Fixed Cost (AFC) Average Variable Cost (AVC) Average Total Costs (ATC) Total Revenue (TR) Profit 0 0 1...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT