In: Computer Science
THE LANGUAGE IS JAVA
Download the class ArrayManager.java and fill out the method shells with the specifications below. After creating the methods 2) through 5), test them by running the application. The main method allows users to enter a command and, when necessary, additional command specifications.
1) a method displayArray:
2) a method expandIntArray:
3) a method shrinkIntArray:
4) a method insertValue:
5) a method removeValue:
HERE'S "ArrayManager.java" code:
import java.util.Scanner; public class ArrayManager { int[] data = new int[0]; public static void main(String[] args) { ArrayManager am = new ArrayManager(); while(true) { System.out.println("please enter a command: display, expand, shrink, insert, remove, expand"); String command = new Scanner(System.in).next(); switch(command) { case "display": am.displayArray(am.data); break; case "expand": am.data = am.expandIntArray(am.data); break; case "shrink": am.data = am.shrinkIntArray(am.data); break; case "insert": System.out.println("please enter the new value"); int value = new Scanner(System.in).nextInt(); System.out.println("please enter the insertion index"); int insert = new Scanner(System.in).nextInt(); am.data = am.insertValue(am.data, value, insert); break; case "remove": System.out.println("please enter the removal index"); int delete = new Scanner(System.in).nextInt(); am.data = am.removeValue(am.data, delete); break; } } } void displayArray(int[] data) { } int[] expandIntArray(int[] data) { int[] data2 = null; return data2; } int[] insertValue(int[] data, int value, int index) { int[] data2 = null; return data2; } int[] removeValue(int[] data, int index) { int[] data2 = null; return data2; } int[] shrinkIntArray(int[] data) { int[] data2 = null; return data2; } }
GIVEN BELOW ARE THE COMPLETED VERSIONS OF THE AFOREMENTIONED FUNCTIONS:
void displayArray(int[] data) {
for(int i=0; i<data.length(); i++)
{
System.out.println(data[i]);
}
int[] expandIntArray(int[] data) {
int len= 2* data.length();
int[] data2 = new int [len];
for(int i=0; i<len; i++)
{
if(!i<data.length())
data2[i]=0;
else
data2[i]=data[i];
}
return data2;
}
int[] shrinkIntArray(int[] data)
{
int len=data.length()/2;
int[] data2 = new int [len];
for(int i=0; i<len; i++)
{
data2[i]=data[i];
}
return data2;
}
int[] insertValue(int[] data, int value, int
index) {
int len= data.length()+1;
int[] data2 = new int[len];
for(int i=0; i<(len-1); i++)
{
if(i<index)
data2[i]=data[i];
else if(i>index)
data2[i+1]=data[i];
}
data2[index]=value;
return data2;
}
int[] removeValue(int[] data, int
index) {
int len= data.length()-1;
int[] data2 = new int [len];
for(int i=0; i<len; i++)
{
if(i<index)
data2[i]=data[i];
else if(i>index)
data2[i]=
data[i-1];
}
return data2;
}