Question: can any one please edit these two codes am getting errors: can any one please edit this code : 1(a)Define a class of bags that

can any one please edit these two codes am getting errors:

can any one please edit this code :

1(a)Define a class of bags that implements the interface BagInterface. Use an instance of the class ArrayList to contain a bags entries. Then write a program that adequately demonstrates your new class. Note that you might have to handle exceptions thrown by methods of ArrayList. (b). Repeat (a), but instead define a class of stacks that implements the interface StackInterface

code for 1(a):

import java.util.ArrayList;

public class Baglnterface implements Baglnterface { @SuppressWarnings("rawtypes") private ArrayList Bag;

private static nal int DEFAULT_CAPACITY = 25;

public void ArrayListbag()

{

this(DEFAULT_CAPACITY);

}

public Baglnterface(int capacity)

{

bag = new ArrayList(capacity);

} public boolean add(T newEntry)

{

retrun bag.add(T newEntry)

}

public int getCurrentSize() { return bag.size() } public boolean isEmpty() { return bag.isEmpty(); } public boolean isFu||() {

return false; } public T remove()

{

T result = null;

if(!bag.isEmpty()) TUTO

result = (T)bag.remove(bag.size() - 1);

return result;

}

public boolean remove(T anEntry)

{

boolean result = bag.remove(anEntry); return result;

}

public void clear()

{

bag.c|ear(); } public int getFrequencyOfCl' anEntry) {

int counter = 0; for (int index = 0; index < bag.size(); index++)

{

Tl

if (anEntry.equals(bag.get(index))) counter++;

}

return counter;

} public boolean contains(T anEntry)

{

return bag.contains(anEntry);

}

public T[] toArray()

{

return (T[])bag.toAITay();

}

public static void main(Stn'ng[] args)

{

ArrayListBag aBag = new ArrayListBagO;

aBag.add("C"); aBag.add("C++"); aBag.add("Java"); aBag.add("PHP");

aBag.add("Java"); aBag.add("Java");

System.out.println("Bag size: "+aBag.getCurrentSize());

Object entriesl] = aBag.toArray();

for(int i=0;i

System.out.print|n(entnes[i]);

System.out.println("Stnng 'Java' occurs " + aBag.getFrequency0f("Java")+" times");

System.out.println("Pascal is present in bag is +aBag.oontains("Pascal"));

aBag.remove();

aBag.remove("PHP");

System.out.println("...After remove is called...");

entries = aBag.toArray();

for(int i=0;i

System.out.println(entr1es[i]); System.out.println("....Call the clear method....");

aBag.c|ear();

System.out.println("The bag is empty is " +aBag.isEmpty()) ; }

Code for 1b:

public interface Stacklnterface {

} import java.util.ArrayList;

public class ArrayListStack implements Stacklnterface {

private ArrayList stack;

private static nal int DEFAULT_CAPACITY = 25; Creates an empty bag whose initial capacity is 25. "Denition of oonstruootr

public ArrayListStackO

{

this(DEFAULT_CAPACITY);

}

public ArrayListStack(int capacity)

{

stack = new ArrayList(capacity);

}l/public ArrayListStack() { // TODO Auto-generated constructor stub }

End constructor

Adds a new entry to the top of this stack. /lMethod denition

public void push(T newEntry)

{

/lUse add method od ArrayList ciass to /lpush entries into the stack stack.add(newEntry);

}l/End method

public T pop() {

T result = null; if(!stack.isEmpty())

result = (T)stack.remove(stack.size()1); return result;

} public T peek()

{

T result = null;

if(!stack.isEmpty()) result=(T)stack.get(stack.size()-1; return result; } public boolean isEmpty() { return stack isEmpty(); } public void clear() { stack.clear(); } public String toString( { String result=""; fpr(int i=0;i result+=stack.get(i)+""; return result; } int size() { return stack.size(); } } public class test { public static void main(String[] args) { ArrayListStack aStack=new ArrayListStack(); aStack.push("C"); aStack.push("C++"); aStack.push("JAVA"); aStack.push("PHP"); aStack.push("JAVA"); aStack.push("JAVA"); System.out.println("Stack size:"+aStack.size()); System.out.println(aStack); aStack.pop(); aStack.pop(); System.out.println(aStack); System.out.println(aStack.peek()); aStack.clear(); System.out.println("The bag is empty is "+ aStack.isEmpty()); } }

}

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!