Attached as well is a picture of what the tiles look like: Intarraybag.java: pub
ID: 3890730 • Letter: A
Question
Attached as well is a picture of what the tiles look like:
Intarraybag.java:
public class IntArrayBag implements Cloneable
{
// instance variables
private int[ ] data;
private int manyItems;
// constructor : behavior #1
public IntArrayBag( )
{
final int INITIAL_CAPACITY = 10;
manyItems = 0;
data = new int[INITIAL_CAPACITY];
}
// another constructor : behavior #1
public IntArrayBag(int initialCapacity)
{
if (initialCapacity < 0)
throw new IllegalArgumentException
("The initialCapacity is negative: " + initialCapacity);
data = new int[initialCapacity];
manyItems = 0;
}
// add one item : behavior #2
public void add(int element)
{
if (manyItems == data.length)
{ // Ensure twice as much space as we need.
ensureCapacity((manyItems + 1)*2);
}
data[manyItems] = element;
manyItems++;
}
// add many items : behavior #3
public void addMany(int... elements)
// this is a variable arity method. We can specify as many parameters
// as needed
{
if (manyItems + elements.length
> data.length)
{ // Ensure twice as much space as we need.
ensureCapacity((manyItems + elements.length)*2);
}
System.arraycopy(elements, 0, data, manyItems, elements.length);
manyItems += elements.length;
}
public void addAll(IntArrayBag addend)
{
ensureCapacity(manyItems + addend.manyItems);
System.arraycopy(addend.data, 0, data, manyItems, addend.manyItems);
manyItems += addend.manyItems;
}
public IntArrayBag clone( )
{ // Clone an IntArrayBag object.
IntArrayBag answer;
try
{
answer = (IntArrayBag) super.clone( );
}
catch (CloneNotSupportedException e) {
throw new RuntimeException
("This class does not implement Cloneable");
}
answer.data = data.clone( );
return answer;
}
// see the remainder of the IntArrayBag.java file in
// the Sept 7 notes
public int countOccurrences(int target)
{
int count = 0;
for (int index = 0; index < manyItems; index++)
if (target == data[index])
count++;
return count;
}
public void ensureCapacity(int minimumCapacity)
{
int biggerArray[ ]; // declaration
if (data.length < minimumCapacity)
{
biggerArray = new int[minimumCapacity]; // allocate space
System.arraycopy(data, 0, biggerArray, 0, manyItems);
data = biggerArray;
}
}
public int getCapacity( )
{
return data.length;
}
public boolean remove(int target)
{
int index; // must declare before the loop
for (index = 0; (index < manyItems) && (target != data[index]);
index++)
;
if (index == manyItems) // target was not found
return false;
else
{
manyItems--;
data[index] = data[manyItems];
return true;
}
}
public int size( )
{
return manyItems;
}
public void trimToSize( )
{
int trimmedArray[ ];
if (data.length != manyItems)
{
trimmedArray = new int[manyItems];
System.arraycopy(data, 0, trimmedArray, 0, manyItems);
data = trimmedArray;
}
}
public static IntArrayBag union(IntArrayBag b1, IntArrayBag b2)
{
// a call to this method would look like
// IntArrayBag.union( first IntArrayBag, second IntArrayBag )
// If either b1 or b2 is null, then a NullPointerException is
// thrown.
// In the case that the total number of items is beyond
// Integer.MAX_VALUE, there will be an arithmetic overflow and
// the bag will fail.
IntArrayBag answer = new IntArrayBag(b1.getCapacity( ) +
b2.getCapacity( ));
// Dr. Steiner replaced the calls to System.arraycopy
// with two simple loops
for (int i = 0; i < b1.manyItems; i++)
answer.data[i] = b1.data[i];
for (int j = 0; j < b2.manyItems; j++)
answer.data[b1.manyItems + j] = b2.data[j];
answer.manyItems = b1.manyItems + b2.manyItems;
return answer;
}
}
Explanation / Answer
Here is the modified code as per the conditions.
Tile.java :
public class Tile implements Cloneable{
public int color;
public int shape;
// constructor to create a tile with given shape and color.
Tile(int color,int shape){
this.color = color;
this.shape = shape;
}
int getColor(){
return color;
}
int getShape(){
return shape;
}
void setColor(int color){
this.color = color;
}
void setShape(int shape){
this.shape = shape;
}
public String toString() {
return "Tile[color=" + color + " ,shape= " + shape + "]";
}
boolean compareTiles(Tile T1, Tile T2){
return T1.toString().equals(T2.toString());
}
public Object clone()throws CloneNotSupportedException{
return super.clone();
}
}
QwirkleBag.java :
import java.util.Random;
public class QwirkleBag implements Cloneable {
private int manyItems;
private Tile[] data;
// constructor : behavior #1
public QwirkleBag( )
{
final int INITIAL_CAPACITY = 108;
manyItems = 108;
data = new Tile[INITIAL_CAPACITY];
for(int i=0;i<36;i++){
for(int j=1;j<=6;j++){
for(int k=1;k<=6;k++){
data[i] = new Tile(j,k);
}
}
}
for(int i=36;i<72;i++){
for(int j=1;j<=6;j++){
for(int k=1;k<=6;k++){
data[i] = new Tile(j,k);
}
}
}
for(int i=72;i<INITIAL_CAPACITY;i++){
for(int j=1;j<=6;j++){
for(int k=1;k<=6;k++){
data[i] = new Tile(j,k);
}
}
}
}
// add one item : behavior #2
public void add(Tile element)throws CloneNotSupportedException
{
if (manyItems == data.length)
{ // Ensure twice as much space as we need.
ensureCapacity((manyItems + 1)*2);
}
element.clone();
data[manyItems] = element;
manyItems++;
}
public String toString(){
String name = "";
for(int i=0;i< manyItems;i++){
name = name + data[i].getColor()+" "+data[i].getShape()+" ";
}
return name;
}
public Object clone( )
{ // Clone an IntArrayBag object.
QwirkleBag answer;
try
{
answer = (QwirkleBag)super.clone( );
}
catch (CloneNotSupportedException e) {
throw new RuntimeException
("This class does not implement Cloneable");
}
answer.data = new QwirkleBag().data;
// Deep cloning means instead of copying into the same object, we create a new object and copy the values.
return answer;
}
// see the remainder of the IntArrayBag.java file in
public int countOccurrences(Tile target)
{
int count = 0;
for (int index = 0; index < manyItems; index++)
if (target == data[index])
count++;
return count;
}
public void ensureCapacity(int minimumCapacity)
{
Tile biggerArray[ ]; // declaration
if (data.length < minimumCapacity)
{
biggerArray = new Tile[minimumCapacity]; // allocate space
for(int i=0;i<manyItems;i++){
biggerArray[i] = (Tile)data[i];
}
data = biggerArray;
}
}
public int getCapacity( )
{
return data.length;
}
public boolean remove(Tile target)
{
int index; // must declare before the loop
for (index = 0; (index < manyItems) && (!target.equals(data[index]));
index++)
;
if (index == manyItems) // target was not found
return false;
else
{
manyItems--;
data[index] = data[manyItems];
return true;
}
}
public int size( )
{
return manyItems;
}
public void trimToSize( )
{
Tile trimmedArray[ ];
if (data.length != manyItems)
{
trimmedArray = new Tile[manyItems];
for(int i=0;i<manyItems;i++){
trimmedArray[i] = (Tile)data[i];
}
data = trimmedArray;
}
}
public Tile draw(){
if(manyItems == 0)
return null;
int random = new Random().nextInt(data.length);
Tile tile = new Tile(data[random].getColor(),data[random].getShape());
return tile;
}
}
QwirkleTester.java :
public class QwirkleTester {
public static void main(String[] args) {
QwirkleBag game1 = new QwirkleBag();
System.out.println("I create a new QwirkleBag. Here's what in it ");
System.out.println(game1);
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.