Question: 1 . ( 6 points ) Please add a method for performing insertion sort on the buffer content to your RealBuff class in Assignment 1

1.(6 points) Please add a method for performing insertion sort on the buffer content to your RealBuff class in
Assignment 1. Notice that the method should only sort the valid elements and the unused slots are ignored.
2.(4 points) Please write a main method (in another class file) to perform the following tests on your new methods.
1) Creating an object of RealBuff.
2)(1 point) Using the Java Random class to generate 10 random double values and appending them to the buffer
object. You do not need to exclude the duplicate values.
3) Displaying the content of the buffer.
4)(1 point) Sorting the numbers in the buffer using insertion sort.
5)(2 points) Displaying the content of the buffer, to see if the numbers are sorted.
This is my code
public class RealBuff {
static final int max_size =100; // the default maximum size of the buffer
double[] content; // the content of the buffer
int current_size; // the number of valid elements
public RealBuff()// initialize an empty buffer of the default maximum size
{
// initializing the array with the capacity of max_size
content = new double[max_size];
current_size =0;
}
// initializing an empty buffer of the maximum size given by n
public RealBuff(final int n){
// initializing the array with capacity of n
content = new double[n];
current_size =0;
}
// initializing a buff which is a copy of buff
public RealBuff(final RealBuff buff)
{
// initializing the array with capacity of buff.content.length
content = new double[buff.content.length];
// copying the values from buff.content to this.content
for (int i =0; i < buff.current_size; i++){
content[i]= buff.content[i];
}
current_size = buff.current_size;
}
// creating a method to append a given double value to the end of the buffer
public void append(double value){
// adding a value to the index: current_size and incrementing current_size, only if current_size=0 && pos <= current_size){
// shifting the elements starting from pos to one place to the right
for (int i = current_size; i > pos; i--)
content[i]= content[i -1];
// adding a value at pos index and incrementing current_size
content[pos]= value;
current_size++;
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 delete(int pos){
// validating the index
if (pos >=0 && pos < current_size){
// shifting all elements that are to the right of pos to one place to the left
for (int i = pos; i < current_size -1; i++)
content[i]= content[i +1];
// updating current_size
current_size--;
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 =0; i < current_size; i++){
System.out.print(content[i]);
//printing a comma and space if this is not the last element
if (i != current_size -1)
System.out.print(",");
}
System.out.println("]");
}
}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!