Write a Java class called FlexArray that has/does the following:
Has a private variable array that holds integers as int[] array. Has a private variable capacity that is the maximum number of integers the array can hold. Has a private variable size that shows the number of currently occupied locations. Obviously, capacity >= size. Capacity tells you how many values the array can hold, not how many it has currently, which is available via the variable size.
Has a default constructor that sets capacity=10 and allocates array to new int[capacity]. Then sets size=0 because there is currently no value stored in the array yet.
Has a non-default constructor having parameter (int maxEntries) that sets up array to hold maxEntries number of integers, sets capacity=maxEntries and size=0. Make sure maxEntries is >0.
This array has the property that only consecutive entries starting from location 0 can contain integer values. In other words, the array contains values in locations 0 through size-1, which is equal to a total of size number of values.
A method called public void add(int val) adds the integer val at location size of the array as long as size<capacity. If size is equal to capacity, there should be an error message saying, Array is full, values cannot be added. add(..) method when successful will also increase the size by 1.
A method called public int set(int val, int location) that sets the value at location of array to val, returning the old value to the caller. (the old value is replaced with the new value val). The method should throw an error, if location >=size or location <0, meaning that you can only modify existing values in the array to new values.
A method called public int get(int location) that returns the value stored at location of the array. The method should throw an error if location >=size or location <0, meaning that you can only modify existing values in the array to new values.
//Java code
public class FlexArray { private int[] array; private int capacity; private int size; //default constructor public FlexArray() { capacity =10; array = new int[capacity]; size=0; } //non-default constructor public FlexArray(int maxEntries) { if(maxEntries>0) capacity = maxEntries; array = new int[capacity]; size =0; } public void add(int val) { if(size<capacity) { array[size] = val; size++; } else { System.err.println("Array is full"); } } public int set(int val, int location) { int oldvalue=-1; if(location>=size || location<0) System.err.println("Wrong location"); else { oldvalue = array[location]; array[location]= val; } return oldvalue; } @Override public String toString() { String result =""; for (int i=0;i<size;i++) { result+= array[i]+" "; } return result; } }
//====================================
public class FlexArrayDemo { public static void main(String[] args) { FlexArray flexArray = new FlexArray(5); flexArray.add(56); flexArray.add(33); flexArray.add(78); flexArray.add(12); flexArray.add(13); System.out.println(flexArray); flexArray.set(77,2); System.out.println("After set new value at index 2: \n"+flexArray); } }
//Output
//If you need any help regarding this solution .......... please leave a comment ........ thanks
Get Answers For Free
Most questions answered within 1 hours.