Question
public class DoubleArrayBag implements Cloneable { // Invariant of the DoubleArrayBag class: // 1. The number of elements in the bag is in the instance
public class DoubleArrayBag implements Cloneable {
// Invariant of the DoubleArrayBag class:
// 1. The number of elements in the bag is in the instance variable
// used, which is no more than data.length.
// 2. For an empty bag, we do not care what is stored in any of data;
// for a non-empty bag, the elements in the bag are stored in data[0]
// through data[used-1], and we don't care what's in the
// rest of data.
public static final int MAX_CAPACITY = 4000;
private double[ ] data;
private int used;
/**
* Initialize an empty bag * @post.
* This bag is empty and has a capacity of MAX_CAPACITY.
* @exception OutOfMemoryError
* Indicates insufficient memory for allocating the array
**/
public DoubleArrayBag( ) {
used = 0;
data = new double[MAX_CAPACITY];
}
/**
* Initialize a new bag as an exact copy of source
* @param source
* reference to bag that is to be copied
* @exception NullPointerException
* occurs when source is null
* @post.
* This bag is a copy of source and has a capacity of
* MAX_CAPACITY
**/
public DoubleArrayBag( DoubleArrayBag source ) {
if ( source == null ) {
throw new NullPointerException("source must not be null");
}
//Update this.used and copy over only the used data from source
//into this.data
// STUDENT IMPLEMENTATION OF ABOVE HERE-- consider use of System.arraycopy shown // in text }
/**
* Generate a copy of this bag. * @post. * x.clone() != x * x.clone().getClass() == x.getClass() * x.clone().equals( x ) * * @return * The return value is a copy of this bag. Subsequent changes to the * copy will not affect the original, nor vice versa. * **/ public DoubleArrayBag clone( ) { // Clone an DoubleArrayBag object. DoubleArrayBag answer; try { answer = (DoubleArrayBag) super.clone( ); answer.data = data.clone( ); return answer; } catch (CloneNotSupportedException e) { // This exception should not occur. But if it does, it would probably // indicate a programming error that made super.clone unavailable. // The most common error would be forgetting the "Implements Cloneable" // clause at the start of this class. throw new RuntimeException ("This class does not implement Cloneable"); } } /** * Compare this DoubleArrayBag to another object for equality of value * @param other * reference to another DoubleArrayBag * * @post. * x.equals(x) is true * if x.equals(y) then y.equals(x) * x.equals(null) is false * * @return * true if number of elements in this and other are the same AND if * the values of all the elements in the bag are the same and in the * same position in the bag * * @note. * If the value of other is null, then the return is false **/ @Override public boolean equals(Object other) { if ( other == null ) return false; boolean isEqual = false; if ( other instanceof DoubleArrayBag ) { DoubleArrayBag candidate = (DoubleArrayBag) other; //are we comparing this with this if (this == other) return true; if ( candidate.used != this.used ) return false; int index = 0; isEqual = true; // Inspect data from data[0] through data[used-1] at most // and compare with respective elements of candidate.data // to determine if all are ==. If a pair are not ==, then // set isEqual to false -- which terminates the loop // STUDENT IMPLEMENTATION OF ABOVE HERE } return isEqual; }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started