Question: Assignment: Start a new Eclipse project called lab1 - WordBag Download WordBag.java and Tester.java to your desktop. Add the two downloaded files to your Eclipse

Assignment:

  1. Start a new Eclipse project called lab1 - WordBag

  2. Download WordBag.java and Tester.java to your desktop.

  3. Add the two downloaded files to your Eclipse project.

  4. remove() and contains() both require finding the index of a particular element. So, it makes sense to create a helper method that returns that index, and then using that helper method in remove() and contains().

  5. Test thoroughly. Pay careful attention to edge cases:

    • What happens when you add to a full bag?

    • What happens when you remove from an empty bag?

 WordBag.Java public class WordBag { private String[] words; private int size; /** * The capacity of the bag if no capacity is specified */ public static final int DEFAULT_CAPACITY = 5; /** * Constructs an empty bag with the specified * capacity. * @param capacity the maximum number of elements * in the bag */ public WordBag(int capacity) { } /** * Constructs an empty bag with the default * capacity as defined by WordBag.DEFAULT_CAPACITY. */ public WordBag() { } /** * Adds the specified word to the bag, if there * is room. * @param word the word to be added * @return true if the word is successfully added, * and false otherwise (which would happen if the * bag were full) */ public boolean add(String word) { return false; } /** * Determines whether the specified word is * in the bag. * @param word the word to check for in the bag * @return true if the word exists, and false otherwise */ public boolean contains(String word) { return false; } /** * Removes an instance of the specified word * from the bag, if it exists. * @param word the word to be removed * @return true if an instance of the word was * removed, and false otherwise. */ public boolean remove(String word) { return false; } /** * Gets the number of words in the bag * @return the number of words in the bag */ public int size() { return -1; } /** * Determines whether the bag is empty. * @return true if the bag contains no words, and false otherwise */ public boolean isEmpty() { return false; } /** * Removes all words from the bag. */ public void clear() { } /** * Gets the number of times that a specified word is in the bag. * @param word the word to be checked * @return the number of times that word is in the bag */ public int getFrequency(String word) { return -1; } /** * Removes an element from the bag, if one exists. * The client should have no expectations about which * element will be returned. * @return an element from the bag, or null if the bag is empty */ public String remove() { return null; } /** * Gets an array of all the elements in the bag. * @return a String array containing all elements in the bag. If * the bag is empty, an array of length 0 will be returned. */ public String[] toArray() { return null; } /* * Returns a space-separated list of words in the bag. */ @Override public String toString() { return null; } }

Tester.Java

import java.util.Arrays; public class Tester { public static void main(String[] args) { // Things to test for: // Does a new bag have size 0 // Does contains work before and after adding an element // Successful add should cause the size to increase // Unsuccessful add should not change the size // If you add 3 items to an empty bag, toArray() should return an array // of size 3. // Removing a particular item should remove only one instance // Successful remove should cause the size to decrease // Unsuccessful remove should not change the size // Clear should change the size to 0. toArray() should then return empty array. // Edge cases: // If you try removing from an empty bag, does the size stay at 0? // If you try adding to a full bag, does the size stay at capacity? } }
8.5.7

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!