Question

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();
0 0
Add a comment Improve this question Transcribed image text
Answer #1

As per your requirement the below one is solution please follow it

package com.venkanna;
interface BagInterface<T>
{
public void remove();
public int getCurrentSize();
public boolean isEmpty();
public boolean add(T newEntry);
public T remove1();
public boolean remove(T anEntry);
public void clear();
public int getFrequencyOf(T anEntry);
public boolean contains(T anEntry);
}
public class LinkedBag<T> implements BagInterface
{
private LinkedListNode<T> first = null;
public void add(LinkedListNode<T> node)
{
node.setNext(first);
first = node;
}
public void remove()
{
if(first.getNext()!=null)
first = first.getNext();
else first = null;
}
private void Display(LinkedListNode<T> node)
{
System.out.println("Node is " + node.getValue());
if(node.getNext()!=null) Display(node.getNext());
}

public void print()
{
Display(first);
}
public static void main(String[] args)
{
LinkedBag<String> list = new LinkedBag<String>();
list.add(new LinkedListNode<String>("venkanna"));
list.add(new LinkedListNode<String>("Shireesha"));
list.add(new LinkedListNode<String>("Ravi"));
list.add(new LinkedListNode<String>("Mounika"));
System.out.println("The ELements in the List is:");
list.print();
System.out.println("");
list.remove();
System.out.println("After removing the head from the List is:");
list.print();
}
public int getCurrentSize() {
return 0;
}
public boolean isEmpty()
{
return false;
}
public boolean add(Object newEntry)
{
return false;
}
public Object remove1()
{
return null;
}
public boolean remove(Object anEntry)
{
return false;
}
public void clear() {
}
public int getFrequencyOf(Object anEntry)
{
return 0;
}
public boolean contains(Object anEntry) {
// TODO Auto-generated method stub
return false;
}

}
class LinkedListNode<T>
{
private T value;
private LinkedListNode<T> next;
public LinkedListNode(T value)
{
this.value = value;
}
public void setNext(LinkedListNode<T> next)
{
this.next = next;
}
public LinkedListNode<T> getNext()
{
return next;
}
public T getValue()
{
return value;
}
}

Add a comment
Know the answer?
Add Answer to:
Create a java class that implements BagInterface using singly linked data. Name this new class LinkedBag....
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
  • 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...

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

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

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

  • import java.util.*; /** * A class that implements the ADT set by using a linked bag....

    import java.util.*; /** * A class that implements the ADT set by using a linked bag. * The set is never full. * * */ public class LinkedSetWithLinkedBag> implements SetInterface { private LinkedBag setOfEntries; /** * Creates a set from a new, empty linked bag. */ public LinkedSetWithLinkedBag() { //TODO Project1 } // end default constructor public boolean add(T newEntry) { //TODO Project1 // new node is at beginning of chain if(this.setOfEntries.isEmpty()) { if (!this.setOfEntries.contains(newEntry)) this.setOfEntries.add(newEntry); } return true; //...

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

  • Given a singly-linked list interface and linked list node class, implement the singly-linked list which has...

    Given a singly-linked list interface and linked list node class, implement the singly-linked list which has the following methods in Java: 1. Implement 3 add() methods. One will add to the front (must be O(1)), one will add to the back (must be O(1)), and one will add anywhere in the list according to given index (must be O(1) for index 0 and O(n) for all other indices). They are: void addAtIndex(int index, T data), void addToFront(T data), void addToBack(T...

  • This is a c++ class utilizing class templates and linked lists and Nodes. I need to...

    This is a c++ class utilizing class templates and linked lists and Nodes. I need to implement the following member function(s) to LinkedBag.cpp. Node.hpp/cpp should be fine but if you feel like there needs to be a change for compilation or testing, feel free to do so but make sure to comment on why it was done. In this case, I need to join the original items with the user items(a_bag). So if the original has {1,2,3} and a_bag has...

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

  • Define a class ArraySet using an array that represents a set and implements the ADT Set....

    Define a class ArraySet using an array that represents a set and implements the ADT Set. Make the ArraySet resizeable. Then write a C++ program that adequately demonstrates your implementation. Hi, I wrote the program but I don"t know why it showing the output - 000 000 0 0 My main.cpp file code is this - #include<stdio.h> #include<iostream> #include<fstream> #include "ArraySet.h" #include "ArraySet.cpp" using namespace std; int main(){ ArraySet<int> setA, setB, setC; // adds to setA setA.add(10); setA.add(20); setA.add(30); //...

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
ADVERTISEMENT