Question

In: Computer Science

//ONLY PUT SCREENSHOTS OF THE ANSWER FROM INTELLIJ //Complete the incomplete methods //You will need to...

//ONLY PUT SCREENSHOTS OF THE ANSWER FROM INTELLIJ
//Complete the incomplete methods
//You will need to create a driver to test this.


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];
}
}

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

}

public void selectionSort()
{
//code for selection sort goes here
}

public void swap(int index1, int index2)
{
//code for swap goes here
}

public int findMaxIndex(int limit)
{
//code to find the INDEX of the maximum value in the array goes here
}

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;
}
}

Solutions

Expert Solution

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


Related Solutions

//Complete the incomplete methods in the java code //You will need to create a driver to...
//Complete the incomplete methods in the java code //You will need to create a driver to test this. 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...
I need to know how to answer this question only in Excel. Please include instructions, screenshots,...
I need to know how to answer this question only in Excel. Please include instructions, screenshots, etc. in Excel which explain the process (formulas included). TropSun is a leading grower and distributer of fresh citrus products with three large citrus groves scattered around central Florida in the cities of Orlando, Eustis, and Winter Haven. TropSun currently has 275,000 bushels of citrus at the grove in Mt. Dora, 400,000 bushels at the groves in Eustis, and 300,000 bushels at the grove...
(Complete Using Ubuntu) Please try each command in Linux system, and get screenshots (you may put...
(Complete Using Ubuntu) Please try each command in Linux system, and get screenshots (you may put multiple commands in one screenshot) which can show how actually each command runs. Also, give a brief description (one or two sentences) for each command. For the commands which have multiple switches/parameters, please try one popular switch/parameter. Linux Commands: rm,cp ,mv (name), ld, ftp, more, less, cat (date), tar, top, ps (name), kill, df, last, patch, mkdir (date)
(NO CLICKED PHOTOS OR SCREENSHOTS PLEASE, ONLY COMPLETE SOLUTION) The data in the table below presents...
(NO CLICKED PHOTOS OR SCREENSHOTS PLEASE, ONLY COMPLETE SOLUTION) The data in the table below presents the hourly quantity of production for three lines of production processes over the first 4 days in XYZ Company. Answer the questions based on the Excel Output given below. Day Process 1 Process 2 Process 3 1 33 33 28 2 30 35 36 3 28 30 30 4 29 38 34 ANOVA: Single Factor SUMMARY Groups Count Sum Average Variance Process 1 4...
Use Packet Tracer to complete the following labs. Answer the questions and record screenshots in a...
Use Packet Tracer to complete the following labs. Answer the questions and record screenshots in a Word document labeled firstInitial+LastName+Communications. In this lab, students will explore how ping, traceroute, and the default gateway setting affect device communication. Execute this lab according to the following guidelines: Configure all devices on the network with the following IP addresses: Device IP Address Wrk1 192.168.15.1 Wrk2 192.168.15.2 Sidon1 192.168.15.250 Eden Fa0/0 192.168.15.254 Eden Fa0/1 192.168.16.254 Sidon2 192.168.16.2 Wrk12 192.168.16.1 Open the command prompt for...
Please only answer if you're 100% on the answer and can fully complete. Thank you. USE...
Please only answer if you're 100% on the answer and can fully complete. Thank you. USE FIRST-IN, FIRST-OUT COSTING Sulu Company produces its finished product in two processing departments--Assembly and Packaging. The following information is available for the month of July: Assembly Department: The beginning work-in-process inventory was $5,755 ($4,815 direct materials and $940 conversion costs) and consisted of 350 units that were 10% complete as to conversion costs. During July, an additional 4,500 units were started into production. A...
Show a complete graph for both of these shocks. You will need two graphs only, one...
Show a complete graph for both of these shocks. You will need two graphs only, one for each shock. The graphs have to show: -The new short run equilibrium immediately after the shock occurs. -The new long run equilibrium. -The transition to the long run equilibrium. Properly label your axes and the curves in your graphs, and use arrows to show shifts of curves and the change in the price level and output. The two shocks are: The stock market...
Choose the correct answer to complete the following sentence. Income from cancellation of debt: Is only...
Choose the correct answer to complete the following sentence. Income from cancellation of debt: Is only excluded from income if the buyer is insolvent. Is not excludable from income if the buyer is insolvent. May be excluded to the extent the buyer has negative net worth. Is taxable to the extent that the buyer has negative net worth
You need to complete the methods getSmallerValue, getLargerValue, compareTo, and equals. Must pass these tests: Test...
You need to complete the methods getSmallerValue, getLargerValue, compareTo, and equals. Must pass these tests: Test that each domino is smaller than every domino after it in the list (compareTo method). - Test that each domino is greater than every domino before it in the list (compareTo method). - Tests if three dominoes with the same values are equal (equal method). Code: public class Domino implements Comparable<Domino> { /** * The smallest possible value for a side of a domino....
For this, problem, please include the excel procedure you used to arrive at an answer (screenshots,...
For this, problem, please include the excel procedure you used to arrive at an answer (screenshots, explanation, actual file, etc.). Brokerage Overall Satisfaction with Electronic Trades Satisfaction with Trade Price Satisfaction with Speed of Execution Scottrade, Inc. 3.5 3.4 3.4 Charles Schwab 3.4 3.2 3.3 Fidelity Brokerage Services 3.9 3.1 3.4 TD Ameritrade 3.7 2.9 3.6 E*Trade Financial 2.9 2.9 3.2 (Not listed) 2.7 2.5 3.2 Vanguard Brokerage Services 2.8 2.6 3.8 USAA Brokerage Services 3.6 2.4 3.8 Thinkorswim 2.6...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT