Consider the following class, which is somewhat inspired by the homework:
public class DoubleBox {
// An array to store doubles
private double[] arr;
// The total number of doubles actually stored in arr
private int numDoubles;
public DoubleBox() {
this.arr = new double[10];
this.numDoubles = 0;
}
// The append method (Part A) will go here
// The sum method (Part B) will go here
// The new constructor (Part C) will go here
// A main method (Part D) will go here
}
Write a new method for the DoubleBox
class: append
. It is described below.
/**
* The append method.
*
* Append a given double onto the end of arr. If arr is already full, then do
* nothing. The numDoubles instance variable should be updated accordingly.
*
* @param val The double to append.
* @return true if a value is appended and false otherwise.
*/
Write a new method for the DoubleBox
class: sum
. It is described below.
/**
* The sum method.
*
* @return The sum of the relevant items in arr.
*/
Write another constructor for the DoubleBox
class. It is described below.
/**
* This constructor should allocate the instance variable arr to be twice as
* large as the argument inp and then place each and every double from inp into
* arr. The numDoubles instance variable should be updated appropriately.
*
* @param inp An array of doubles to be added to arr.
*/
main
For Testing [5 pts]Now let’s partially test our class. Write a main
method for the DoubleBox
class that does the following:
DoubleBox
object.1.5
to the object.1.8
to the object.sum
method on the object.