In: Computer Science
SOURCE CODE
public class ManagedArray {
private int[] managedIntegerArray; // this is the array that we are managing
private int maximumSize; // this will hold the size of the array
private int currentSize = 0; // this will keep track of what positions in the array have been used
private final int DEFAULT_SIZE = 10; // the default size of the array
public ManagedArray()// default constructor initializes array to DEFAULT_SIZE
{
managedIntegerArray = new int[DEFAULT_SIZE];
maximumSize = DEFAULT_SIZE;
}
public ManagedArray(int size)// parameterized constructor initializes array to specified size
{
managedIntegerArray = new int[size];
maximumSize = size;
}
public ManagedArray(int[] arrayToCopy)// copy constructor
{
managedIntegerArray = new int[arrayToCopy.length];
maximumSize = arrayToCopy.length;
for (int i = 0; i < arrayToCopy.length; i++) {
managedIntegerArray[i] = arrayToCopy[i];
currentSize++;
}
}
public void add(int integerToAdd)// adds a new integer to the array
{
if (currentSize < maximumSize)// if the new integer fits in the existing array, add it
{
managedIntegerArray[currentSize] = integerToAdd;
currentSize++;
} else// if the new integer does not fit in the existing array, make a new array, copy
// the old one into it, and add the new integer to the new array
{
expand();
managedIntegerArray[currentSize] = integerToAdd;
currentSize++;
}
}
public void remove(int index)// remove an item from the array and shift all the values over
{
if (index < maximumSize - 1) {
for (int i = index; i < maximumSize - 1; i++) {
managedIntegerArray[i] = managedIntegerArray[i + 1];
}
currentSize--;
} else if (index == maximumSize - 1) {
currentSize--;
}
}
public void bubbleSort() {
// code for bubble sort goes here
for (int i = 0; i < currentSize-1; i++)
for (int j = 0; j < currentSize-i-1; j++)
if (managedIntegerArray[j] > managedIntegerArray[j+1])
// swap temp and arr[i]
swap(j, j+1);
}
public void selectionSort() {
// code for selection sort goes here
// One by one move boundary of unsorted subarray
for (int i = 0; i < currentSize-1; i++)
{
// Find the minimum element in unsorted array
int min_idx = i;
for (int j = i+1; j < currentSize; j++)
if (managedIntegerArray[j] < managedIntegerArray[min_idx])
min_idx = j;
// Swap the found minimum element with the first
// element
swap(i, min_idx);
}
}
public void swap(int index1, int index2) {
// code for swap goes here
int temp = managedIntegerArray[index1];
managedIntegerArray[index1] = managedIntegerArray[index2];
managedIntegerArray[index2] = temp;
}
public int findMaxIndex(int limit) {
// code to find the INDEX of the maximum value in the array goes here
// finds the maximum element in the array within the given limit
int max_idx = 0;
for (int i = 0; i < currentSize; i++) {
for (int j = 1; j < currentSize; j++) {
if(managedIntegerArray[j] > managedIntegerArray[max_idx] && managedIntegerArray[j] <= limit)
max_idx = j;
}
}
return max_idx;
}
private void expand()// create a new array and copy the existing one into it
{
int[] tempArray = new int[maximumSize * 2];
int i = 0;
for (int x : managedIntegerArray) {
tempArray[i++] = x;
}
managedIntegerArray = tempArray;
maximumSize = managedIntegerArray.length;
}
@Override // display the array
public String toString() {
String arrayString = "";
for (int i = 0; i < currentSize; i++) {
arrayString += (managedIntegerArray[i] + "\n");
}
return arrayString;
}
}
/**
* managedArrayTest
*/
// we shall use this class to test the managed array class
class managedArrayTest {
public static void main(String[] args) {
int[] testarr1 = new int[]{3, 4, 6, 1, 10};
int[] testarr2 = new int[]{12, 31, 22, 1, 43, 21, 8};
ManagedArray mr1 = new ManagedArray(testarr1);
ManagedArray mr2 = new ManagedArray(testarr2);
mr1.add(8);
mr2.remove(3);
System.out.println("Contents of first managed array before sorting: ");
System.out.println(mr1.toString());
System.out.println("Contents of second managed array before sorting: ");
System.out.println(mr2.toString());
mr1.bubbleSort();
mr2.selectionSort();
System.out.println("Contents of first managed array after sorting: ");
System.out.println(mr1.toString());
System.out.println("Contents of second managed array after sorting: ");
System.out.println(mr2.toString());
System.out.println("Index of maximum value in first managed array");
System.out.println(mr1.findMaxIndex(100000));
System.out.println("Index of maximum value in second managed array");
System.out.println(mr2.findMaxIndex(100000));
}
}
OUTPUT