Write the class Bag that implements the Baglnterface using an Array. Baglnterfac
ID: 3872200 • Letter: W
Question
Write the class Bag that implements the Baglnterface using an Array. Baglnterface should have the following methods. Write the main method to create two bags. One bag is of type Car and the other bag is of type string. Place 3 car objects and 4 strings in each bag. Test each of the following methods in the class Bag +getCurrentSizeO: integer +isEmpty O boolean +add (newEntry: T): boolean +removeO: T +remove CanEntry: T): boolean +clearO: void +getFrequencyOf (anEntry: T): integer +contains (anEntry: T):boolean +toArrayO: TC]Explanation / Answer
The Answer for ypur question is given below:
code:
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package javaapplication1;
/**
*
* @author miracle
*/
public class Bag<T> implements BagInterface<T>
{
private final T[] bag;
private static final int CAPACITY_DEFAULT=30;
private int numberOfEntries;
public Bag()
{
this(CAPACITY_DEFAULT);
}
public Bag(int capacity)
{
numberOfEntries =0;
T[] tempBag =(T[])new Object[capacity];
bag =tempBag;
}
public boolean add(T newEntry)
{
boolean result =true;
if (isFull())
{
result =false;
}
else
{
bag[numberOfEntries]=newEntry;
numberOfEntries++;
}
return result;
}
public int getIndexOf(T anEntry){
int index =-1;
for ( index=0; index<numberOfEntries; index++)
{
if (bag[index].equals(anEntry))
return index;
}
return index;
}
public T remove(T item){
int index =getIndexOf(item);
T result =removeEntry(index);
return result;
}
public T removeEntry(int index)
{
T result =null;
if (!isEmpty() && (index >=0))
{
result =bag[index];
numberOfEntries--;
bag[index] =bag[numberOfEntries];
bag[numberOfEntries]=null;
}
return result;
}
public T[] toArray(){
@SuppressWarnings("unchecked")
T[] result =(T[])new Object[numberOfEntries];
for (int index=0; index<numberOfEntries; index++)
{
result[index]=bag[index];
}
return result;
}
public boolean isFull()
{
return numberOfEntries == bag.length;
}
public boolean isEmpty()
{
return numberOfEntries == 0;
}
public boolean contains(T anEntry)
{
boolean found =false;
for (int index =0; !found && (index<numberOfEntries); index++)
{
if (anEntry.equals(bag[index]))
found =true;
}
return found;
}
public boolean replace(T item, T newItem)
{
int index =this.getIndexOf(item);
if (index>=0)
{
bag[index] =newItem;
return true;
}
return false;
}
public void removeAll(T item)
{
while (this.contains(item))
this.remove(item);
}
public boolean equals(BagInterface<T> other)
{
T[] thisBag =this.toArray();
T[] otherBag =other.toArray();
Arrays.sort(thisBag);
Arrays.sort(otherBag);
if (thisBag.length != otherBag.length) return false;
for (int index =0; index <thisBag.length; index++)
{
if (!thisBag[index].equals(otherBag[index]))
return false;
}
return true;
}
}
Hope This Helps, if you have any doubts Please comment i will get back to you, thank you and please thumbs up
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.