Access over 20 million homework & study documents

search

6 [18] Assume that you have the following Bag object, myBag, with

Content type
User Generated
Rating
Showing Page:
1/12
6. [18%] Assume that you have the following Bag object,
myBag, with n String data : BagInterface <String> myBag =
new ArrayBag<String>();
Write Java statements that create a newBag object which
contains non-duplicate data in myBag. Example:
If myBag contains data:
hello, Hello, world, hello, james, james
newBag object should contain :
hello, Hello, world, james
Hint: Use the following Bags methods :
Answer:
Solution
public class ArrayBag<T> implements BagInterface<T>
{
private final T[] bag;
private static final int DEFAULT_CAPACITY = 25;
private int numberOfEntries;
public ArrayBag()
{
this(DEFAULT_CAPACITY);
} public ArrayBag(int capacity)
{
numberOfEntries = 0;

Sign up to view the full document!

lock_open Sign Up
Showing Page:
2/12
@SuppressWarnings(\"unchecked\")
T[] tempBag = (T[])new Object[capacity]; // unchecked
cast
bag = tempBag;
// for (int index = 0; index < initialCapacity; index++)
// System.out.print(bag[index] + \" \");
// System.out.println();
} // end constructor
public boolean add(T newEntry)
{
boolean result = true;
if (isFull())
{
result = false;
}
else
{ // assertion: result is true here
bag[numberOfEntries] = newEntry;
numberOfEntries++;
} // end if
return result;
} // end add
public boolean isFull()
{

Sign up to view the full document!

lock_open Sign Up
Showing Page:
3/12

Sign up to view the full document!

lock_open Sign Up
End of Preview - Want to read all 12 pages?
Access Now
Unformatted Attachment Preview
6. [18%] Assume that you have the following Bag object, myBag, with n String data : BagInterface myBag = new ArrayBag(); Write Java statements that create a newBag object which contains non-duplicate data in myBag. Example: If myBag contains data: “hello”, “Hello”, “world”, “hello”, “james”, “james” newBag object should contain : “hello”, “Hello”, “world”, “james” Hint: Use the following Bag’s methods : Answer: Solution public class ArrayBag implements BagInterface { private final T[] bag; private static final int DEFAULT_CAPACITY = 25; private int numberOfEntries; public ArrayBag() { this(DEFAULT_CAPACITY); } public ArrayBag(int capacity) { numberOfEntries = 0; @SuppressWarnings(\"unchecked\") T[] tempBag = (T[])new Object[capacity]; // unchecked cast bag = tempBag; // for (int index = 0; index < initialCapacity; index++) // System.out.print(bag[index] + \" \"); // System.out.println(); } // end constructor public boolean add(T newEntry) { boolean result = true; if (isFull()) { result = false; } else { // assertion: result is true here bag[numberOfEntries] = newEntry; numberOfEntries++; } // end if return result; } // end add public boolean isFull() { return numberOfEntries == bag.length; } // end isFull public T[] toArray() //OK { @SuppressWarnings(\"unchecked\") T[] result = (T[])new Object[numberOfEntries]; // unchecked cast for (int index = 0; index < numberOfEntries; index++) { result[index] = ba ...
Purchase document to see full attachment
User generated content is uploaded by users for the purposes of learning and should be used following Studypool's honor code & terms of service.

Anonymous
Just what I needed…Fantastic!

Studypool
4.7
Trustpilot
4.5
Sitejabber
4.4

Similar Documents