Question

In: Computer Science

Create a method that gets the difference for a linked bag and a resizable array: Also,...

Create a method that gets the difference for a linked bag and a resizable array:

Also, implement the methods in the interface and test out that the program works in the main class

//please comment what you are doing for the difference method for the linked bag and the array bag (also explain its time complexity in terms of Big O

EX: Bag 1 has : ABC

EX: Bag 2 has : ARC

the difference is "R"

BagInterface

public boolean add(T anEntry);

public T remove(); //if you need this method, include it, if not then no worries

public boolean remove(T anEntry);

public int getFrequencyOf(T anEntry); //if you need this method, include it, if not then no worries

public T[] toArray();

public boolean contains(T anEntry);

public BagInterface<T> difference(BagInterface<T> otherBag);

ResizeableArrayBag

public class ResizeableArrayBag<T> implements BagInterface<T> {

private T[] bag;

private static final int DEFAULT_CAPACITY = 25;

private int numberOfEntries;

//constructor

public ResizeableArrayBag() {

this(DEFAULT_CAPACITY);

}

//constructor

public ResizeableArrayBag(int desiredCapacity) {

@SuppressWarnings("unchecked")

T[] tempBag = (T[]) new Object[desiredCapacity];

bag = tempBag;

numberOfEntries = 0;

}

public boolean add(T anEntry){

//write code

}

public T remove(){

//if you need this method, include it, if not then, no worries

}

public boolean remove(T anEntry){

//write code

}

public int getFrequencyOf(T anEntry){

//if you need this method, include it, if not then no worries

}

public T[] toArray(){

//write code

}

public boolean contains(T anEntry){

//write code

}

public BagInterface<T> difference(BagInterface<T> otherBag){

//write code, explain what is being done and give time complexity in terms of Big O notation

}

}

ResizeableArrayBagDemo

public class ResizeableArrayBagDemo {

public static void main(String[] args) {

ResizeableArrayBag<String> bag1 = new ResizeableArrayBag<>(1);

ResizeableArrayBag<String> bag2 = new ResizeableArrayBag<>();

bag1.add("z");

bag1.add("y");

bag1.add("c");

bag1.add("d");

bag2.add("z");

bag2.add("y");

bag2.add("e");

bag2.add("f");

System.out.print("The Difference of Bag One and Bag Two is: ");

System.out.println(Arrays.toString(bag1.difference(bag2).toArray())); //this should display [c,d,e,f]

}

}

Node Class

public class Node<T> {

public T data;

public Node<T> next;

public Node(T dataPortion) {

//write code

}

public Node(T dataPortion, Node<T> nextNode) {

//write code

}

public T getData() {

//write code

}

public void setData(T newData) {

//write code

}

public Node<T> getNextNode(){

//write code

}

public void setNextNode(Node<T> nextNode) {

//write code

}

}

Linked Bag

private Node<T> firstNode;

private int numberOfEntries;

public LinkedBag() {

firstNode = null;

numberOfEntries = 0;

}

public boolean add(T anEntry){

//write code

}

public T remove(){

//if you need this method, include it, if not then, no worries

}

public boolean remove(T anEntry){

//write code

}

public int getFrequencyOf(T anEntry){

//if you need this method, include it, if not then no worries

}

public T[] toArray(){

//write code

}

public boolean contains(T anEntry){

//write code

}

public BagInterface<T> difference(BagInterface<T> otherBag){

//write code, explain what is being done and give time complexity in terms of Big O notation

}

Linked Bag Demo

public class LinkedBagTest {

public static void main(String[] args) {

BagInterface<String> bag1 = new LinkedBag<>();

BagInterface<String> bag2 = new LinkedBag<>();

bag1.add("z");

bag1.add("y");

bag1.add("c");

bag1.add("d");

bag2.add("z");

bag2.add("y");

bag2.add("e");

bag2.add("f");

System.out.print("The Difference of Bag One and Bag Two is: ");

System.out.println(Arrays.toString(bag1.difference(bag2).toArray())); //this should display [c,d,e,f]

}

}

Solutions

Expert Solution

Solution:

In the code below I created one java file and there is a main class and all other utility classes are defined there. All utility classes could be defined in different files. And I didn't create ArrayBagTest.java and LinkedBagTest.java instead I put all test cases into the main method of the main class.

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

// TODO could be well documented java doc
// and classes can be put into different files

// bag contract
// custom type T should implement equals and hashcode
// data order in the bag does not matter 
abstract class Bag<T> {
        
        // this list has to initialized as per implementation strategy
        protected List<T> data;
        
        // add collections of data
        public void addAll(Collection<T> data) {
                this.data.addAll(data);
        }
        
        // add data one by one
        public void add(T ... data) {
                for(T d : data) {
                        this.data.add(d);
                }
        }
        
        // union of two bags
        public final Bag<T> union(Bag<T> bag) {
                // union with another bag
                // here new bag data order does not matter
                Bag<T> newbag = new ResizableArrayBag<T>();
                newbag.addAll(this.data);
                newbag.addAll(bag.data);
                return newbag;
        }
        
        // internal usage
        Map<T, Integer> map(Bag<T> bag) {
                Map<T, Integer> elements = new HashMap<>(); // track duplicate elements
                for(T d : bag.data) {
                        if(elements.containsKey(d)) {
                                elements.put(d, elements.get(d) + 1);
                        } else {
                                elements.put(d, 1);
                        }
                }
                return elements;
        }
        
        // intersection of two bags
        public final Bag<T> intersection(Bag<T> bag) {
                Map<T, Integer> elements = map(this);
                // intersection
                // here new bag data order does not matter
                Bag<T> newbag = new ResizableArrayBag<T>();
                for(T d : bag.data) {
                        if(elements.containsKey(d)) {
                                // common
                                int c = elements.get(d);
                                newbag.add(d);
                                c--;
                                // track common elements
                                if(c == 0) {
                                        elements.remove(d);
                                } else {
                                        elements.put(d, c);
                                }
                        }
                }
                return newbag;
        }
        
        // difference of two bags
        public final Bag<T> difference(Bag<T> bag) {
                Map<T, Integer> elements = map(bag);
                // difference
                // here new bag data order does not matter
                Bag<T> newbag = new ResizableArrayBag<T>();
                for(T d : this.data) {
                        if(elements.containsKey(d)) {
                                int c = elements.get(d);
                                c--;
                                // remove common elements from another bag
                                if(c == 0) {
                                        elements.remove(d);
                                } else {
                                        elements.put(d, c);
                                }
                        } else {
                                // can be put into new bag directly
                                newbag.add(d);
                        }
                }
                return newbag;
        }
        
        // text representation
        @Override
        public String toString() {
                StringBuilder text = new StringBuilder();
                text.append('[');
                int s = data.size();
                if(s > 0) {
                        text.append(data.get(0));
                        for(int i = 1; i < s; i++) {
                                text.append(", ").append(data.get(i));
                        }
                }
                text.append(']');
                
                return text.toString();
        }
}

// one implementation using array-list
class ResizableArrayBag<T> extends Bag<T> {
        // constructor
        public ResizableArrayBag() {
                this.data = new ArrayList<T>();
        }
}

// another implementation is based on linked list
class LinkedBag<T> extends Bag<T> {
        public LinkedBag() {
                this.data = new LinkedList<T>();
        }
}

// collection bag implementations
public class CollectionBag {
        
        // test method (instead of client program and putting all test cases into main method)
        public static void main(String[] args) {
                // resizable array bag
                Bag<Character> bag1 = new ResizableArrayBag<Character>();
                bag1.add('a', 'b', 'b', 'c');
                Bag<Character> bag2 = new ResizableArrayBag<Character>();
                bag2.add('b', 'b', 'b', 'c', 'd', 'e');
                
                // test 3 methods
                System.out.println(bag1.union(bag2));
                System.out.println(bag1.intersection(bag2));
                System.out.println(bag1.difference(bag2));
                
                // linked bag
                bag1 = new LinkedBag<Character>();
                bag1.add('a', 'b', 'b', 'c');
                bag2 = new LinkedBag<Character>();
                bag2.add('b', 'b', 'b', 'c', 'd', 'e');
                
                // test 3 methods
                System.out.println(bag1.union(bag2));
                System.out.println(bag1.intersection(bag2));
                System.out.println(bag1.difference(bag2));
        }
}

Related Solutions

What is an array-based list? What is a resizable list? What is the difference between a...
What is an array-based list? What is a resizable list? What is the difference between a list’s capacity and its size? When a list is expanded, is the size changed or is its capacity changed?
Linked List: Complete the following code to create a linked list from an Array. After creating...
Linked List: Complete the following code to create a linked list from an Array. After creating the list, display the elements of the linked list iteratively. Write two others function called as RDisplayTailRecursion(first) and RDisplayTailRecursion(first) which will print elements of the linked list using the tail and head recursions respectively. #include <stdio.h> #include <stdlib.h> struct Node { }*first=NULL; void create(int A[], int n) { for(i=1; i<n; i++) { } } void Display(struct Node*p) { while(p!=NULL) { } } void RDisplayTailRecursion...
1. With respect to resizable arrays, establish the relationship between the size of the array and...
1. With respect to resizable arrays, establish the relationship between the size of the array and the time it takes to perform random access (read a the value stored at a given position somewhere in the array). Provide empirical evidence to support your answer.
Given an array of Student type and size 10, create a linked list of students by...
Given an array of Student type and size 10, create a linked list of students by linking students with an odd index first and then linking students with an even index. Write a loop to print out the students in the linked list #include<iostream> #include<string> #include<fstream> using namespace std; const int NUM = 10; struct Student{ string fName; string lName; Student * next; }; int main() {        Student stuArr[NUM];        ifstream myfile;        myfile.open("Test.txt");        for(int i = 0;...
Given an array of Student type and size 10, create a linked list of students by...
Given an array of Student type and size 10, create a linked list of students by linking students with an odd index first and then linking students with an even index. Write a loop to print out the students in the linked list. #include #include #include using namespace std; const int NUM = 10; struct Student{ string fName; string lName; Student * next; }; int main() { Student stuArr[NUM]; ifstream myfile; myfile.open("Test.txt"); for(int i = 0; i < NUM; i++)...
In C++ write an implementation of the ADT sorted list that uses a resizable array (vector...
In C++ write an implementation of the ADT sorted list that uses a resizable array (vector class of C++ STL) to represent the list items.
Question: Write an implementation of the ADT sorted list that uses a resizable array (vector class...
Question: Write an implementation of the ADT sorted list that uses a resizable array (vector class of C++ STL) to represent the list items. Anytime the list becomes full, double the size of the array.
Create a ValueGet() method that takes a linked list as input and an integer index and...
Create a ValueGet() method that takes a linked list as input and an integer index and returns the value stored in the node at that index position. Sample Input: 01->100->300->214, index = 2 Output: 300 At index 2 the node has a value of 300 give me the full code. Give the full code with c++
Please use Python to create a method for a linked list that returns the index of...
Please use Python to create a method for a linked list that returns the index of a lookup value within the linked lust
Create global variables Mean, Mode, Median, then create a method that takes an array of 10...
Create global variables Mean, Mode, Median, then create a method that takes an array of 10 double numbers and return three answers of the Mean, Median, Mode ( no need for implementation of the mean, median and mode, calculate them manually and return the answers), assign the answers to the global variables. In java, please!!
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT