Hello! I have added the class I am trying to fix and a tester. The tester needs
ID: 3889332 • Letter: H
Question
Hello! I have added the class I am trying to fix and a tester. The tester needs to be a success. Please fix LinkedBag!
public class LinkedBag implements BagInterface<String> {
private Node head;
private int size;
/*
* Constructs an empty bag by initializing head node. Initialize size to zero.
*/
public LinkedBag() {
Node head = new Node(null);
}
/*
* Returns the current size of the bag. You can return the value of size.
*/
@Override
public int getCurrentSize() {
return size;
}
/*
* Checks whether the bag is empty and returns true/false
*/
@Override
public boolean isEmpty() {
return size == 0;
}
/*
* Adds a new node to the start, because it's more efficient. Wraps a new node
* around the data. Then we make that new node the head of the list, first
* pointing it to the old head.
*/
@Override
public boolean add(String newEntry) {
// As long as there is memory remaining in the computer, this method should
// always succeed.
Node newNode = new Node(newEntry);
newNode.next = head.next;
head.next = newNode;
return true;
}
/*
* Since it's a bag, the programmer is free to choose which element to remove
* and return. So, since we want to be efficient programmers, we will remove the
* first node by advancing the head. Note the special case: if the bag is empty,
* return null.
*/
@Override
public String remove() {
Node current = head.next;
if (head.next == null) {
return null;
}
head.next = head.next.next;
return current.data;
}
/*
* THE EASIEST NODE TO REMOVE IS THE FIRST NODE. So, here is the algorithm: 1.
* Find the first occurrence of the string you want to remove. 2. COPY THE DATA
* from the first node to the node you found in step 1. 3. Remove the FIRST
* node.
* "But won't this mess up the order? Yes. But order doesn't matter. It's a bag."
* "Isn't it cheating to just copy the data? No. It's not cheating. It's clever."
*/
@Override
public boolean remove(String anEntry) {
Node temp = head;
if (!head.next.data.contains(anEntry)) {
return false;
}
while (!temp.next.data.equals(anEntry)) {
temp = temp.next;
}
temp = head.next;
remove();
return true;
}
/*
* Just set the head to null (and the size to 0). "But won't this leave a bunch
* of nodes hanging around in memory? No. As soon as we stop pointing to the
* first node, it will get marked as garbage so that memory can be cleaned up."
*/
@Override
public void clear() {
head = null;
size = 0;
}
// Returns a count of the number of items equal to anEntry
@Override
public int getFrequencyOf(String anEntry) {
Node temp = head;
int count = 0;
while (head.next != null) {
temp = temp.next;
if (temp.next.data.equals(anEntry)) {
count++;
}
}
return count;
}
// Returns whether or not anEntry appears in this bag
@Override
public boolean contains(String anEntry) {
Node temp = head;
while (!temp.next.data.equals(anEntry)) {
temp = temp.next;
if (temp.next == null) {
return false;
}
}
return true;
}
// Returns an array consisting of the elements in the bag
// If there are no items, return an array of size 0. This method
// should never return null.
@Override
public String[] toArray() {
String[] str;
if (size == 0) {
str = new String[0];
return str;
}
int i = 0;
str = new String[size];
Node temp = head;
while (head.next != null) {
i++;
str[i] = temp.next.data;
temp = temp.next;
}
return str;
}
public String toString() {
String result = "";
Node current = head;
while (current.next != null) {
result = result + current.next.data + " ";
current = current.next;
}
return result;
}
// A private inner class. Notice that
// the outer class can freely access its inner class, even
// though it's private.
private class Node {
private String data;
private Node next;
private Node(String data) {
this.data = data;
}
}
}
========================================================================================
public class LinkedBagSimpleTester {
public static void main(String[] args) {
// Testing you should do to make sure your code
// is working before submitting it:
// Empty bag
LinkedBag bag = new LinkedBag();
// Add some items, print the bag.
bag.add("cat");
bag.add("horse");
System.out.println(bag);
// Check the size
// Check if the bag contains cat
// Check if the bag contains horse
// Check if the bag contains dog
// Add another cat
// Test getFrequencyOf
// Add a few more strings
// test remove() once, and print
// what did the method return?
// verify size
// test remove("cat") once, and print
// what did the method return?
// verify size
// repeat 2 more times
// Test toArray: return the array, and print its elements
// EDGE CASES:
// remove() from a bag with one item
// remove() with an empty bag
// remove("cat") with an empty bag
// toArray() with an empty bag
}
}
Explanation / Answer
I removed the interface implements part from the LinkedBag class since BagInterface was not provided as part of the question. Add it back and include override notation in the methods, the code would work just fine.
PROGRAM CODE:
LinkedBag.java
package linkedlist;
public class LinkedBag {
private Node head;
private int size;
/*
* Constructs an empty bag by initializing head node. Initialize size to zero.
*/
public LinkedBag() {
head = new Node(null);
size = 0;
}
/*
* Returns the current size of the bag. You can return the value of size.
*/
public int getCurrentSize() {
return size;
}
/*
* Checks whether the bag is empty and returns true/false
*/
public boolean isEmpty() {
return size == 0;
}
/*
* Adds a new node to the start, because it's more efficient. Wraps a new node
* around the data. Then we make that new node the head of the list, first
* pointing it to the old head.
*/
public boolean add(String newEntry) {
// As long as there is memory remaining in the computer, this method should
// always succeed.
Node newNode = new Node(newEntry);
newNode.next = head.next;
head.next = newNode;
size++;
return true;
}
/*
* Since it's a bag, the programmer is free to choose which element to remove
* and return. So, since we want to be efficient programmers, we will remove the
* first node by advancing the head. Note the special case: if the bag is empty,
* return null.
*/
public String remove() {
Node current = head.next;
if (head.next == null) {
return null;
}
head.next = head.next.next;
size--;
return current.data;
}
/*
* THE EASIEST NODE TO REMOVE IS THE FIRST NODE. So, here is the algorithm: 1.
* Find the first occurrence of the string you want to remove. 2. COPY THE DATA
* from the first node to the node you found in step 1. 3. Remove the FIRST
* node.
* "But won't this mess up the order? Yes. But order doesn't matter. It's a bag."
* "Isn't it cheating to just copy the data? No. It's not cheating. It's clever."
*/
public boolean remove(String anEntry) {
Node temp = head;
if(size <=0)
return false;
if(head.next.data.equals(anEntry))
{
remove();
return true;
}
while (temp.next != null) {
if(!temp.next.data.equals(anEntry))
temp = temp.next;
else
{
String data = temp.next.data;
temp.next.data = head.next.data;
head.next.data = data;
remove();
return true;
}
}
return false;
}
/*
* Just set the head to null (and the size to 0). "But won't this leave a bunch
* of nodes hanging around in memory? No. As soon as we stop pointing to the
* first node, it will get marked as garbage so that memory can be cleaned up."
*/
public void clear() {
head = null;
size = 0;
}
// Returns a count of the number of items equal to anEntry
public int getFrequencyOf(String anEntry) {
Node temp = head;
int count = 0;
while (temp.next != null) {
temp = temp.next;
if (temp.data.equals(anEntry)) {
count++;
}
}
return count;
}
// Returns whether or not anEntry appears in this bag
public boolean contains(String anEntry) {
Node temp = head;
while (!temp.next.data.equals(anEntry)) {
temp = temp.next;
if (temp.next == null) {
return false;
}
}
return true;
}
// Returns an array consisting of the elements in the bag
// If there are no items, return an array of size 0. This method
// should never return null.
public String[] toArray() {
String[] str;
if (size == 0) {
str = new String[0];
return str;
}
int i = 0;
str = new String[size];
Node temp = head;
while (temp.next != null) {
str[i] = temp.next.data;
temp = temp.next;
i++;
}
return str;
}
public String toString() {
String result = "";
Node current = head;
while (current.next != null) {
result = result + current.next.data + " ";
current = current.next;
}
return result;
}
// A private inner class. Notice that
// the outer class can freely access its inner class, even
// though it's private.
private class Node {
private String data;
private Node next;
private Node(String data) {
this.data = data;
}
}
}
LinkedBagSimpleTester.java
package linkedlist;
import java.util.Arrays;
public class LinkedBagSimpleTester {
public static void main(String[] args) {
// Testing you should do to make sure your code
// is working before submitting it:
// Empty bag
LinkedBag bag = new LinkedBag();
// Add some items, print the bag.
bag.add("cat");
bag.add("horse");
System.out.println(bag);
// Check the size
if(bag.getCurrentSize() == 2)
System.out.println("Size is 2");
else System.out.println("size is wrong");
// Check if the bag contains cat
if(bag.contains("cat"))
System.out.println("Bag contains cat");
else System.out.println("Contains is not working");
// Check if the bag contains horse
if(bag.contains("horse"))
System.out.println("Bag contains horse");
else System.out.println("Contains is not working");
// Check if the bag contains dog
if(!bag.contains("dog"))
System.out.println("Bag does not contain dog");
else System.out.println("Contains is not working");
// Add another cat
bag.add("cat");
// Test getFrequencyOf
if(bag.getFrequencyOf("cat") == 2)
System.out.println("Frequency of cat is 2");
else System.out.println("getFrequencyOf() is not working");
// Add a few more strings
bag.add("lion");
bag.add("tiger");
System.out.println(bag.toString());
// test remove() once, and print
bag.remove();
System.out.println(bag.toString());
// what did the method return?
// verify size
if(bag.getCurrentSize() == 4)
System.out.println("Size is 4");
else System.out.println("size is wrong");
// test remove("cat") once, and print
bag.remove("cat");
System.out.println(bag.toString());
// what did the method return?
// verify size
if(bag.getCurrentSize() == 3)
System.out.println("Size is 3");
else System.out.println("size is wrong");
// repeat 2 more times
bag.remove("cat");
if(bag.getCurrentSize() == 2)
System.out.println("Size is 2");
else System.out.println("size is wrong");
bag.remove("cat");
if(bag.getCurrentSize() == 2)
System.out.println("Size is 2");
else System.out.println("size is wrong");
// Test toArray: return the array, and print its elements
String items[] = bag.toArray();
for(int i=0; i<items.length; i++)
System.out.print(items[i] + " ");
System.out.println();
// EDGE CASES:
// remove() from a bag with one item
bag.remove();
// remove() with an empty bag
bag.remove();
// remove("cat") with an empty bag
bag.remove("cat");
// toArray() with an empty bag
System.out.println(Arrays.toString(bag.toArray()));
}
}
OUTPUT:
horse cat
Size is 2
Bag contains cat
Bag contains horse
Bag does not contain dog
Frequency of cat is 2
tiger lion cat horse cat
lion cat horse cat
Size is 4
lion horse cat
Size is 3
Size is 2
Size is 2
horse lion
[]
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.