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)
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)...
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...
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...
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...
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...
Submission 2 - due Saturday June 23 before 5pm - You must submit your completed Bank...
Submission 2 - due Saturday June 23 before 5pm - You must submit your completed Bank Reconciliation, the journal entries to bring the cash balance to the correct balance, and the Bank Reconciliation formulas tab completed. Your file must be named correctly – Please Show equations The following information should be used to prepare a bank reconciliation for SWARS Sales and Consulting as of March 31. Prepare the reconciliation on the Bank Reconciliation tab. On March 31, 2018, SWARS Sales...
This will be your first collaborative assignment with your team. Your team will submit one 1-2...
This will be your first collaborative assignment with your team. Your team will submit one 1-2 page plan outlining the methods your team will use to work together. Topics such as communications, expectations of team members, and technology use should be covered.
Please write a C++ program. Please rewrite your Array (including the operator overloading) into a template....
Please write a C++ program. Please rewrite your Array (including the operator overloading) into a template. And rewrite your main function to test your template for integer array and double array. Following is my complete code: #include <iostream> using namespace std; class Array { private: // Pointer to memory block to store integers int* data; // Maximum size of memory block int cap; // Stores number of integers in an array int num; public: // Constructor Array(int size); // Default...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT