In: Computer Science
Write these java classes:
1) DynArray.java: a class that models some of the functionality of the Java ArrayList. This class is not complete and must be modified as such:
Write the method body for the default constructor
Write the method body for the methods: arraySize(), elements(), grow(), shrink(). The incomplete code is provided here:
public class DynArray
{
private double[] array;
private int size;
private int nextIndex;
public int arraySize()
{
}
public int elements()
{
}
public DynArray()
{
}
private void grow()
{
}
private void shrink()
{
}
}
2) DynArrayDriver.java A simple driver class to test your DynArray class, you must write this class from scratch.
Thanks.
//Create java class DynArray.java
public class DynArray {
public double array[];
private int size;
private int nextIndex;
//constructor to initialise the variabls
public DynArray() {
array = new double[1];
nextIndex = 0;//next index is 0
size = 1;// size of the array initially 1
}
public int arraySize() {
//return size of array
return size;
}
public int elements() {
//return element count
return nextIndex;
}
//makes size double of array
private void grow() {
double temp[] = null;
if (nextIndex == size) {
temp = new double[2 * size];//temp array is double in size
for (int i = 0; i < size; i++) {
temp[i] = array[i];//copy all elements in temp array
}
}
array = temp;
size = size * 2;
}
public void shrink() {
double temp[] = null;
if (nextIndex > 0) {
// temp is a count size array
// and store array elements
temp = new double[nextIndex];
for (int i = 0; i < nextIndex; i++) {
// copy all array value into temp
temp[i] = array[i];
}
size = nextIndex;
// count size array temp initialize
// into variable array again
array = temp;
}
}
//we need to add method to add new element in array
public void add(double data) {
// check no of element is equql to size of array
if (nextIndex == size) {
grow(); // make array size double
} // insert element at end of array
array[nextIndex] = data;
nextIndex++;
}
}
____________________________________________________________________________________________
//Craete driver class DynArrayDriver.java
public class DynArrayDriver {
//driver class for DynArray.java
public static void main(String[] args) {
//create object of DynArray class
DynArray dyn = new DynArray();
System.out.println("Size of DynArray " + dyn.arraySize());//check
size of array using arraySIze function
System.out.println("Total elements in DynArray " +
dyn.elements());//get element count using elements()
dyn.add(10);//add element to the array
dyn.add(25);
System.out.println("Size of DynArray "+dyn.arraySize());
dyn.add(60);
dyn.add(1.5);
System.out.println("Total Elements in Array
"+dyn.elements());
System.out.println("Elements of DynArray");
for(int i=0;i<dyn.elements();i++)
{
System.out.print(dyn.array[i]+" ");//here array needs to public to
access it in another class
}
System.out.println("");
}
}
_____________________________________OUTPUT_______________________________________________