Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Java Main topics: Arrays ArrayLists Exercise This week we will be practicing wri

ID: 3732982 • Letter: J

Question

Java

Main topics:

Arrays

ArrayLists

Exercise

This week we will be practicing writing a portion of a class, that emulates some of the features of a Java ArrayList. You will be writing five of the methods from this week’s homework (Assignment 5).

Getting Started

To start this exercise, you should:

1. Open eclipse and start a new Java project named Lab05

2. Add a Class (named DynArray) to this project, and copy the contents of the DynArray.java file provided into it.

3. Add a Class (named DynArrayDriver) to this project, the contents of which you will be writting from scratch.

Requirements

DynArray.java

A very simple class which models some of the functionality of the Java ArrayList. This class is not complete and must be modified as such:

1. Write the method body for the default constructor

2. Write the method body for the method arraySize()

3. Write the method body for the method elements()

4. Write the method body for the method grow()

5. Write the method body for the method shrink()

DynArrayDriver.java

A simple driver class to test your DynArray class, you must write this class from scratch

--------------------------------------------------------------------------

This is what I have so far, all I need to be finished is the elements method

public class DynArray

{

private Double[] array;

private int physicalSize;

private int effectiveSize;

public int arraySize()

{

return physicalSize;

}

public int elements()

{

//HELP

}

public DynArray()

{

this.physicalSize = 1;

this.array = new Double[this.physicalSize];

this.effectiveSize = 0;

}

private void grow()

{

Double[] tempArray = array;

physicalSize = physicalSize*2;

this.array = new Double[physicalSize];

for (int i=0;i<effectiveSize;i++)

{

this.array[i] = tempArray[i];

}

}

private void shrink()

{

Double[] tempArray = array;

if (effectiveSize < physicalSize/2)

{

if (physicalSize % 2 == 0)

{

physicalSize = physicalSize/2;

}

else

{

physicalSize = (physicalSize/2)+1;

}

this.array = new Double[physicalSize];

}

for (int i=0;i<effectiveSize;i++)

{

this.array[i] = tempArray[i];

}

}

}

Explanation / Answer

Answer.)

As per the understanding of the code, providing the implementation, however no method description being provided in the question.

DynArray.java


package com.example.Arraylist1;
public class DynArray
{
private Double[] array;
private int physicalSize;
private int effectiveSize;

public int arraySize()
{
return physicalSize;
}

public Double[] elements()
{
return this.array;
}

public DynArray()
{
this.physicalSize = 1;
this.array = new Double[this.physicalSize];
this.effectiveSize = 0;
}

public void grow()
{
Double[] tempArray = array;
physicalSize = physicalSize*2;
this.array = new Double[physicalSize];
for (int i=0;i<effectiveSize;i++)
{
this.array[i] = tempArray[i];
}
}

public void shrink()
{
Double[] tempArray = array;
if (effectiveSize < physicalSize/2)
{
if (physicalSize % 2 == 0)
{
physicalSize = physicalSize/2;
}
else
{
physicalSize = (physicalSize/2)+1;
}
this.array = new Double[physicalSize];
}
for (int i=0;i<effectiveSize;i++)
{
this.array[i] = tempArray[i];
}
}
}

DynArrayDriver.java


package com.example.Arraylist1;
public class DynArrayDriver {
public static void main(String[] args) {
DynArray dynArray=new DynArray();
System.out.println("Initial Array size :"+dynArray.arraySize());
dynArray.grow();
System.out.println("New Array size after grow :"+dynArray.arraySize());
dynArray.grow();
System.out.println("New Array size after grow :"+dynArray.arraySize());
dynArray.shrink();
System.out.println("New Array size after shrink :"+dynArray.arraySize());
System.out.print("Elements in DynArray : ");
for(Double d : dynArray.elements()){
System.out.print(d+" ");
}
}
}

Output :

Initial Array size :1
New Array size after grow :2
New Array size after grow :4
New Array size after shrink :2
Elements in DynArray : null null

If you have any doubts, drop a comments below. Thanks.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote