Hello, This is my Java exercise from last week, I tried to fill in the blanks bu
ID: 3873456 • Letter: H
Question
Hello, This is my Java exercise from last week, I tried to fill in the blanks but always had errors. Please help. Thanks a lot
/** * * * */ public class ArrayBag implements Baggable {
//instance variable bag (array)
// constant capacity = 25
// instance variable number of entries
/** * ArrayBag Constructor */
public ArrayBag(int capacity) { numberOfEntries = 0; T[] tempBag = (T[])new Object[capacity]; bag = tempBag; }
/** * Gets the curent number of entries in this bag. @return this integer number of entries currently in the bag */
public int getCurrentSize() { }
/** * Check whether the bag is full @return true if the bag is full, or false if not */
public boolean isFull() { }
/** * Check whether bag is empty @return true if the bag is empty, or false if not */
public boolean isEmpty() { }
/** * Adds a new entry into this bag @param newEntry the object to be added as a new entry @return true if the addition is successful, or false if not */
public boolean add(T newEntry) { }
/* * a private method to get the index of an item * */
private int getIndexOf(T anEntry) { }
/* * private method to remove an entry. If no such entry then * return null. Otherwise return this entry */
T removeEntry(int givenIndex) { }
/** * Removes on uspecified entry from this bag, if possible * @return either the removed entry, if the removal was successful, or null */
public T remove() { }
/** * Remove one occurrence of a given entry from this bag * @param anEntry the entry to be removed * @return true if the removal was successful, or false if not */
public boolean remove(T anEntry) { }
/** * Remove all entries from this bag */
public void clear() { }
/** * Test whether this bag contains a given entry * @param anEntry * @return true if this bag contains anEntry, or false otherwise */
public boolean contains(T anEntry) { }
/** * Retrieve all entries that are in the bag * @return a newly allocated array of all the entries in this bag */
public T[] toArray() { } }
*******************************
/** * * @author * * @param * This is an interface that describes the operatios of a bag of objects */
public interface Baggable {
/** * Gets the current number of entries in this bag. @return an integer which is the number of entries currently in the bag */
/** * Check whether the bag is full @return true if the bag is full, or false if not */
/** * Check whether bag is empty @return true if the bag is empty, or false if not */
/** * Adds a new entry into this bag @param newEntry the object to be added as a new entry @return true if the addition is successful, or false if not */
/** * Removes on uspecified entry from this bag, if possible * @return either the removed entry, if the removal was successful, or null */
/** * Remove one occurrence of a given entry from this bag * @param anEntry the entry to be removed * @return true if the removal was successful, or false if not */
/** * Remove all entries from this bag */
/** * Test whether this bag contains a given entry * @param anEntry * @return true if this bag contains anEntry, or false otherwise */
/** * Retrieve all entries that are in the bag * @return a newly allocated array of all the entries in this bag */
}
Explanation / Answer
public interface Baggable<T>
{
/** Gets the current number of entries in this bag.
@return the integer number of entries currently in the bag */
public int getCurrentSize();
/** Check whether the bag is full @return true if the bag is full, or false if not*/
public boolean isFull();
/** Check whether bag is empty @return true if the bag is empty, or false if not */
public boolean isEmpty();
/** Adds a new entry to this bag.
@param newEntry the object to be added as a new entry
@return true if the addition is successful, or false if not */
public boolean add(T newEntry);
/** Removes one unspecified entry from this bag, if possible.
@return either the removed entry, if the removal
was successful, or null */
public T remove(T item);
/** Removes one occurrence of a given entry from this bag.
@param anEntry the entry to be removed
@return true if the removal was successful, or false if not */
public boolean remove1(T anEntry);
/** Removes all entries from this bag. */
public void clear();
/** Test whether this bag contains a given entry * @param anEntry *
@return true if this bag contains anEntry, or false otherwise */
public boolean contains(T anEntry);
/** Retrieves all entries that are in the bag.
@return a newly allocated array of all the entries in the bag */
public T[] toArray();
} // end Baggable Interface
----------------------------------------------------------------------------------------------------------------------------------------------
public class ArrayBag<T> implements Baggable<T>{
//instance variable bag (array)
private T[] bag;
// constant capacity = 25
private static final int DEFAULT_CAPACITY=25;
// instance variable number of entries
private int numberOfEntries;
/** * ArrayBag Constructor */
public ArrayBag(int capacity) {
numberOfEntries = 0;
T[] tempBag = (T[])new Object[capacity];
bag = tempBag;
}
/** * Gets the curent number of entries in this bag.
@return this integer number of entries currently in the bag */
public int getCurrentSize(){
return numberOfEntries;
}
/** * Check whether the bag is full @return true if the bag is full,
or false if not */
public boolean isFull() {
return (numberOfEntries == bag.length);
}
/** * Check whether bag is empty @return true if the bag is empty,
or false if not */
public boolean isEmpty() {
return (numberOfEntries == 0);
}
/** * Adds a new entry into this bag @param newEntry the object to be
added as a new entry @return true if the addition is successful,
or false if not */
public boolean add(T newEntry) {
boolean res =true;
if (isFull())
{
res =false;
}
else
{
bag[numberOfEntries]=newEntry;
numberOfEntries++;
}
return res;
}
/* * a private method to get the index of an item * */
private int getIndexOf(T anEntry) {
int index =-1;
for ( index=0; index<numberOfEntries; index++)
{
if (bag[index].equals(anEntry))
return index;
}
return index;
}
/* * private method to remove an entry. If no such entry then
return null. Otherwise return this entry */
private T removeEntry(int givenIndex) {
T res =null;
if (!isEmpty() && (givenIndex >=0))
{
res =bag[givenIndex];
numberOfEntries--;
bag[givenIndex] =bag[numberOfEntries];
bag[numberOfEntries]=null;
}
return res;
}
/** * Removes on uspecified entry from this bag, if possible
@return either the removed entry, if the removal was successful,
or null */
public T remove(T item) {
int index =getIndexOf(item);
T result =removeEntry(index);
return result;
}
/** * Remove one occurrence of a given entry from this bag
@param anEntry the entry to be removed
@return true if the removal was successful, or false if not */
public boolean remove1(T anEntry) {
for (int i = 0; i < numberOfEntries; i++) {
if (bag[i].equals(anEntry)) {
// Shift the remaining items left by one.
for (int j = i; j < numberOfEntries - 1; j++)
bag[j] = bag[j + 1];
bag[numberOfEntries - 1] = null;
numberOfEntries--;
return true;
}
}
return false; // if not found
}
/** * Remove all entries from this bag */
public void clear() {
if (!isEmpty()){
numberOfEntries=0;
bag=null;
}
}
/** * Test whether this bag contains a given entry
@param anEntry
@return true if this bag contains anEntry, or false otherwise */
public boolean contains(T anEntry) {
boolean found =false;
for (int index =0; !found && (index<numberOfEntries); index++)
{
if (anEntry.equals(bag[index]))
found =true;
}
return found;
}
/** * Retrieve all entries that are in the bag
@return a newly allocated array of all the entries in this bag */
public T[] toArray() {
@SuppressWarnings("unchecked")
T[] res =(T[])new Object[numberOfEntries];
for (int index=0; index<numberOfEntries; index++)
{
res[index]=bag[index];
}
return res;
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.