instructions: Write a
non-member method named bagScaler, which removes from a Bag all
elements that occur less than N times . The method returns the
number of copies removed.
For example, if B = {Bob, Joe, Bob, Ned, Bob, Ned, Kim}, then
calling bagScaler(B, 3) removes Joe, Ned, and Kim making B = {Bob,
Bob, Bob}, and returns 4 because it removed 2 copies of Ned, one of
Joe, and one of Kim.
Note: There's no iterator, but there is a toArray method.
public class
BagWrapper {
public static interface Bag {
public int size();
public boolean isEmpty();
public void add(Object e);
public boolean isMember(Object e);
public boolean remove(Object e);
public int removeAll(Object e);
public int count(Object e);
public void clear();
public Object[] toArray();
}
public static class DynamicBag implements Bag{
private Object[] elements;
private int currentSize;
private static final int DEFAULT_SIZE = 10;
public DynamicBag(int initialSize) {
if (initialSize < 1) {
throw new IllegalArgumentException("Size must be at least
1");
}
this.elements = new Object[initialSize];
this.currentSize = 0;
}
public DynamicBag() {
this(DEFAULT_SIZE);
}
@Override
public int size() {
return this.currentSize;
}
@Override
public boolean isEmpty() {
return this.size() == 0;
}
@Override
public void add(Object e) {
if (e == null) {
throw new IllegalArgumentException("Argument cannot be
null");
}
if (this.size() == this.elements.length) {
this.reAllocate();
}
this.elements[this.currentSize++] = e;
}
private void reAllocate() {
Object temp[] = new Object[2*this.size()];
for (int i=0; i < this.size(); ++i) {
temp[i] = this.elements[i];
}
this.elements = temp;
}
@Override
public boolean isMember(Object e) {
return this.count(e) > 0;
}
@Override
public boolean remove(Object e) {
for (int i=0; i < this.size(); ++i) {
if (this.elements[i].equals(e)) {
this.elements[i] = this.elements[this.currentSize-1];
this.elements[this.currentSize-1] = null;
--this.currentSize;
return true;
}
}
return false;
}
@Override
public int removeAll(Object e) {
int result = 0;
while(this.remove(e)) {
result++;
}
return result;
}
@Override
public int count(Object e) {
int result = 0;
for (int i=0; i < this.size(); ++i) {
if (this.elements[i].equals(e)) {
result++;
}
}
return result;
}
@Override
public void clear() {
for (int i=0; i < this.size(); ++i) {
this.elements[i] = null;
}
this.currentSize = 0;
}
public Object[] toArray() {
Object[] result = new Object[this.size()];
for (int i=0; i < this.size(); ++i) {
result[i] = this.elements[i];
}
return result;
}
}
// NON-MEMBER METHOD
public static int bagScaler(Bag B, int n) {
// ADD YOUR CODE HERE
return n; //dummy return
}
}
package apr26;
import java.util.Arrays;
public class BagWrapper {
public static interface Bag {
public int
size();
public boolean
isEmpty();
public void
add(Object e);
public boolean
isMember(Object e);
public boolean
remove(Object e);
public int
removeAll(Object e);
public int
count(Object e);
public void
clear();
public Object[]
toArray();
}
public static class DynamicBag implements
Bag {
private Object[]
elements;
private int
currentSize;
private static
final int DEFAULT_SIZE = 10;
public
DynamicBag(int initialSize) {
if
(initialSize < 1) {
throw
new IllegalArgumentException("Size must be at least 1");
}
this.elements
= new Object[initialSize];
this.currentSize
= 0;
}
public DynamicBag()
{
this(DEFAULT_SIZE);
}
@Override
public int size()
{
return
this.currentSize;
}
@Override
public boolean
isEmpty() {
return
this.size() == 0;
}
@Override
public void
add(Object e) {
if
(e == null) {
throw
new IllegalArgumentException("Argument cannot be null");
}
if
(this.size() == this.elements.length) {
this.reAllocate();
}
this.elements[this.currentSize++]
= e;
}
private void
reAllocate() {
Object
temp[] = new Object[2 * this.size()];
for
(int i = 0; i < this.size(); ++i) {
temp[i]
= this.elements[i];
}
this.elements
= temp;
}
@Override
public boolean
isMember(Object e) {
return
this.count(e) > 0;
}
@Override
public boolean
remove(Object e) {
for
(int i = 0; i < this.size(); ++i) {
if
(this.elements[i].equals(e)) {
this.elements[i]
= this.elements[this.currentSize - 1];
this.elements[this.currentSize
- 1] = null;
--this.currentSize;
return
true;
}
}
return
false;
}
@Override
public int
removeAll(Object e) {
int
result = 0;
while
(this.remove(e)) {
result++;
}
return
result;
}
@Override
public int
count(Object e) {
int
result = 0;
for
(int i = 0; i < this.size(); ++i) {
if
(this.elements[i].equals(e)) {
result++;
}
}
return
result;
}
@Override
public void clear()
{
for
(int i = 0; i < this.size(); ++i) {
this.elements[i]
= null;
}
this.currentSize
= 0;
}
public Object[]
toArray() {
Object[]
result = new Object[this.size()];
for
(int i = 0; i < this.size(); ++i) {
result[i]
= this.elements[i];
}
return
result;
}
}
// NON-MEMBER METHOD
public static int bagScaler(Bag B, int n)
{
int r = 0;
Object[] data =
B.toArray();
for(Object d: data)
{
if(B.count(d)
< n)
{
while(B.count(d)
!= 0) {
B.remove(d);
r++;
}
}
}
return r;
}
public static void main(String args[])
{
Bag x = new
DynamicBag();
x.add("Bob");
x.add("Joe");
x.add("Bob");
x.add("Ned");
x.add("Bob");
x.add("Ned");
x.add("Kim");
System.out.println(bagScaler(x,
3));
System.out.println(Arrays.toString(x.toArray()));
}
}
**************************************************
I have provided a test code in main method.. You may or may not
consume that.
Thanks for your question. We try our best to help you with detailed
answers, But in any case, if you need any modification or have a
query/issue with respect to above answer, Please ask that in the
comment section. We will surely try to address your query ASAP and
resolve the issue.
Please consider providing a thumbs up to this question if it helps you. by Doing that, You will help other students, who are facing similar issue.
instructions: Write a non-member method named bagScaler, which removes from a Bag all elements that occur...
Im having trouble implimenting the addAll method.. import java.util.Collection; public interface Tree<E> extends Collection<E> { /** Return true if the element is in the tree */ public boolean search(E e); /** Insert element e into the binary tree * Return true if the element is inserted successfully */ public boolean insert(E e); /** Delete the specified element from the tree * Return true if the element is deleted successfully */ public boolean delete(E e); /** Get the number of...
Im writing a method to evaluate a postfix expression. Using my own stack class. Here is my code but I keep getting a classcastexception where it says java.lang.Character cannot be cast to java.lang,Integer. Im not sure how to fix this. public class Evaluator { public static void evaluatePost(String postFix) { LinkedStack stack2 = new LinkedStack(); int val1; int val2; int result; for(int i = 0; i < postFix.length(); i++) { char m = postFix.charAt(i); if(Character.isDigit(m)) { stack2.push(m); } else { ...
In a file named LLBag.java, write a class called LLBag that implements the Bag interface using a linked list instead of an array. You may use a linked list with or without a dummy head node. Bag interface code: /* * Bag.java * * Computer Science 112, Boston University */ /* * An interface for a Bag ADT. */ public interface Bag { /* * adds the specified item to the Bag. Returns true on success * and...
WRITE IN JAVA: I've been trying to do this for hours and I can't figure this out. I tried the toArray method and I'm stumped. I'm not sure if the remove method is right aswell. If you need anymore info, let me know. I need the following methods completed: toArray Remove ReverseArray /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template...
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...
This wont take you more than 15 mins. Comple the two method and besure to test with Junit test that provided below. toArray() -- this method returns a newly allocated array containing the elements in the multiset. The array this method returns must only contain the elements in the multiset and not any nulls or other values that are stored in the backing store, but are not in the multiset. fromArray(E[] arr) -- this method updates the multiset so that...
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 { /**...
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...
Q1) Write a method called inToTree of ExpressionTree class which takes a string of infix expression (fully parenthesized), resets the root of the expression tree to such tree which constructed from the expression. The root set to null of the expression contains characters other than value, operator, and parenthesis or if it’s not an in-fix full parenthesized expression. You may assume each value is single digit only . public void inToTree(String expression){ You may use a JCF Deque class as...