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

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)
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.
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...
1) Load the data from lab6_precipitation.csv into a numpy array; 2) Make a numpy array with...
1) Load the data from lab6_precipitation.csv into a numpy array; 2) Make a numpy array with the years 1916-2016 and dtype=int; 3) Make a numpy array with the months 1-12 and dtype=int; 4) Print out the shape of the data array and the lengths of the years and month array. If the length of your year array does not equal the number of rows in your data array, or the length of your months array does not equal the number...
For this lab you will continue your dynamic array by completing the class called MyDynamicArray. The...
For this lab you will continue your dynamic array by completing the class called MyDynamicArray. The MyDynamicArray class should manage the storage of an array that can grow and shrink. The public methods of your class should already be the following: MyDynamicArray(); Default Constructor. The array should be of capacity 2. MyDynamicArray(int s); For this constructor the array should be of capacity and size s. int& operator[](int i); Traditional [] operator. Should print a message if i is out of...
A template (generic) function for insertion sort is given to you. Part-1: design a function to...
A template (generic) function for insertion sort is given to you. Part-1: design a function to compare 2 strings using case-insensitive comparison, a function to test if 2 records are not equal (using case-insensitive string comparison). Part-2: define your own compare function required by the template insertionSort function. #include <iostream> #include <string> #include <cstdlib> #include <iomanip> #include <ctime> #define BASE 1000000000LL // long long int using namespace std; struct record { long long value; string str; }; // Part-1: case-insensitive...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT