Question
In this assignment, you are required to implement a class MyArray as defined below, to store an array of integers (int). Many of its methods
In this assignment, you are required to implement a class MyArray as defined below, to store an array of integers (int). Many of its methods will be implemented using the principle of recursion.
Users can create an object by default, in which case, the array should contaienough 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.
MyArray supports the following operations.
• 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.
The private instance variables of MyArray are an array of integers, and how many elements does it have.
It is perfectly legal to use the services of private methods to implement any recursive algorithms you wish. That is, 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 eachelement, 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.
input file
13 // Number of elements to insert in the array 573 2739 76321 3873 73430 333 7293 38383 272 946 3832 8410 2583 // Last element for insertion - Search Elements follow 333 946 76321 26353 833 3832 123 573 765 2583
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Executable Code package myarray import javaioFileNotFoundException import javaioFileReader import javautilArrays import javautilNoSuchElementException import javautilScanner public class MyArray priva...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