Question: Java LinkedBag (Code is needed for LinkedBag and WordGame) Implement three methods you wrote for for LinkedBag to give users the ability to remove a

Java LinkedBag (Code is needed for LinkedBag and WordGame)

Implement three methods you wrote for for LinkedBag to give users the ability to remove a particular item, or to remove or retrieve a random item.

boolean remove (T item);

T removeRandom();

T getRandom();

Then test them using Bags.java.

Here's what's in our bag

[lime, coconut, kiwi, grape, orange]

Does our bag contain the word 'kiwi'? yes

Does our bag contain the word 'mango'? no

Selecting an item (always same)

lime

lime

Selecting a random item (varies)

coconut

grape

Removing 'grape' from the bag

[coconut, kiwi, lime, orange]

Removing an item (always front one)

coconut

[kiwi, lime, orange]

Removing a random item

kiwi

[lime, orange]

Let's empty the bag [ ]

Trying to get a random item

null

Trying to remove a random item

null

Trying to remove 'kiwi'

false

One use for removing random items is a word game like Scrabble in which players select seven tiles for their hand. The file WordGame.java contains a method for adding the Scrabble letter tiles to a bag. Implement a method that randomly removes seven letters from the bag and adds them to a players hand:

public static void selectLetters(Bag let, Bag player)

Each time the program is run, it should pick seven random letters:

Your letters are: [A, N, T, G, I, H, R]

Your letters are: [N, D, C, J, T, A, A]

Your letters are: [E, I, E, D, T, S, R]

//Interface for LinkedBag

public interface Bag { void add(T item);

T remove();

T get();

boolean contains(T item);

int size();

public String toString();

boolean remove(T item);

T removeRandom();

T getRandom(); }

//

/* * Bags (driver program) * * program for testing methods in LinkedBag */

public class Bags { public static void main(String[] args) { try { Bag words = new LinkedBag(); // Bag words = new LinkedBag();

// adding to bag String[] fruits = { "orange", "grape", "kiwi", "coconut", "lime" }; for (int i = 0; i < fruits.length; i++) { words.add(fruits[i]); } System.out.println(" Here's what's in our bag " + words);

// seeing if bag contains item boolean result = words.contains("kiwi"); System.out.println(" Does our bag contain the word 'kiwi'? " + (result ? "yes" : "no")); result = words.contains("mango"); System.out.println("Does our bag contain the word 'mango'? " + (result ? "yes" : "no"));

// retrieving item System.out.println(" Selecting an item (always same)"); System.out.println(words.get()); System.out.println(words.get());

// retrieving random item System.out.println(" Selecting a random item (varies)"); System.out.println(words.getRandom()); System.out.println(words.getRandom());

// removing specific item words.remove("grape"); System.out.println(" Removing 'grape' from the bag " + words);

// removing item System.out.println(" Removing an item (always end one)"); System.out.println(words.remove()); System.out.println(words);

// removing random item System.out.println(" Removing a random item"); System.out.println(words.removeRandom()); System.out.println(words + " ");

// testing methods on empty bag System.out.println("Let's empty the bag"); words.remove(); words.remove(); System.out.println(words); System.out.println("Trying to get a random item " + words.getRandom()); System.out.println("Trying to remove a random item " + words.removeRandom()); System.out.println("Trying to remove 'kiwi' " + words.remove("kiwi")); } catch (IllegalArgumentException e) { System.out.println(e.getMessage()); } } }

//

/* * LinkedBag *only 3 methods need additional code: boolean remove(T item), T removeRandom(), T getRandom() * linked structure implementation of a bag * * unordered collection with duplicates allowed */

public class LinkedBag implements Bag { /* * inner class to represent node */

private class Node { private T data; private Node next;

public Node(T item) { data = item; next = null; } }

// instance variables for bag private Node head; private int size;

/* * creates bag */

public LinkedBag() { head = null; size = 0; }

/* * adds item to bag */

public void add(T item) { checkForNull(item); Node newest = new Node(item); newest.next = head; head = newest; size++; }

/* * removes item from bag * * returns removed item, null if empty */

public T remove() { if (size == 0) { return null; }

T removed = head.data; head = head.next; size--; return removed; }

/* * returns item from bag */

public T get() { if (size == 0) { return null; }

return head.data; }

/* * returns true if item in bag, false otherwise */

public boolean contains(T item) { Node current = head;

for (int i = 0; i < size; i++) { if (current.data.equals(item)) { return true; }

current = current.next; }

return false; }

/* * returns size of bag */

public int size() { return size; }

/* * returns string representation of contents of bag */

public String toString() { if (size == 0) { return "[ ]"; }

String c = "["; Node current = head;

while (current.next != null) { c += current.data + ", "; current = current.next; }

return c + current.data + "]"; }

/* methods to be added */

/* * removes specified item from bag * * returns false if item not present */

public boolean remove(T item) { return false; }

/* * removes random item from bag * * returns null if bag empty */

public T removeRandom() { return null; }

/* * returns random item from bag * * returns null if bag empty */

public T getRandom() { return null; }

/* end of added methods */

/* * checks if item is null and throws exception if so */

private void checkForNull(T item) { if (item == null) { throw new IllegalArgumentException("null not a possible value!"); } } }

//Wordgame

/* * WordGame * * beginnings of a Scrabble-like word game * * adds the letter tiles from Scrabble to a bag and randomly selects * seven for the user's hand */

public class WordGame { // number of letters in player's hand in Scrabble public static final int LETTERS_PER_TURN = 7;

// distribution of Scrabble tiles public static final char[][] ENGLISH_LETTERS = { { '1', 'K', 'J', 'X', 'Q', 'Z' }, { '2', 'B', 'C', 'M', 'P', 'F', 'H', 'V', 'W', 'Y' }, { '3', 'G' }, { '4', 'L', 'S', 'U', 'D' }, { '6', 'N', 'R', 'T' }, { '8', 'O' }, { '9', 'A', 'I' }, { '0', 'E' } };

/* * adds letter tiles to bag according to their distribution */

public static void addLetters(Bag b, char[][] let) { // cycle through set of letters for (int i = 0; i < let.length; i++) { // get count for letter group int count = let[i][0] - '0'; if (count == 0) count = 12;

// for each letter in group for (int j = 1; j < let[i].length; j++) { // add it to the bag the specified number of times for (int k = 0; k < count; k++) { b.add(let[i][j]); } } } }

/* * removes seven letters from bag and adds them to user's hand */

public static void selectLetters(Bag let, Bag player) {

}

public static void main(String[] args) { // add letters to bag Bag letters = new LinkedBag(); addLetters(letters, ENGLISH_LETTERS);

// give player seven random letters Bag playerLetters = new LinkedBag(); selectLetters(letters, playerLetters); System.out.println("Your letters are: " + playerLetters); } }

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!