Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

java question: /* * WordGame * * beginnings of a Scrabble-like word game * * adds the letter tiles from Scrabble to a bag and

java question: image text in transcribed

/* * 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, Bag player) { } public static void main (String [] args) { // add letters to bag Bag letters = new ArrayBag(); addLetters(letters, ENGLISH_LETTERS); // give player seven random letters Bag playerLetters = new LinkedBag(); selectLetters(letters, playerLetters); System.out.println("Your letters are: " + playerLetters); } } 

import java.util.Iterator;

public interface Bag { void add (T item); T remove(); T get(); boolean contains (T item); int size(); String toString();

boolean remove (T item); T removeRandom(); T getRandom(); }

import java.util.Random;

public class LinkedBag implements Bag

{

private class Node {

private T data;

private Node next;

public Node(T item) {

data = item;

next = null;

}

}

private Node head;

private int size;

public LinkedBag() {

head = null;

size = 0;

}

public void add(T item) {

checkForNull(item);

Node newest = new Node(item);

newest.next = head;

head = newest;

size++;

}

public T remove() {

if (size == 0)

return null;

T removed = head.data;

head = head.next;

size--;

return removed;

}

public T get() {

if (size == 0)

return null;

return head.data;

}

public boolean contains(T item) {

Node current = head;

for (int i = 0; i

if (current.data.equals(item))

return true;

current = current.next;

}

return false;

}

public int size() {

return size;

}

public String toString() {

if (size == 0)

return "[ ]";

String b = "[";

Node current = head;

while (current.next != null) {

b += current.data + ", ";

current = current.next;

}

return b + current.data + "]";

}

@Override

public boolean remove(T item) {

if (size == 0) {

return false;

}

if (head.data == item) {

head = head.next;

size -- ;

return true;

}

Node current = head;

while (current.next != null && current.next.data != item) {

current = current.next;

}

if (current.next == null) {

return false;

}

current.next = current.next.next;

System.gc();

size -- ;

return true;

}

@Override

public T removeRandom() {

if (size == 0) {

return null;

}

T randomItem = getRandom();

remove(randomItem);

return randomItem;

}

@Override

public T getRandom() {

if (size == 0) {

return null;

}

int randomPosition = new Random().nextInt(size);

int count = 0;

Node current = head;

while (current.next != null && count

current = current.next;

count++;

}

return current.data;

}

private void checkForNull(T item) {

if (item == null)

throw new IllegalArgumentException("null not a possible value!");

}

}

import java.util.Random;

public class ArrayBag implements Bag

{

public static final int DEFAULT_CAPACITY = 10;

private T [] collection;

private int size;

/*

* default constructor * * bag initially set to 10 items */

public ArrayBag()

{

this(DEFAULT_CAPACITY);

}

/*

* argument constructor * size of bag specified by user */

@SuppressWarnings("unchecked")

public ArrayBag(int capacity)

{

if (capacity

{

throw new IllegalArgumentException("bag can't be negative size!");

}

collection = (T []) new Object [capacity];

size = 0;

}

/* * adds item to bag * if at capacity, bag is resized */

public void add (T item)

{

checkForNull(item);

ensureSpace();

collection[size] = item;

size++;

}

/*

* removes item from bag * returns removed item */

public T remove ()

{

if (size == 0)

{

return null;

}

T removed = collection[size-1];

collection[size-1] = null;

size--;

return removed;

}

/* * returns item from bag */

public T get()

{

if (size == 0)

{

return null;

}

return collection[size-1];

}

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

public boolean contains(T item)

{

for (int i = 0; i

{

if (collection[i].equals(item))

{

return true;

}

}

return false;

}

/* * returns size of bag */

public int size()

{

return size;

}

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

public String toString()

{

String s = "[";

for (int i = 0; i

{

s+= collection[i];

if (i

{

s+= ", ";

}

}

return s + "]";

}

/* methods to be added */ /* * removes specified item from bag * * returns false if item not present */

public boolean remove (T item)

{

for(int i=0; i

{

if(item.equals(collection[i]))

{

for(int j=i; j

{

collection[j] = collection[j+1];

}

collection[size-1] = null;

size--;

return true;

}

}

return false;

}

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

public T removeRandom ()

{

if(size == 0)

return null;

T item = collection[new Random().nextInt(size)];

remove(item);

return item;

}

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

public T getRandom ()

{

if(size == 0)

return null;

return collection[new Random().nextInt(size)];

}

/* 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!");

}

}

/* * doubles size of array if at capacity */ private void ensureSpace()

{

if (size == collection.length)

{

@SuppressWarnings("unchecked")

T [] larger = (T []) new Object [size * 2];

for (int i = 0; i

{

larger[i] = collection[i];

}

collection = larger;

larger = null;

System.out.println("Hi");

}

}

}

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 player's hand: public static void selectLetters(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]

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_step_2

Step: 3

blur-text-image_step3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions

Question

=+ Why have these changes occurred?

Answered: 1 week ago

Question

=+90 percent of all oil refineries) into several smaller companies

Answered: 1 week ago