Question
Create a new Java class, LX6. Download the class ArrayManager.java and copy its source code into LX6, changing the class name where necessary. Then fill
Create a new Java class, LX6. Download the class ArrayManager.java and copy its source code into LX6, changing the class name where necessary. Then fill out the method shells in LX6 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:
accepts as a parameter an int array named data
prints the array elements to the console, separated by commas and spaces, with the entire array enclosed in braces, e.g., the array [0, 1, 2] should display as {0, 1, 2}
2) a method expandIntArray:
accepts as a parameter an int array named data
creates a new int array twice as large as data
copies every int value from data into the same index in the new array
the other indexes in the new array should be zeroes
returns the new array
3) a method shrinkIntArray:
accepts as a parameter an int array named data
creates a new int array half as large as data
for every index in the new array, copies the corresponding int value from data into it
returns the new array
4) a method insertValue:
accepts as parameters an int array named data, an int named value, and an int named index
creates a new array one element larger than data
for every index < index, copies the value from data into the new array
copies value into the new array's index element
for every index > index, copies the value from data into the new array's next index, i.e., copies from index x in data to index x+1 in the new array
returns the new array
5) a method removeValue:
accepts as parameters an int array named data and an int named index
creates a new array one element smaller than data
for every index < index, copies the value from data into the new array
for every index > index, copies the value from data into the new array's previous index, i.e., copies from index x in data to index x-1 in the new array
returns the new array
Arraymanager.java source code below import java.util.Scanner; public class ArrayManager { int[] data = new int[0]; public static void main(String[] args) { ArrayManager am = new ArrayManager(); // you can try with different numbers to test your code am.data = new int[] {2,5,23,63,1,34,53,7}; 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; } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started