Question
For this assignment you will write a class using composition. You will implement a method that will sort the contents of the array contained in
For this assignment you will write a class using composition. You will implement a method that will sort the contents of the array contained in your class as an instance variable. Your sorting method will make use of the insertion sort algorithm and should return the time in milliseconds that it took your algorithm to sort the array (The System class in Java could be useful for this).
Implement the interface provided (following). Your class should have two constructors, a default constructor that uses DEFAULT_MAX_CAPACITY from the interface and a constructor that takes a single integer as a parameter to define the maximum capacity of the array. Additionally, your class should have a method of readFile() for reading the contents of a file (UnsortedNumberFile.txt) and storing the values therein into the array. And then your insertionSort() will sort the numbers in the Array. Afterwards the saveFile() method for storing the contents of the array into the outputfile. This output file should be named SortedNumberFile.txt and the format should be the same as the input file format (one entry per line).
/** SortableArray interface for use in Assignment 1
* Concrete class should have an integer array as an instance variable
* Throw exceptions when user attempts to access indexes less than zero or greater*than the number of elements.*/
public interface SortableArrayInterface{
int DEFAULT_MAX_CAPACITY = 1024;
//Returns true if the element was added to the first available position of the array
boolean add(int element);
//Inserts the specified element at the specified position in this list
//Shifts the element currently at that position and any subsequent elements to the right
//Returns true if element added correctly.
// Must observe 0 <= index <= size.
boolean add(int index, int element);
//Returns the element at the specified position in the array
int get(int index);
//Removes and returns the element at the specified position in the array
int remove(int index);
//Returns the number of elements in the array
int size();
//Returns true if able to read and store specified files contents into array
boolean readFile(String filename);
//Returns true if able to store array contents into specified file
boolean saveFile(String filename);
//Sort the elements of the array using insertion sort
voidinsertionSort();
}
Additional Details
Each method must be as efficient as possible. That is, a O(n) is unacceptable if the method could be written with O(log n) complexity.
Your project must consist of only the files specified (including the provided interface), no additional source code files are permitted.
You may not make any modifications to the SortableArrayInterface provided.
All of the above classes must be in a package named 'data_structures'.
You may import java.util.Iterator, java.util.NoSuchElementException, and java.io.* for file read and write. you may not use the Java API for any containers.
Your code must not print anything.
You must write generic code according to the interface provided. You may not add any public methods to the implementations, but you may add private ones, if needed.
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