Question
Hello, I'm having a little trouble understanding this programming problem using Java . So far, I've created a class called StringBag, implementing the interface below,
Hello, I'm having a little trouble understanding this programming problem using Java. So far, I've created a class called "StringBag", implementing the interface below, therefore, inheriting all of its methods. I've created a private LinkedList called "stringBag", a private String called "bagName" and that's all I have. I see that I'm trying to implement a stack using a LinkedList as described in the description but am confused on how to do so... The thing that I'm confused about the most is in the description where it says: "Note: if the list is not empty, then the following code will remove the first item in a linked list: head = head.getLink();" Is this suggesting that I create a private field called "head"? And is this supposed to represent the top element in the list? Also, for certain abstract methods like size() and clear(), am I just returning stringBag.size() and stringBag.clear() using methods built in to the LinkedList class?
Thanks in advance.
UPDATE: Also, is the head variable a Node? So the program is suggesting that I have to specifically link nodes together?
DESCRIPTION:
A StringBag Abstract Data Type (ADT) is a simple collection of strings ordered by time of entry (not alphabetical). Like a stack, the last string entered goes on the top. The strings are not alphabetized in the bag. Client programs can insert strings into the bag, check its size, clear it, use its toString, remove the top string, and check to see if it is full. We want to keep things simple, so we allow duplicates. There is no provision to see if a given string is already in the bag. Further, the client can only remove the string on top, like a stack. The StringBag is a LIFO collection. See the StringBagInterface, below. Create a class to implement the interface using a private linked list to hold the inserted strings. Note: if the list is not empty, then the following code will remove the first item in a linked list: head = head.getLink(); Design a test driver that shows that your StringBag.java class works correctly.
public interface StringBagInterface {
void insert(String element);
// Precondition: This StringBag is not full.
// Places element into this StringBag.
boolean isFull();
// Returns true if this StringBag is full, otherwise returns false.
boolean isEmpty();
// Returns true if this StringBag contains no strings.
int size();
// Returns the number of Strings in this StringBag.
String remove();
// Precondition: the Bag is not empty
// Removes the string at the top of the bag and returns it.
void clear();
// Makes this StringBag empty. String getName();
// Returns the name of this StringBag
String toString();
// Returns a nicely formatted string representing the
// name of the bag and all of its contents.
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started