Hello this is java in the Eclipse software, i\'ve already done the assignment bu
ID: 3749490 • Letter: H
Question
Hello this is java in the Eclipse software, i've already done the assignment but I lost points due to it not being optimized correctly.
Specifically: "*need to check valid indexes for addArray(). *directions stated to write for loop in findLast() starting at the end and decrementing i."
If someone could fix my code that would be awesome. Here are the instructions for the assignment:
This is the optimal code:
This is my code:
public class BufferOfDoubles {
private double[] arrayOfValues;
private int numberOfValues;
public BufferOfDoubles(int size)
{
arrayOfValues = new double[size];
numberOfValues = 0;
}
public boolean add(double value)
{
if(numberOfValues <= arrayOfValues.length-1)
{
arrayOfValues[numberOfValues] = value;
numberOfValues++;
return true;
}
else
return false;
}
public int addArray(double[] array, int firstIndex, int lastIndex)
{
int valueAdded = 0;
if(firstIndex < lastIndex && firstIndex < array.length-1
&& lastIndex <= array.length-1)
{
for(int i = firstIndex; i <= lastIndex; i++)
{
boolean isAdded = this.add(array[i]);
if(isAdded)
valueAdded++;
}
}
return valueAdded;
}
public int findFirst(double value)
{
for(int i = 0; i < numberOfValues; i++)
{
if(arrayOfValues[i] == value)
{
return i;
}
}
return -1;
}
public int findLast(double value)
{
int index = -1;
for(int i = 0; i < numberOfValues; i++)
{
if(arrayOfValues[i] == value)
{
index = i;
}
}
return index;
}
public boolean removeStable(int index)
{
if( numberOfValues == 0 || index >= numberOfValues || index < 0)
{
return false;
}
numberOfValues = numberOfValues - 1;
double[] newArray = new double[arrayOfValues.length];
int newArray_index = 0;
for(int i = 0; i < numberOfValues + 1; i++)
{
if(i != index)
{
newArray[newArray_index] = arrayOfValues[i];
newArray_index++;
}
}
arrayOfValues = newArray;
return true;
}
public boolean removeQuick(int index)
{
if(index < numberOfValues && index >= 0)
{
arrayOfValues[index] = arrayOfValues[numberOfValues - 1];
numberOfValues = numberOfValues - 1;
return true;
}
return false;
}
public void clear()
{
numberOfValues = 0;
}
public void display()
{
for(int i = 0; i < numberOfValues; i++)
System.out.printf("%.2f ", arrayOfValues[i]);
System.out.println();
}
public void displayAsTable(int columnPerRow)
{
for(int i = 0; i < numberOfValues; i++)
{
if( (i > 0) && (i % columnPerRow == 0) )
System.out.println();
System.out.printf("%8.3f", arrayOfValues[i]);
}
System.out.println();
}
}
It outputs everything normally, but if someone can make the edits and optimizations my instructor wants I would really appreciate it. Thanks!
An Implementation of the Array Buffer Implement a Java class called BufferOfDoubles. A skeleton of the class is provided below public class BufferOfDoubles private double[] arrayofValues; private int numberofValues; public BufferOfDoubles (int size) f public boolean add(double value) ( return false; public int addArray(double[] array, int firstIndex, int lastIndex) return 0; public int findFirst(double value) return-1; public int findLast(double value) return-1; public boolean removeStable(int index) return false; public boolean removeQuick (int index) { return false; public void clear) public void display() ( public void displayAsTable(int columnsPerRow) A class for testing your homework, called HW1, is also provided (at the end of this document) When run on the above empty implementation of the class, the output should be as shown here testing findFirst: -1 testing findLast:-1 testing removeStable: false testing removeQuick: false testing remove: falseExplanation / Answer
Now all the functions contain specfied number of lines or less
Please Note :: I have not altered the flow of the program. Just removed redundant lines and braces
CODE
Please comment for any further assistance
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.