Question
Implement a class MyArray to store an array of integers, implementing most of the methods by recursion. Users can create an object by default, in
Implement a class MyArray to store an array of integers, implementing most of the methods by recursion.
Users can create an object by default, in which case, the array should contain enough space to store 10 integer values. Obviously, the user can specify the size of the array s/he requires. Users may choose the third way of creating an object of type MyArray by making a copy of another MyArray object given as parameter.
public class MyArray { private int [] arr; private int sizeArray = 0;
public MyArray(){ this.sizeArray = 10; arr = new int[sizeArray]; } public MyArray(int sizeArray) { this.sizeArray = sizeArray; arr = new int[sizeArray]; } public MyArray(MyArray arr1){ for(int i: arr1.getArray()){ arr[i]=arr1.getArray()[i]; } }
Implement the following methods:
getSize : returns the size of the array.
get (i): returns the i-th element of the array. If the element does not exist, it throws a "NoSuchElementException which is a subclass of Java class RunTimeException.
add (val): inserts value as the last element of the array. If necessary, double the size of the current array to accommodate the new item.
equals: Two MyArrays are equal if they have the same number of elements in the same order. Implement this overriding method recursively.
sum: returns the sum of all elements of the array. Implement this recursively.
max: returns the largest element of the array. Implement this recursively.
min: returns the smallest element of the array. Implement this recursively.
srch (elt): Searches for the element using the recursive binary search algorithm and returns the index of position where it is found, -1 if not found.
sort: Sorts the array using the bubble sort algorithm. Implement this recursively.
invert: Inverts the order of elements in the array. Implement this recursively.
toString: This must return a String which contains complete information about the array including how many elements it has and what are their values. Overrides the same method in the Object class.
You can implement some of the above methods with a call to another (private) method of the class passing appropriate parameters.
To test your implementation of MyArray class, write a test program as follows:
Create a default MyArray MA1.
Prompt the user for the name of the input and output files.
Output the size of MA1.
Read the number of elements to insert in MA1. Let this be n. [This is the first line of input from the file.]
Now, read n integer values from the file and insert them in MA1 in that order.
Output the size of MA1.
Output MA1.
Find and output the largest and smallest elements of MA1.
Output the sum of all elements of MA1.
Create another MyArray MA2 by copying MA1.
Check and output if MA1 and MA2 are equal.
Sort MA2 using the recursive bubble sort algorithm.
Check and output if MA1 and MA2 are equal.
Output MA2.
Read the search elements from the input file (just continue to read the input file) till the end of the file, and for each element, output if that element exists in MA2. If it does, output its position; otherwise indicate that it does not exist in the array.
Invert MA2.
output MA2.
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