![Assume that you have the following Bag object, myBag, with n String data: Baginterface <String> myBag new ArrayBag< >( Write Java statements that create a newBag object which contains non-duplicate data in myBag and marks the duplicate data. Example: if myBag contains data Hn newBag object should contain: aa, Аа, СС, Bb, DUP. 1.Bb, DUP.2.Aa, D, Hint: You can use the Bags methods: int getCurrentSize (0; boolean isFull (0; boolean isEmpty (0: boolean add (T newEntry); T remove (0; boolean remove (T anEntry): void clear (0; int getFrequencyOf (T anEntry); boolean contains (T anEntry); T [] toArray (0:](http://img.homeworklib.com/questions/43c93ff0-8540-11eb-b452-db0d635345b7.png?x-oss-process=image/resize,w_560)
I don't understand the question. Could someone solve and explain it please?
ANSWER:
THE QUESTION IS ABOUT TO REMOVE THE DUPLICATE DATA IN THE BAG
CODE:
import java.util.*;
/* class BagTest */
public class BagTest
{
/* main module */
public static void main(String[] args)
{
/* object creation */
ArrayList<String> mybag = new ArrayList<String>();
/* adding String to Bag Array */
mybag.add("hello");
mybag.add("hello");
mybag.add("world");
mybag.add("hello");
mybag.add("james");
mybag.add("james");
/* before removal */
System.out.println("Initial Array:"+mybag);
/* removing duplicates */
for(int inc1=0;inc1<mybag.size();inc1++)
{
for(int inc2=inc1+1;inc2<mybag.size();inc2++)
{
if(mybag.get(inc1).equals(mybag.get(inc2)))
{
mybag.remove(inc2);
inc2--;
}
}
}
/* after removal */
System.out.println("Output Array:"+mybag);
}
}
(OR)
CODE:
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;
@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] =
bag[index];
} // end for
return result;
} // end toArray
public boolean isEmpty()
{
return numberOfEntries == 0;
} // end isEmpty
public int getCurrentSize()
{
return numberOfEntries;
} // end getCurrentSize
public int getFrequencyOf(T anEntry)
{
int counter = 0;
for (int index = 0; index <
numberOfEntries; index++)
{
if
(anEntry.equals(bag[index]))
{
counter++;
} // end
if
} // end for
return counter;
} // end getFrequencyOf
public boolean contains(T anEntry)
{
return getIndexOf(anEntry) > -1;
// or >= 0
} // end contains
private int getIndexOf(T anEntry)
{
int where = -1;
boolean found = false;
for (int index = 0; !found
&& (index < numberOfEntries); index++)
{
if
(anEntry.equals(bag[index]))
{
found = true;
where = index;
} // end
if
} // end for
// Assertion: If where > -1, anEntry is in the array bag, and
it
// equals bag[where]; otherwise, anEntry is not in the array
return where;
} // end getIndexOf
/** Removes all entries from this bag. */
public void clear()
{
while (!isEmpty())
remove();
} // end clear
public T remove()
{
T result =
removeEntry(numberOfEntries - 1);
return result;
} // end remove
public boolean remove(T anEntry)
{
int index =
getIndexOf(anEntry);
T result = removeEntry(index);
return
anEntry.equals(result);
} // end remove
private T removeEntry(int givenIndex)
{
T result = null;
if (!isEmpty() &&
(givenIndex >= 0))
{
result = bag[givenIndex]; // entry to remove
numberOfEntries--;
bag[givenIndex] = bag[numberOfEntries]; // replace entry to remove
with last entry
bag[numberOfEntries] = null; // remove reference to last
entry
} // end if
return result;
} // end removeEntry
} // end ArrayBag
TestBag:
import junit.framework.TestCase;
/**
* A JUnit test case class.
* Every method starting with the word "test" will be called when
running
* the test with JUnit.
*/
public class TestBag extends TestCase {
/**
* A test method.
* (Replace "X" with a name describing the test. You may write
as
* many "testSomething" methods in this class as you wish, and
each
* one will be called when running JUnit over this class.)
*/
public void testIsFull() {
BagInterface<String> bag = new
ArrayBag<String>(3);
assertFalse(bag.isFull());
bag.add("app");
assertFalse(bag.isFull());
bag.add("ban");
bag.add("cherry");
assertTrue(bag.isFull());
}
public void testAdd() {
BagInterface<String> bag = new
ArrayBag<String>(3);
assertTrue(bag.add("app"));
assertTrue(bag.add("ban"));
assertTrue(bag.add("che"));
assertFalse(bag.add("don"));
}
public void testGetCurrentSize() {
BagInterface<String> bag = new
ArrayBag<String>();
assertEquals(0,bag.getCurrentSize());
assertTrue(bag.add("app"));
assertEquals(1,bag.getCurrentSize());
}
public void testIsEmpty() {
BagInterface<String> bag = new
ArrayBag<String>();
assertTrue(bag.isEmpty());
assertTrue(bag.add("app"));
assertFalse(bag.isEmpty());
}
public void testClear() {
BagInterface<String> bag = new
ArrayBag<String>();
assertTrue(bag.add("app"));
assertTrue(bag.add("ban"));
assertTrue(bag.add("che"));
assertTrue(bag.add("don"));
assertFalse(bag.isEmpty());
// nothing to assert as clear() returns void
bag.clear();
assertTrue(bag.isEmpty());
}
public void testContains() {
BagInterface<String> bag = new
ArrayBag<String>();
assertTrue(bag.add("app"));
assertTrue(bag.add("app"));
assertFalse(bag.contains("ban"));
}
// remove(T)
public void testRemoveParam() {
BagInterface<String> bag = new
ArrayBag<String>();
assertFalse(bag.remove("app"));
assertTrue(bag.add("app"));
assertTrue(bag.remove("app"));
}
// remove()
public void testRemove() {
BagInterface<String> bag = new
ArrayBag<String>();
assertNull(bag.remove());
/**
* If we only have app in the bag
* that should be the item that is removed.
*/
assertTrue(bag.add("app"));
assertEquals("app",bag.remove());
}
public void getFrequencyOf() {
BagInterface<String> bag = new
ArrayBag<String>();
assertEquals(0,bag.getFrequencyOf("app"));
assertTrue(bag.add("app"));
assertEquals(1,bag.getFrequencyOf("app"));
for (int i = 0; i < 5; i++)
assertTrue(bag.add("ban"));
assertEquals(5,bag.getFrequencyOf("ban"));
}
}
PLZZZZZZZZZZZZZZZZZZ RATE THUMBSUP PLZZZZZZZ
I don't understand the question. Could someone solve and explain it please? Assume that you have...
Create a java class that implements BagInterface using singly linked data. Name this new class LinkedBag. Be sure to include a default constructor to initialize the private members of the class. Test that your code works properly. BagInterface includes the methods: public int getCurrentSize(); public boolean isEmpty(); public boolean add(T newEntry); public T remove(); public boolean remove(T anEntry); public void clear(); public int getFrequencyOf(T anEntry); public boolean contains(T anEntry); public T[] toArray();
Write a complete bag class implementation using linked implementation. The linked bag class name must be LinkedBag and name your test program as LinkedBagDemo. Your test program should include following test conditions: 1. Get the number of items currently in the bag 2. See whether the bag is full 3. See whether the bag is empty 4. Add a given object to the bag 5. Remove an unspecified (not random) object from the bag 6. Remove an occurrence of a...
Study the provided BagInterface and the methods implemented in the ResizableArrayBag For the following methods that you are to implement in ResizableArrayBag for Project3 of Lab02: equals, removeMax, removeEvery, replace, union, intersection, difference, getAllLessThan, isSubset give an algorithm in pseudocode draw memory diagrams showing how the respective bags will look like as the result of the given method call public interface BagInterface<T extends Comparable<? super T>> { public int getCurrentSize(); public boolean isEmpty(); public boolean add(T newEntry); public T remove(); public...
Write a program that thoroughly tests the class LinkedBag. Write the "LinkedBag" class, and a main program named "Project1.java" testing the methods defined for the LinkedBag object. The interface file: BagInterface.java. /** An interface that describes the operations of a bag of objects. @author Frank M. Carrano @author Timothy M. Henry @version 4.0*/public interface BagInterface<T>{ /** Gets the current number of entries in this bag. @return The integer number of entries currently in the bag. */ public int getCurrentSize(); /**...
Suppose that you have several numbered billiard balls on a pool table. The smallest possible number on the ball is “1”. At each step, you remove a billiard ball from the table. If the ball removed is numbered n, you replace it with n balls randomly numbered less than n. For example, if you remove the “5” ball, you replace it with balls numbered “2”, “1”, “1”, “4”, and “3”, where numbers 2, 1, 1, 4, and 3 were randomly...
Java Write an intersection method for the ResizableArrayBag class. The intersection of two bags is the overlapping content of the bags. Intersections are explained in more detail in Chapter 1, #6. An intersecion might contain duplicates. The method should not alter either bag. The current bag and the bag sent in as a parameter should be the same when the method ends. The method header is: public BagInterface<T> intersection(ResizableArrayBag <T> anotherBag) Example: bag1 contains (1, 2, 2, 3) bag2 contains...
In this lab, we will implement the Linked Bag. The bag will contain a sequence of strings. First, you need to design the Node class (Node.java). It will contain an integer data and a reference to thenext Node. The constructor of Node class receives an integer and assigns it to the data field. It also has a default constructor. Data Next Node Then we will design another class named LinkedBag (LinkedBag.java). LinkedBag class has an instance variable called firstNode of...
Can you help with the merge method? This method should create and return an ArrayBagcontaining one occurrence of any item that is found in either the called object or the parameter other. For full credit, the resulting bag should not include any duplicates. Give the new ArrayBag a maximum size that is the sum of the two bag’s maximum sizes. import java.util.*; /** * An implementation of a bag data structure using an array. */ public class ArrayBag { /**...
Load to the IDEA the remaining classes from the provided Lab02.zip file. Repeat the previous project inside the ArraySetWithArray class. As shown in the UML diagram below ArraySetWithArray class does not utilize ResizableArrayBag object as its instance variable, it has setOfEntries defined as an array which should be dynamically resized if more room needed (double the size). displaySet method should check if the set is empty and display appropriate message; if the set is not empty should display the number...
A set is a special kind of list, one that does not allow repeated, or duplicate, entries. Whenever you must process an item in a data collection only once, you can use a set. For example, a compiler must find the identifiers in a program and ensure that each one has been defined only once. It could add each identifier encountered to a set. If this addition is unsuccessful, the compiler will have detected an identifier previously found. Remark: There...