Question

In: Computer Science

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


code has to be readable.

Solutions

Expert Solution

If you have any problem with the code feel free to comment.

Program

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;
                currentSize = maximumSize;
                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
                for (int i = currentSize - 1; i > 1; i--) {// ascending order
                        for (int j = 0; j < i; j++) {
                                if (managedIntegerArray[j] > managedIntegerArray[j + 1]) {
                                        swap(j, j + 1);
                                }
                        }
                }
        }

        public void selectionSort() {
                // code for selection sort goes here
                for (int i = 0; i < currentSize; i++) {
                        int min = i;
                        for (int j = i; j < currentSize; j++) {
                                if (managedIntegerArray[min] > managedIntegerArray[j])
                                        min = j;
                        }
                        swap(min, i);
                }
        }

        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
                int max = managedIntegerArray[0];
                int index = 0;
                for (int i = 1; i < currentSize; i++) {
                        if (max < managedIntegerArray[i]) {
                                max = managedIntegerArray[i];
                                index = i;
                        }
                }
                return index;
        }

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

public class Test {//test class
        public static void main(String[] args) {
                int[] ar1 = { 6, 5, 8, 4, 3, 2, 8 };
                ManagedArray ma1 = new ManagedArray(ar1);

                int[] ar2 = { 10, 63, 48, 12, 32, 74 };
                ManagedArray ma2 = new ManagedArray(ar2);

                // sorting ma1 using bubble sort
                ma1.bubbleSort();
                System.out.println("Managed Array 1: \n" + ma1);

                // sorting ma2 using selection sort
                ma2.selectionSort();
                System.out.println("Managed Array 2: \n" + ma2);
        }
}

Output


Related Solutions

java code Add the following methods to the LinkedQueue class, and create a test driver for...
java code Add the following methods to the LinkedQueue class, and create a test driver for each to show that they work correctly. In order to practice your linked list cod- ing skills, code each of these methods by accessing the internal variables of the LinkedQueue, not by calling the previously de?ined public methods of the class. String toString() creates and returns a string that correctly represents the current queue. Such a method could prove useful for testing and debugging...
//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...
PLEASE CODE THIS IN JAVA Create a driver class Playground that contains the function, public static...
PLEASE CODE THIS IN JAVA Create a driver class Playground that contains the function, public static void main(String[] args) {}. Create 2 SportsCar and 2 Airplane instances using their constructors. (SPORTSCAR AND AIRPLANE CLASSES LISTED BELOW THIS QUESTION. Add all 4 instances into a single array called, “elements.” Create a loop that examines each element in the array, “elements.” If the elements item is a SportsCar, run the sound method and if the item is an Aeroplane, run it’s ChangeSpeed...
Using the provided Java program below, complete the code to do the following. You may need...
Using the provided Java program below, complete the code to do the following. You may need to create other data items than what is listed for this assignment. The changes to the methods are listed in the comments above the method names. TODO comments exist. Apply any TODO tasks and remove the TODO comments when complete. Modify the method findMyCurrency() to do the following:    a. Use a do-while loop b. Prompt for a currency to find. c. Inside this...
I need the output of the code like this in java First we create a new...
I need the output of the code like this in java First we create a new building and display the result: This building has no apartments. Press enter to continue......................... Now we add some apartments to the building and display the result: This building has the following apartments: Unit 1 3 Bedroom Rent $450 per month Currently unavailable Unit 2 2 Bedroom Rent $400 per month Currently available Unit 3 4 Bedroom Rent $1000 per month Currently unavailable Unit 4...
Need to make Java code as following: Create a dice playing threading program to do the...
Need to make Java code as following: Create a dice playing threading program to do the following: 1. Start a thread for a process to simulate a computer rolling a die 1000 times. 2. Start another thread for a process to simulate a user rolling a die 1000 times. 3. Keep track of rolls for user and computer in an array(s). 4. Display on screen when the computer thread starts, the rolls, and when the computer thread ends. 5. Display...
Code in Java 1. Create a class Flower with data: Name, Price, Color and properly methods....
Code in Java 1. Create a class Flower with data: Name, Price, Color and properly methods. 2. Create another class named ListFlower. This class manages a collection of Flower (may be LinkedList) named a. Implementing some methods for ListFlower: Add: add new item of Flower to a Display: display all items of a sort(): sort as descending by Price and display all items of a search(Flower f): check and return whether f is exists in a or not. delete(int pos):...
Code in Java Create a stack class to store integers and implement following methods: 1) void...
Code in Java Create a stack class to store integers and implement following methods: 1) void push(int num): This method will push an integer to the top of the stack. 2) int pop(): This method will return the value stored in the top of the stack. If the stack is empty this method will return -1. 3) void display(): This method will display all numbers in the stack from top to bottom (First item displayed will be the top value)....
I need a full java code. And I need it in GUI With the mathematics you...
I need a full java code. And I need it in GUI With the mathematics you have studied so far in your education you have worked with polynomials. Polynomials are used to describe curves of various types; people use them in the real world to graph curves. For example, roller coaster designers may use polynomials to describe the curves in their rides. Polynomials appear in many areas of mathematics and science. Write a program which finds an approximate solution to...
very urgent !!! need it asap::::::::Write Java code which will create a GUI window on screen....
very urgent !!! need it asap::::::::Write Java code which will create a GUI window on screen. The window has one button: Doggy. When the user selects Doggy, “Bow Bow” should be printed on screen. Justify your syntax
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT