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

Java Question: Write the class Bag that implements the Baglnterface using an Arr

ID: 3872217 • Letter: J

Question

Java 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

Interface :

interface BagInterface<T> {
    public int getCurrentSize();
    public boolean isEmpty();
    public boolean add(T newEntry);
    public T remove();
    public boolean remove(T anEntry);
    public void clear();
    public int getFrequencyOf(T anEntry);
    public boolean contains(T anEntry);
    public Object[] toArray();
    public Object[] replace(T oldEntry,T NewEntry); // Replace old item with new item
}

Bag.java

import java.util.Arrays;

class Bag implements BagInterface {

    private Object[] Entry = null;

    Bag(int size) {
        Entry = new Object[size];
    }
    public static void main(String args[]) {
        Bag b1 = new Bag(3); // Creating String Array
        b1.add("Item 1"); // Adding First Item
        b1.add("Item 2"); // Adding Second Item
        b1.add("Item 3"); // Addming Third Item
        System.out.println("B1 " + b1.getCurrentSize()); // To Check The size printed the bag size
        System.out.println("B1 " + Arrays.toString(b1.toArray()));// To Check content printed the bag content (converting the array into String array)
        b1.replace("Item 1", "Item 4"); // Replacing First Item with new Item
        System.out.println("B1 " + b1.getCurrentSize()); // To Check The size printed the bag size
        System.out.println("B1 " + Arrays.toString(b1.toArray()));// To Check content printed the bag content (converting the array into String array)
        Bag b2 = new Bag(4); // Creating Integer Array your case you need to use "CAR" Obj
        b2.add(1); // Adding First Item
        b2.add(2); // Adding Second Item
        b2.add(3); // Adding Third Item
        b2.add(1);
        System.out.println("B2 " + b2.getCurrentSize()); // To Check The size printed the bag size
        System.out.println("B2 " + Arrays.toString(b2.toArray()));// To Check content printed the bag content (converting the array into String array)
        System.out.println("B2 " + Arrays.toString(b2.replace(1, 2))); // Replacing First Item by new Item and printing the bag content (converting the array into String array)
        System.out.println("B2 " + b2.getCurrentSize()); // To Check The size printed the bag size
    }

    @Override
    public int getCurrentSize() {
        return Entry.length;
    }

    @Override
    public boolean isEmpty() {
        return (Entry.length == 0);
    }

    @Override
    public boolean add(Object newEntry) {
        boolean status = false;
        for (int i = 0; i < Entry.length; i++) {
            if (Entry[i] == null) {
                Entry[i] = newEntry;
                status = true;
                break;
            }
        }
        return status;
    }

    @Override
    public Object remove() {
        boolean status = false;
        for (int i = Entry.length; i >= 0; i--) {
            if (Entry[i] != null) {
                Entry[i] = null;
                status = true;
            }
        }
        return status;
    }

    @Override
    public boolean remove(Object anEntry) {
        boolean status = false;
        for (int i = 0; i < Entry.length; i++) {
            if (Entry[i] == anEntry) {
                Entry[i] = null;
                status = true;
            }
        }
        return status;
    }

    @Override
    public void clear() {
        Entry = new Object[Entry.length];
    }

    @Override
    public int getFrequencyOf(Object anEntry) {
        int count = 0;
        for (int i = 0; i < Entry.length; i++) {
            if (Entry[i] == anEntry) {
                count++;
            }
        }
        return count;
    }

    @Override
    public boolean contains(Object anEntry) {
        boolean status = false;
        for (int i = 0; i < Entry.length; i++) {
            if (Entry[i] == anEntry) {
                status = true;
            }
        }
        return status;
    }

    @Override
    public Object[] toArray() {
        return Entry;
    }

    @Override
    public Object[] replace(Object oldEntry, Object NewEntry) {
        for (int i = 0; i < Entry.length; i++) {
            if (Entry[i].equals(oldEntry)) {
                Entry[i] = NewEntry;
                break;
            }
        }
        return Entry;
    }
}

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