Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Many applications require a list of values to remain in sorted order, even if new values are added to the list (or old values are

Many applications require a list of values to remain in sorted order, even if new values are added to the list (or old values are removed). It can be very expensive (in terms of CPU time) to continually re-sort the array every time a new value is added. A better approach is to insert new values in their proper place as they are added to the list, shifting elements down as necessary.

Define a new class, SortedIntegerArray. This class contains two private instance variables: an integer array (DO NOT use an ArrayList!) and an integer that tracks how many values are currently stored in the array (a value between 0 and the array length).

Implement the following public methods for your new data type:

* A constructor that takes a single integer argument. The constructor should set the size instance variable to 0 (the array is initially empty) and creates a new integer array large enough to hold the specified number of values (for example, SortedIntegerArray(10) would create a new instance with a 10-element array). Unlike the stack from Lab 17, your internal array DOES NOT have to expand to accommodate additional values.

* A size() method that returns the number of elements currently stored inside the array.

* A get() method. This method takes an integer index as its argument and returns the value stored in that position. If the position does not exist (because the specified index is less than 0 or greater than or equal to size), return -1 instead.

* A toString() method that returns a String containing all of the elements that are currently stored in the array. Your resulting String should only contain values located in currently occupied array positions (in other words, only include the values from index 0 up through index (size 1))!

* An add() method. This method takes a single integer argument and inserts that value into its proper place in the array, in sorted (ascending) order. You may need to shift other array elements over by one position each to make room for the new value; use a loop to do this, starting from the end of the array. If the array is already full (its size is equal to the arrays length), do not insert the new value (print an error message if you wish, or just allow the operation to fail silently). You may assume that the array will only store non-negative integers (0 or greater). Be sure to update the size variable if your addition is successful!

* A remove() method. This method takes an array index (an integer in the range 0 through (size - 1)) as its argument. If the index is valid, remove the element at that position and shift any following elements forward to close the gap (dont forget to update the size variable as well). This method does not return anything.

Finally, add a main() method that creates a new SortedIntegerArray object and displays the result of inserting several values into the array. Show that your implementation correctly places values into their proper sorted positions, and that your size(), get(), and toString() methods work correctly.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Machine Learning And Knowledge Discovery In Databases European Conference Ecml Pkdd 2015 Porto Portugal September 7 11 2015 Proceedings Part 2 Lnai 9285

Authors: Annalisa Appice ,Pedro Pereira Rodrigues ,Vitor Santos Costa ,Joao Gama ,Alipio Jorge ,Carlos Soares

1st Edition

3319235249, 978-3319235240

Students also viewed these Databases questions

Question

Might the benefit be reinstated someday?

Answered: 1 week ago