Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Implement unimplemented methods: contains and getFrequencyOf. And Create the method public Resizable Array Bagintersection(ResizableArrayBagother){} package bag; import java.util.Arrays; public class ResizableArrayBag implements BagInterface { private

Implement unimplemented methods: contains and getFrequencyOf. And Create the method public Resizable Array Bagintersection(ResizableArrayBagother){}  package bag; import java.util.Arrays; public class ResizableArrayBag implements BagInterface { private static final int DEFAULT_CAPACITY = 10; private T[] bagArray; private int numberOfEntries = 0; private int capacity; /** * Creates an empty bag having a given initial capacity * * @param capacity The integer capacity desired */ @SuppressWarnings("unchecked") public ResizableArrayBag(int capacity) { bagArray = (T[]) new Object[capacity]; this.capacity = capacity; } /** * Creates an empty bag having the default initial capacity */ public ResizableArrayBag() { this(DEFAULT_CAPACITY); } private boolean isFull () { return (numberOfEntries == capacity); } private void doubleCapacity() { capacity *= 2; bagArray = Arrays.copyOf(bagArray, capacity); } @Override public int getCurrentSize() { return numberOfEntries; } @Override public boolean isEmpty() { return numberOfEntries == 0; } @Override public boolean add(T newEntry) { bagArray[numberOfEntries] = newEntry; numberOfEntries ++; if (isFull()) doubleCapacity(); return true; } @Override public boolean remove(T anEntry) { for (int idx = 0; idx < numberOfEntries; idx ++) { if (anEntry.equals(bagArray[idx])){ bagArray[idx] = bagArray[numberOfEntries - 1]; bagArray[numberOfEntries - 1] = null; return true; } } return false; } @Override public T remove() { if (isEmpty()) return null; T outData = bagArray[numberOfEntries - 1]; bagArray [--numberOfEntries] = null; return outData; } @Override public void clear() { for (int idx = 0; idx < numberOfEntries; idx ++) bagArray [idx] = null; numberOfEntries = 0; } @Override public boolean contains(T anEntry) { // to implement return false; } @Override public int getFrequencyOf(T anEntry) { // to implement return 0; } @Override public T[] toArray() { T[] result = Arrays.copyOf(bagArray, numberOfEntries); return result; } public ResizableArrayBag copy() { ResizableArrayBag result = new ResizableArrayBag <> (); for (int idx = 0; idx < numberOfEntries; idx ++) result.add (bagArray [idx]); return result; } public boolean equals (Object o) { ResizableArrayBag otherBag = (ResizableArrayBag ) o; if (otherBag.getCurrentSize() != numberOfEntries) return false; otherBag = otherBag.copy(); for (int idx = 0; idx < numberOfEntries; idx ++) { if (!otherBag.remove(bagArray[idx])) return false; } return true; } public ResizableArrayBag union (ResizableArrayBag otherBag) { ResizableArrayBag result = otherBag.copy(); for (int idx = 0; idx < numberOfEntries; idx ++) result.add (bagArray[idx]); return result; } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

101 Database Exercises Text Workbook

Authors: McGraw-Hill

2nd Edition

0028007484, 978-0028007489

More Books

Students also viewed these Databases questions

Question

=+When and under what circumstances are contracts renegotiated?

Answered: 1 week ago

Question

=+Are the contracts enforceable?

Answered: 1 week ago