Question: 1 . ( 6 points ) Please add a method for performing insertion sort on the buffer content to your RealBuff class in Assignment 1
points Please add a method for performing insertion sort on the buffer content to your RealBuff class in
Assignment Notice that the method should only sort the valid elements and the unused slots are ignored.
points Please write a main method in another class file to perform the following tests on your new methods.
Creating an object of RealBuff.
point Using the Java Random class to generate random double values and appending them to the buffer
object. You do not need to exclude the duplicate values.
Displaying the content of the buffer.
point Sorting the numbers in the buffer using insertion sort.
points Displaying the content of the buffer, to see if the numbers are sorted.
This is my code
public class RealBuff
static final int maxsize ; the default maximum size of the buffer
double content; the content of the buffer
int currentsize; the number of valid elements
public RealBuff initialize an empty buffer of the default maximum size
initializing the array with the capacity of maxsize
content new doublemaxsize;
currentsize ;
initializing an empty buffer of the maximum size given by n
public RealBufffinal int n
initializing the array with capacity of n
content new doublen;
currentsize ;
initializing a buff which is a copy of buff
public RealBufffinal RealBuff buff
initializing the array with capacity of buff.content.length
content new doublebuffcontent.length;
copying the values from buff.content to this.content
for int i ; i buff.currentsize; i
contenti buff.contenti;
currentsize buff.currentsize;
creating a method to append a given double value to the end of the buffer
public void appenddouble value
adding a value to the index: currentsize and incrementing currentsize, only if currentsize && pos currentsize
shifting the elements starting from pos to one place to the right
for int i currentsize; i pos; i
contenti contenti ;
adding a value at pos index and incrementing currentsize
contentpos value;
currentsize;
return true; operation is a success
return false; not enough space or an invalid index
creating a method to delete the number in the given position
public boolean deleteint pos
validating the index
if pos && pos currentsize
shifting all elements that are to the right of pos to one place to the left
for int i pos; i currentsize ; i
contenti contenti ;
updating currentsize
currentsize;
return true; operation is a success
return false; the index is invalid or the buffer is empty
creating a method to display all valid elements
public void display
System.out.print;
for int i ; i currentsize; i
System.out.printcontenti;
printing a comma and space if this is not the last element
if i currentsize
System.out.print;
System.out.println;
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
