Question
####I NEED HELP CODING THE AREAS WHERE STUDENT IMPLEMENTATION IS REQUIRED...THIS IS A JAVA ASSIGNMENT CAN SOMEONE HELP??? /** * Determine the number of elements
####I NEED HELP CODING THE AREAS WHERE STUDENT IMPLEMENTATION IS REQUIRED...THIS IS A JAVA ASSIGNMENT CAN SOMEONE HELP??? /** * Determine the number of elements in this bag. * @return * the number of elements in this bag * @post. * the bag is not altered by this method **/ public int size( ) { return used; } /** * Accessor method to count the number of occurrences of a particular element * in this bag. * @param target * the element for which number of occurrences will be counted * @post. * the bag is not altered by this method * @return * the number of times that target occurs in this bag **/ public int occurrences(double target) { int answer = 0; for (int index = 0; index < used; ++index) { if (target == this.data[index]) ++answer; } return answer; }
/** * This method renders the bag's contents into a human readable form * @return * a string representation of the bag * @post. * The bag is not altered by this method **/ @Override public String toString() { // StringBuffer is used since its contents are mutable. Strings are // immutable and, thus, very inefficient for such purpose StringBuffer sb = new StringBuffer(); sb.append("DoubleArrayBag with "); sb.append(this.size()); sb.append(" elements: ["); if ( this.size() == 0 ) { sb.append(" ]"); sb.append(" Capacity: "); sb.append(this.getCapacity()); return sb.toString(); } for (int i=0; i < this.size()-1; ++i) { sb.append(" "); sb.append(this.data[i]); sb.append(","); } sb.append(" "); sb.append(this.data[this.size()-1]); sb.append(" ]"); sb.append(" Capacity: "); sb.append(this.getCapacity()); return sb.toString(); }
/** * reduce capacity of this bag to current size if there is * excess capacity * * @post. * capacity of this bag is reduced to the current number * of items in bag or left unchanged if capacity equals to * number of items in bag but must be at least 1 * * @exception * OutOfMemoryError if not enough dynamic memory to * support allocation and deallocation of memory **/ public void trimToSize( ) { if ( this.used < this.data.length ) { int newCapacity; if ( this.used <= 1 ) newCapacity = 1; else newCapacity = this.used;
// STUDENT WORK GOES HERE } }
/** * Create a new bag that contains all the elements from two other bags -- note that * this is a class method NOT an instance method and must be called with class * name qualifier. * @param b1 * the first of two bags * @param b2 * the second of two bags * @pre. * Neither b1 nor b2 is null, and b1.size() + b2.size() cannot * exceed MAX_CAPACITY * @post. * bag referenced by b1 and bag referenced by b2 are not altered * @return * the union of b1 and b2 * @exception NullPointerException * Indicates that one of the arguments is null. * @exception OutOfMemoryError * Indicates insufficient memory for the new bag. Only happens * when not enough dynamic memory to allocate a new bag of * adequate size. **/ public static DoubleArrayBag union(DoubleArrayBag b1, DoubleArrayBag b2) { if ( b1 == null || b2 == null ) { throw new NullPointerException("one or both bags reference is null"); }
// guarantee that the new bag has exactly the capacity needed to // hold all items from both b1 and b2 and then copy the items from // b1 and then b2 into the new bag
//DoubleArrayBag newBag = null; // = new DoubleArrayBag( ?? ); //STUDENT WORK HERE /* correct below return */ return b1; }
}
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