Question
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
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
- 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 new ArrayIndexOutOfBoundsException(), if location >=size or location <0, meaning that you can only modify existing values in the array to new values. Use throw ... as indicated.
- A method called public int get(int location) that returns the value stored at location of the array. The method should throw new ArrayIndexOutOfBoundsException(), if location >=size or location <0, meaning that you can only modify existing values in the array to new values. Use throw ... as indicated.
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