Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

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

ID: 3866291 • Letter: C

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 implements the interface BagInterface. Use an
instance of the class ArrayList to contain a bag’s 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());
       }
       }


   }

}

Explanation / Answer

I have fixed the code. I presume you have the code for BagInterface and StackInterface. The fixed code is given below. Please do rate the answer if it helped. Thank you.

ArrayListBag.java

import java.util.ArrayList;

public class ArrayListBag<T> implements Baglnterface<T> {
     
   private ArrayList<T> bag;
   private final static int DEFAULT_CAPACITY = 25;

   public ArrayListBag() {
       this(DEFAULT_CAPACITY);

   }

   public ArrayListBag(int capacity) {
       bag = new ArrayList<T>(capacity);
   }

   public boolean add(T newEntry)
   {
       bag.add(newEntry);
       return true;
   }

   public int getCurrentSize()
   {
       return bag.size();
   }

   public boolean isEmpty() {
       return bag.isEmpty();
   }

   public boolean isFull()
   {
       return false;
   }

   public T remove()
   {
       T result = null;
       if(!bag.isEmpty())
           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.clear();
   }
  
   public int getFrequencyOf(T anEntry)
   {
       int counter = 0;
       for (int index = 0; index < bag.size(); index++)
       {
           if (anEntry.equals(bag.get(index)))
               counter++;
       }
       return counter;
   }

   public boolean contains(T anEntry) {
       return bag.contains(anEntry);
   }
      
   public T[] toArray()
   {
      
       return (T[]) bag.toArray();
      
   }

   public static void main(String[] args)
   {

       ArrayListBag<String> aBag = new ArrayListBag<String>();
       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[] entries = aBag.toArray();
       for(int i = 0; i <entries.length; i++)
           System.out.println(entries[i]);

       System.out.println("String 'Java' occurs " +
                       aBag.getFrequencyOf("Java") + " times");

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

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

       entries = aBag.toArray();
       for(int i = 0; i <entries.length; i++)
           System.out.println(entries[i]);
      
       System.out.println("....Call the clear method....");
       aBag.clear();
       System.out.println("The bag is empty is " + aBag.isEmpty()) ;
   }
}

output

Bag size: 6

C

C++

Java

PHP

Java

Java

String 'Java' occurs 3 times

Pascal is present in bag is false

...After remove is called...

C

C++

Java

Java

....Call the clear method....

The bag is empty is true

ArrayListStack.java

import java.util.ArrayList;

public class ArrayListStack<T> implements Stacklnterface<T> {
   private ArrayList<T> stack;

   //Creates an empty bag whose initial capacity is 25
  
   public ArrayListStack()
   {
       stack = new ArrayList<T>();
   }

   public ArrayListStack(int capacity) {
       stack = new ArrayList<T>(capacity);
   }

     

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

   public void push(T newEntry)
   {
       stack.add(newEntry);
   }

     

   public T pop()
   {
       if(!isEmpty())
           return stack.remove(stack.size() - 1);
       else
           return null;
   }

   public T peek()
   {
       if(!stack.isEmpty())
           return stack.get(stack.size()-1);
       else
           return null;
   }

   public boolean isEmpty()
   {
       return stack.isEmpty();
   }

   public void clear() {
       stack.clear();
   }

   public String toString()
   {
       String result="";
       for(int i=0; i < stack.size(); i++)
           result += stack.get(i) + " ";
       return result;
   }

   int size() {
       return stack.size();
   }
}

TestStack.java

public class TestStack
{
   public static void main(String[] args)
   {
       ArrayListStack<String> aStack=new ArrayListStack<String>();
       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());
   }
}

output

Stack size:6
C
C++
JAVA
PHP
JAVA
JAVA

C
C++
JAVA
PHP

PHP
The bag is empty is true

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at drjack9650@gmail.com
Chat Now And Get Quote