Write a UniqueBag container. This is a lot like a Bag in that it supports add() and Iteration, except for one important thing: if you try to add a value that’s already in the UniqueBag, the value won’t be added. No error will be thrown, but the add() method will have no effect. So, if we add 1, 3, 2, 1, 4, 2 and 3, the UniqueBag will only contain the values 1, 2, 3 and 4.
Use the code for Bag in the book as your starting point. Be sure to implement the Generic mechanism. Also, here’s a tip: you probably need to compare values in your UniqueBag class, so it won’t work with any possible type of Java Objects – the values must be things that can be compared (so, your class should start out something like public class UniqueBag>). In the main routine of UniqueBag ( public static void main(String[] args) ), you should create a couple of UniqueBag objects for different types of data – maybe one for ints and one for Strings – and test them out.
*** Here is the Bag.java Below ***
import java.util.Iterator;
public class Bag<Item> implements Iterable<Item> {
private Node first; // first node in list
private class Node {
Item item;
Node next;
}
public void add(Item item) { // same as push() in Stack
Node oldfirst = first;
first = new Node();
first.item = item;
first.next = oldfirst;
}
public Iterator<Item> iterator() {
return new ListIterator();
}
private class ListIterator implements Iterator<Item> {
private Node current = first;
public boolean hasNext() {
return current != null;
}
public void remove() { }
public Item next() {
Item item = current.item;
current = current.next;
return item;
}
}
}
public class UniqueBag<Item extends Comparable<Item>> extends Bag<Item> {
public void add(Item item) {
for(Item it : this) {
if(it.compareTo(item) == 0) {
return;
}
}
super.add(item);
}
}

public class UniqueBagTest {
public static void main(String[] args) {
UniqueBag<Integer> bag1 = new UniqueBag<>();
bag1.add(4);
bag1.add(2);
bag1.add(4);
bag1.add(1);
for(Integer it : bag1) {
System.out.print(it + " ");
}
System.out.println();
UniqueBag<String> bag2 = new UniqueBag<>();
bag2.add("How");
bag2.add("are");
bag2.add("How");
bag2.add("you?");
for(String it : bag2) {
System.out.print(it + " ");
}
System.out.println();
}
}

Write a UniqueBag container. This is a lot like a Bag in that it supports add()...
Java: Return an array of booleans in a directed graph. Please complete the TODO section in the mark(int s) function import algs13.Bag; import java.util.HashSet; // See instructions below public class MyDigraph { static class Node { private String key; private Bag<Node> adj; public Node (String key) { this.key = key; this.adj = new Bag<> (); } public String toString () { return key; } public void addEdgeTo (Node n) { adj.add (n); } public Bag<Node> adj () { return adj;...
Improve the speed of public T get(int i) by having it work backward from the end of the array if you attempt to get a member which is closer to the end than the start. This improvement will be tested through timing tests on large lists. The method should still return null if i is not a valid index. Code import java.util.Iterator; public class DLList<T> implements Iterable<T> { private static class Node<T> { public Node<T> prev, next;...
Complete P16.1 and P16.4 (Class Name: NewMethodDemo) Once complete, upload all .java files. For primary test class: Remember your header with name, date, and assignment. Also include class names that will be tested. Psuedocode (level 0 or mixture of level 0 and algorithm [do not number steps]) is required if main() contains more than simple statements (for example, your program includes constructs for decisions (if/else), loops, and methods. For Secondary class(es): Include a JavaDoc comment that describes the purpose of...
JAVA Have not gotten the public Iterator<Item> iterator() . Can someone explain it please? import java.util.Iterator; /* * GroupsQueue class supporting addition and removal of items * with respect to a given number of priorities and with * respect to the FIFO (first-in first-out) order for items * with the same priority. * * An example, where GroupsQueue would be useful is the airline * boarding process: every passenger gets assigned a priority, * usually a number, e.g., group 1,...
I have a Java Data Structures project and we're creating a linked list and an iterator for that linked list. The add method for the linked list has to be O(1). This is my code public class MyLinkedList extends CS20bLinkedList implements Iterable<Integer> { @Override public void add(int value){ Node tail = head; if(head == null){ head = new Node(value); //System.out.println("Very Fun! "+head.value); return; }else{ ...
Please help with the codes for the implementation of java.util.List, specifically for the 3 below overridden methods @Override public int lastIndexOf(Object arg0) { required code } @Override public ListIterator<R> listIterator() { required code } @Override public ListIterator<R> listIterator(int arg0) { required code } PLEASE NOTE: Below are some of other overridden methods that are already implemented, they are meant to be examples of what the answers to the above questions for the 3 methods should...
Java help: Please help complete the locate method that is in bold.. public class LinkedDoubleEndedList implements DoubleEndedList { private Node front; // first node in list private Node rear; // last node in list private int size; // number of elements in list ////////////////////////////////////////////////// // YOU MUST IMPLEMENT THE LOCATE METHOD BELOW // ////////////////////////////////////////////////// /** * Returns the position of the node containing the given value, where * the front node is at position zero and the rear node is...
Consider the following class definition class FibSequence implements Iterablexnteger public Iterator<Integer> iterator) return new FibIterator); private class FibIterator implements Iterator<Integer> f //complete this You are required to create an inner class in the FibSequence class. The inner class should be called FibIterator. It implements the Iterator<Integer> interface and implements the hasNext() and the next() methods. The iterator iterates through the Fibonacci sequence. The Fibonacci sequence is a series of numbers where a number is the sum of previous two numbers....
Requirements Print a range Write a bag member function with two parameters. The two parameters are Items x and y. The function should write to the console all Items in the bag that are between the first occurrence of x and the first occurrence of y. You may assume that items can be compared for equality using ==. Use the following header for the function: void print_value_range(const Item& x, const Item& y); print_value_range can be interpreted in a number of...
Need help with fixing "TreeMap cannot be resolved to a type" error in code. This was posted in another chegg question but has errors. Was wondering if someone can please fix this. Link to the question asked: https://www.chegg.com/homework-help/questions-and-answers/using-java-import-javautiliterator-import-javautilnosuchelementexception-public-class-pric-q44594716?trackid=undefined I have written the updated code (changes highlighted with green color). The idea is to add a new treemap to priceQueue class to be able to satisfy the asked requirements. Now, if we look at the changes we will need to update...