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(); /** Sees whether this 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(); /** 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
remove(T anEntry); /** Removes all entries from this bag. */ public void
clear(); /** Counts the number of times a given entry appears in this bag.
@param anEntry The entry to be counted. @return The number of
times anEntry appears in the bag. */ public int getFrequencyOf(T anEntry);
/** Tests whether this bag contains a given entry. @param anEntry
The entry to locate. @return True if the bag contains anEntry, or false
if not. */ public boolean contains(T anEntry); /** Retrieves all entries
that are in this bag. @return A newly allocated array of all the entries
in the bag. Note: If the bag is empty, the returned array is empty.
*/ public T[] toArray();// public <T> T[] toArray(); // Alternate// public
Object[] toArray(); // Alternate /** Creates a new bag that combines the
contents of this bag and anotherBag. @param anotherBag The bag that is to
be added. @return A combined bag. */// public BagInterface<T>
union(BagInterface<T> anotherBag); /** Creates a new bag that contains those
objects that occur in both this bag and anotherBag. @param anotherBag
The bag that is to be compared. @return A combined bag. */// public
BagInterface<T> intersection(BagInterface<T> anotherBag); /** Creates a new bag of
objects that would be left in this bag after removing those that also occur
in anotherBag. @param anotherBag The bag that is to be removed. @return
A combined bag. */// public BagInterface<T> difference(BagInterface<T>
anotherBag);} // end BagInterface
Please find the source code below. It has three files: LinkedBag.java - an implementation of the given BagInterface based on LinkedList, Util.java - contains some utility methods and Project1.java - Main class to test the files
/** LinkedBag.java **/
public class LinkedBag<T> implements BagInterface<T> {
private Node head;
private int length;
public LinkedBag() {
this.head = null;
this.length = 0;
}
@Override
public int getCurrentSize() {
return this.length;
}
@Override
public boolean isEmpty() {
return this.length == 0;
}
@Override
public boolean add(T newEntry) {
Node newNode = new Node(newEntry);
newNode.next = head;
head = newNode;
this.length++;
return true;
}
@Override
public T remove() {
T result = null;
if(this.head != null) {
result = this.head.data;
this.head = this.head.next;
this.length --;
}
return result;
}
@Override
public boolean remove(T anEntry) {
boolean res = false;
Node node = searchFor(anEntry);
if (node != null) {
node.data = this.head.data;
this.head = this.head.next;
this.length --;
res = true;
}
return res;
}
@Override
public void clear() {
while (!isEmpty()) {
remove();
}
}
@Override
public int getFrequencyOf(T anEntry) {
int frequency = 0;
int count = 0;
Node cur = this.head;
while (count < this.length && cur != null) {
if (anEntry.equals(cur.data)) {
frequency ++;
}
count ++;
cur = cur.next;
}
return frequency;
}
@Override
public boolean contains(T anEntry) {
Node toSearch = searchFor(anEntry);
return toSearch != null;
}
@Override
public T[] toArray() {
T[] array = (T[]) new Object[this.length];
Node cur = this.head;
for (int i=0; i<this.length && cur != null; i++) {
array[i] = cur.data;
cur = cur.next;
}
return array;
}
@Override
public BagInterface<T> union(BagInterface<T> anotherBag) {
T[] thisBag = toArray();
T[] otherBag = anotherBag.toArray();
T[] latest = Util.union(thisBag, otherBag);
return LinkedBag.fromArray(latest);
}
@Override
public BagInterface<T> intersection(BagInterface<T> anotherBag) {
return LinkedBag.fromArray(Util.intesection(this.toArray(), anotherBag.toArray()));
}
@Override
public BagInterface<T> difference(BagInterface<T> anotherBag) {
return LinkedBag.fromArray(Util.difference(this.toArray(), anotherBag.toArray()));
}
public static <T> LinkedBag<T> fromArray(T[] array) {
LinkedBag<T> linkedBag = new LinkedBag<>();
for(int i=0; i<array.length; i++) {
linkedBag.add(array[i]);
}
return linkedBag;
}
public void print() {
Node cur = this.head;
while (cur != null) {
System.out.print(cur.data + "->");
cur = cur.next;
}
System.out.print("null");
System.out.println();
}
private Node searchFor(T toSearch) {
boolean found = false;
Node cur = this.head;
while (!found && (cur != null)) {
if (toSearch.equals(cur.data)) {
found = true;
} else {
cur = cur.next;
}
}
return cur;
}
private class Node {
private T data;
private Node next;
public Node(T data) {
this(data, null);
}
public Node(T data, Node next) {
this.data = data;
this.next = next;
}
}
}
/** Util.java **/
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class Util {
public static <T> T[] union(T[] a, T[] b) {
ArrayList<T> l1 = new ArrayList<>(Arrays.asList(a));
ArrayList<T> l2 = new ArrayList<>(Arrays.asList(b));
l1.addAll(l2);
return l1.toArray((T[]) new Object[l1.size()]);
}
public static <T> T[] intesection(T[] a, T[] b) {
Set<T> res = new HashSet<>();
ArrayList<T> f = new ArrayList<>(Arrays.asList(a));
ArrayList<T> s = new ArrayList<>(Arrays.asList(b));
res = Stream.concat(f.stream(), s.stream())
.filter(f::contains)
.filter(s::contains)
.collect(Collectors.toSet());
return res.toArray((T[]) new Object[res.size()]);
}
public static <T> T[] difference(T[] a, T[] b) {
ArrayList<T> l1 = new ArrayList<>(Arrays.asList(a));
ArrayList<T> l2 = new ArrayList<>(Arrays.asList(b));
l1.removeAll(l2);
return l1.toArray((T[]) new Object[l1.size()]);
}
}
/** Project1.java **/
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class Project1 {
public static void main(String[] args) {
List<Integer> list = new ArrayList<>(Arrays.asList(1,2,3,4,5,6,6,7,8,9,9,10));
LinkedBag<Integer> linkedBag = LinkedBag.fromArray(list.toArray(new Integer[0]));
linkedBag.print();
System.out.println("Current Size is: " + linkedBag.getCurrentSize());
System.out.println("Frequency of element 9 is " + linkedBag.getFrequencyOf(9));
System.out.println("Removing an unspecified element; remove()");
linkedBag.remove();
linkedBag.print();
System.out.println("Removing an element; remove(9)");
linkedBag.remove(9);
linkedBag.print();
LinkedBag<Integer> bag2 = LinkedBag.fromArray(new ArrayList<>(Arrays.asList(4,5,6,99,10)).toArray(new Integer[5]));
LinkedBag<Integer> bag3 = LinkedBag.fromArray(new ArrayList<>(Arrays.asList(6,8,9,10)).toArray(new Integer[4]));
System.out.println("Union of bag 2 and original bag: ");
((LinkedBag<Integer>)linkedBag.union(bag2)).print();
System.out.println("Difference of bag 3 and original bag: ");
((LinkedBag<Integer>)linkedBag.difference(bag3)).print();
System.out.println("Intersection of bag 2 and original bag: ");
((LinkedBag<Integer>)linkedBag.intersection(bag2)).print();
}
}
Write a program that thoroughly tests the class LinkedBag. Write the "LinkedBag" class, and a main...
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...
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...
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...
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...
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...
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...
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();
Problem 3 (List Implementation) (35 points): Write a method in the DoublyLList class that deletes the first item containing a given value from a doubly linked list. The header of the method is as follows: public boolean removeValue(T aValue) where T is the general type of the objects in the list and the methods returns true if such an item is found and deleted. Include testing of the method in a main method of the DoublyLList class. ------------------------------------------------------------------------------------- /** A...
c++ Please create Set.cpp and Set.h The following information is needed (setinterface.h) Class Set You are given an interface: SetInterfac This is a public interface and completely specifies what the Set class operations must be. SetInterface is an abstract class (it has no implementation), so your Set class must inherit from SetInterface and implement all of its methods. Set differs in the fact that it does not allow duplicates. This also means that add()must check that an element is not...
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...