Design and implement an insertSorted() method for the MyArrayList class. The method should receive a single object and insert it in its correct position. You can assume that the calling array is already in a sorted order. The calling array is therefore updated. Follow the three step method for designing the method. Develop the java code for the method. Create a test program to test the method thoroughly using the Integer wrapper class.

EBK JAVA PROGRAMMING
9th Edition
ISBN:9781337671385
Author:FARRELL
Publisher:FARRELL
Chapter9: Advanced Array Concepts
Section: Chapter Questions
Problem 19RQ
icon
Related questions
Question

Design and implement an insertSorted() method for the MyArrayList class. The method should receive a single object and insert it in its correct position. You can assume that the calling array is already in a sorted order. The calling array is therefore updated.

Follow the three step method for designing the method.

Develop the java code for the method.

Create a test program to test the method thoroughly using the Integer wrapper class.

 

 

PMyarraylistcE> returnarray - new HyArraylist();
if (this.getsize() --a) // sane as if (sizes-0)
return paran;
if (paran. getsize()a0)
return this;
if ((this.getsize()+ param. getsize()) > HAXELEHENTS)
throw new IndexoutofBoundsException
("Combined list out of bounds");
Il traverse both list until one list is completely done
uhile (icthis.getsize() jeparam.getsize())
I/ Compare single value from each list and copy smallest into result
if (((Comparable)data[i]).compareTo(param.deta(j]) <0)
returnarray.data(k)- this.data(i];
else
returnarray.data[k]=paran.data[j];
returnarray.size k; // set size of return array
return returnarray;
public String tostring() {
String resulte"(";
for (int i - 0; i « size; i++) {
resultte data(i];
if (4 « size - i) resulte", ";
return result. tostring() + "1":
public int getsize() {
return size;
public boolean sortiist() {
E hold;
for (int i- e; 1 < size-1; i+t)
for (int j- 0; jesize-1; j++)
iFccComparable)data( ]). compareTo(data[j+1])>0)
holde datalj+1];
data(j+1]=data[j]:
data(i]-hold;
return true;
Transcribed Image Text:PMyarraylistcE> returnarray - new HyArraylist(); if (this.getsize() --a) // sane as if (sizes-0) return paran; if (paran. getsize()a0) return this; if ((this.getsize()+ param. getsize()) > HAXELEHENTS) throw new IndexoutofBoundsException ("Combined list out of bounds"); Il traverse both list until one list is completely done uhile (icthis.getsize() jeparam.getsize()) I/ Compare single value from each list and copy smallest into result if (((Comparable)data[i]).compareTo(param.deta(j]) <0) returnarray.data(k)- this.data(i]; else returnarray.data[k]=paran.data[j]; returnarray.size k; // set size of return array return returnarray; public String tostring() { String resulte"("; for (int i - 0; i « size; i++) { resultte data(i]; if (4 « size - i) resulte", "; return result. tostring() + "1": public int getsize() { return size; public boolean sortiist() { E hold; for (int i- e; 1 < size-1; i+t) for (int j- 0; jesize-1; j++) iFccComparable)data( ]). compareTo(data[j+1])>0) holde datalj+1]; data(j+1]=data[j]: data(i]-hold; return true;
public class MyArrayList<E>
private int size; // Number of elenents in the list
private E[] data;
private int MAXELEMENTS- 100;
j** Create an empty list */
public MyarrayList() {
data - (E[])new Object[MAXELENENTS];// cannot create array of generics
size - 0; // Number of elements in the list
public void add(int index, E e) {
// Ensure the index is in the right range
if (index <e || index > size)
throw new IndexOutofBoundsException
("Index: "+ index + ", Size: + size);
// Move the elements to the right after the specified index
for (int i- size - 1; i >- index; 1--)
data[i + 1] - data[i];
// Insert new element to data[index]
data[index] - e;
// Increase size by 1
size+t;
public boolean contains (Object e) {
for (int i - o, i< size; i++)
if (e.equals (data[i])) return true;
return false;
public E get(int index) (
if (index < e || index >- size)
throu new IndexOutofBoundsException
("Index: " + index + ", Size: + size);
return data[index];
public E remove(int index) {
if (index <e || index >- size)
throu new IndexOutofBoundsException
("Index: " + index + ", Size: + size);
Ee- data[index];
// Shift data to the left
for (int j - index; 1< size - 1; 1+)
data[j] - data[j + 1]:
data[size - 1] - null; // This element is now null
// Decrement size
size--;
return e;
public void clear()
size - 0;
public MyArrayList<E> merge(yArrayListcE> paran)
int 1-0; //counter in calling array
int jue; // counter in paran array
int k-9; // sounter in return array
Transcribed Image Text:public class MyArrayList<E> private int size; // Number of elenents in the list private E[] data; private int MAXELEMENTS- 100; j** Create an empty list */ public MyarrayList() { data - (E[])new Object[MAXELENENTS];// cannot create array of generics size - 0; // Number of elements in the list public void add(int index, E e) { // Ensure the index is in the right range if (index <e || index > size) throw new IndexOutofBoundsException ("Index: "+ index + ", Size: + size); // Move the elements to the right after the specified index for (int i- size - 1; i >- index; 1--) data[i + 1] - data[i]; // Insert new element to data[index] data[index] - e; // Increase size by 1 size+t; public boolean contains (Object e) { for (int i - o, i< size; i++) if (e.equals (data[i])) return true; return false; public E get(int index) ( if (index < e || index >- size) throu new IndexOutofBoundsException ("Index: " + index + ", Size: + size); return data[index]; public E remove(int index) { if (index <e || index >- size) throu new IndexOutofBoundsException ("Index: " + index + ", Size: + size); Ee- data[index]; // Shift data to the left for (int j - index; 1< size - 1; 1+) data[j] - data[j + 1]: data[size - 1] - null; // This element is now null // Decrement size size--; return e; public void clear() size - 0; public MyArrayList<E> merge(yArrayListcE> paran) int 1-0; //counter in calling array int jue; // counter in paran array int k-9; // sounter in return array
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 2 steps with 2 images

Blurred answer
Knowledge Booster
Array
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
EBK JAVA PROGRAMMING
EBK JAVA PROGRAMMING
Computer Science
ISBN:
9781337671385
Author:
FARRELL
Publisher:
CENGAGE LEARNING - CONSIGNMENT