Question

Write a complete bag class implementation using linked implementation. The linked bag class name must be...

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 particular object from the bag, if possible

7. Remove all objects from the bag

8. Count the number of times a certain object occurs in the bag

9. Test whether the bag contains a particular object

10. Look at all objects that are in the bag

=============================================

Node.java

public class Node<T>
{
        private T entry;
        private Node next;
        
        private Node(T entryPortion)
        {
                this(entryPortion, null);
        }
                
        private Node(T entryPortion, Node nextNode)
        {
                entry = entryPortion;
                next = nextNode;
        }
}

=======================================================

BagInterface.java

public interface BagInterface<T>
{
        public int getCurrentSize();
        public boolean isFull();
        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 T[] toArray();
} // end BagInterface
0 0
Add a comment Improve this question Transcribed image text
Answer #1

please comment for any query and please don't forget like if it helped you

code:

//LinkedBag.java

public class LinkedBag<T> implements BagInterface<T>

{

private class Node

{

T entry;

Node next;

Node(T entryPortion)

{

this(entryPortion, null);

}

Node(T entryPortion, Node nextNode)

{

entry = entryPortion;

next = nextNode;

}

}

private Node first; // beginning of bag

private int n; // number of elements in bag

private int maxsize=10; //max size of the bag

public LinkedBag() {

this.first = null;

this.n = 0;

}

public int getCurrentSize()

{

return this.n;

}

public boolean isFull()

{

return this.n==this.maxsize;

}

public boolean isEmpty()

{

return this.n==0;

}

public boolean add(T item) {

if(!isFull())

{

Node oldfirst = this.first;

this.first = new Node(item,oldfirst);

this.n++;

return true;

}

else

return false;

}

public T remove()

{

Node oldfirst = this.first;

this.first=oldfirst.next;

this.n--;

T rem=oldfirst.entry;

return rem;

}

public boolean contains(T anEntry)

{

Node tmp=this.first;

for(int i=0;i<n;i++)

{

if(tmp.entry==anEntry)

return true;

tmp=tmp.next;

}

return false;

}

public boolean remove(T anEntry)

{

Node tmp=this.first;

for(int i=0;i<n;i++)

{

if(tmp.entry==anEntry)

{

this.n--;

tmp=tmp.next;

return true;

}

tmp=tmp.next;

}

return false;

}

public void clear()

{

this.n=0;

this.first=null;

}

public int getFrequencyOf(T anEntry)

{

Node tmp=this.first;int ct=0;

for(int i=0;i<n;i++)

{

if(tmp.entry==anEntry)

ct++;

tmp=tmp.next;

}

return ct;

}

public T[] toArray()

{

T arr[]=(T[])java.lang.reflect.Array.newInstance(Integer.class,n);

Node tmp=this.first;

for(int i=0;i<n;i++)

{

arr[i]=tmp.entry;

tmp=tmp.next;

}

return arr;

}

}

/**

* LinkedBagDemo - test case of the class

*/

class LinkedBagDemo

{

public static void main(String[] args) {

LinkedBag<Integer> l=new LinkedBag<Integer>();

for(int i=0;i<3;i++)

{

System.out.println("adding "+i+" to bag");

l.add(i);

}

for(int i=0;i<10;i++)

if(l.contains(i))

{ System.out.println("frequency of "+i+" is "+l.getFrequencyOf(i));

System.out.println("removing: "+l.remove(i));

}

System.out.println("bag size is "+l.getCurrentSize());

if(l.isEmpty())

System.out.println("bag is empty");

else

System.out.println("bag is not empty");

}

}

output:

Add a comment
Know the answer?
Add Answer to:
Write a complete bag class implementation using linked implementation. The linked bag class name must be...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Create a java class that implements BagInterface using singly linked data. Name this new class LinkedBag....

    Create a java class that implements BagInterface using singly linked data. Name this new class LinkedBag. Be sure to include a default constructor to initialize the private members of the class. Test that your code works properly. BagInterface includes the methods:  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 T[] toArray();

  • Write a program that thoroughly tests the class LinkedBag. Write the "LinkedBag" class, and a main...

    Write a program that thoroughly tests the class LinkedBag. Write the "LinkedBag" class, and a main program named "Project1.java" testing the methods defined for the LinkedBag object. The interface file: BagInterface.java. /** An interface that describes the operations of a bag of objects. @author Frank M. Carrano @author Timothy M. Henry @version 4.0*/public interface BagInterface<T>{ /** Gets the current number of entries in this bag. @return The integer number of entries currently in the bag. */ public int getCurrentSize(); /**...

  • In this lab, we will implement the Linked Bag. The bag will contain a sequence of...

    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...

  • Study the provided BagInterface and the methods implemented in the ResizableArrayBag For the following methods that...

    Study the provided BagInterface and the methods implemented in the ResizableArrayBag For the following methods that you are to implement in ResizableArrayBag for Project3 of Lab02:  equals, removeMax, removeEvery, replace, union, intersection, difference, getAllLessThan, isSubset give an algorithm in pseudocode draw memory diagrams showing how the respective bags will look like as the result of the given method call public interface BagInterface<T extends Comparable<? super T>> { public int getCurrentSize(); public boolean isEmpty(); public boolean add(T newEntry); public T remove(); public...

  • I don't understand the question. Could someone solve and explain it please? Assume that you have...

    I don't understand the question. Could someone solve and explain it please? Assume that you have the following Bag object, myBag, with n String data: Baginterface <String> myBag new ArrayBag< >( Write Java statements that create a newBag object which contains non-duplicate data in myBag and marks the duplicate data. Example: if myBag contains data Hn newBag object should contain: "aa", "Аа", "СС", "Bb", "DUP. 1.Bb", "DUP.2.Aa", "D', Hint: You can use the Bag's methods: int getCurrentSize (0; boolean isFull...

  • Java Write an intersection method for the ResizableArrayBag class. The intersection of two bags is the...

    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...

  • Problem 3 (List Implementation) (35 points): Write a method in the DoublyLList class that deletes the...

    Problem 3 (List Implementation) (35 points): Write a method in the DoublyLList class that deletes the first item containing a given value from a doubly linked list. The header of the method is as follows: public boolean removeValue(T aValue) where T is the general type of the objects in the list and the methods returns true if such an item is found and deleted. Include testing of the method in a main method of the DoublyLList class. ------------------------------------------------------------------------------------- /** A...

  • 5. Below i s the class deciaration for the Bag class from your text. Refer to...

    5. Below i s the class deciaration for the Bag class from your text. Refer to this hea he Ted implementation @file Bag.h #ifndet BAG #define BAG template <class ItemType> class Bag private: static const int DEFAULT BAG SIZE 6; Il current count of Bag items /I max capacity of the Bag ItemType items[DEFAULT BAG SIZE]; //array of Bag items int itemCount; int maxitems; /l Returns either the index of the element in the array items that ll contains the...

  • Suppose that you have several numbered billiard balls on a pool table. The smallest possible number...

    Suppose that you have several numbered billiard balls on a pool table. The smallest possible number on the ball is “1”. At each step, you remove a billiard ball from the table. If the ball removed is numbered n, you replace it with n balls randomly numbered less than n. For example, if you remove the “5” ball, you replace it with balls numbered “2”, “1”, “1”, “4”, and “3”, where numbers 2, 1, 1, 4, and 3 were randomly...

  • In a file named LLBag.java, write a class called LLBag that implements the Bag interface using...

    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...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT