Subject Computer Science Java Programming

Question

1. (20 marks, CardDeckTester) Consider the classes included in the q1 package. These include Card, CardDeck and CardDeckTester. Card represents a playing card, while CardDeck is a simple collection class that represents … wait for it … a deck of cards. The CardDeck class uses an array and you should take a minute to review how it works. We’ve discussed in the lectures how a linked list is a data structure “engine”. Your task in this question is to re-work the CardDeck class to use Java’s built-in LinkedList collection class instead of the array that is there now. Here are a few considerations:
 You need to change the data field in CardDeck to a LinkedList (don’t forget any necessary import statements).
 The constructor needs to be rewritten. Start with an empty LinkedList and add all 52 cards in order.
Look at how the CardDeck constructor works now to know what order they should be put in. Keep in mind that a Card takes a single integer as an argument that determines both the suit and the rank.
 Shuffle() needs to be rewritten. You’ll need to find a way to randomize the cards without losing any of them. You could try using an iterator to grab a card from a random location then moving to a new random location in the list and adding the card back in – but that’s just one possible approach.
 Deal() needs a bit of work too – as with the current method, you need to remove the top card and return it. The top Card must leave the list – we don’t deal cards AND keep them in the deck unless we are playing at a crooked table.
 cardsInDeck() needs to return the number of cards left in the deck. This ~could~ be used when dealing to figure out if there are enough cards left to deal.
 Empty() should return true if there are no cards left in the deck.
 toString() should be rewritten. Take a look at the existing one. It prints out the cards left in the deck.
You don’t need to worry about the rank and suit, they are part of the Card class. I suggest an Iterator here to walk through the LinkedList.
Just to be clear – ALL of your modifications should be done in the CardDeck class. Do NOT modify Card or CardTester. The markers will run CardTester to make sure that your class works properly. Again, the idea here is to recognize that we can change the engine of a data structure without changing its external interface (i.e., it still works the same). As you are modifying CardDeck, comment out some of the lines in CardDeckTester so that it’s not trying to test methods you haven’t adjusted yet.
2. (30 marks, ArrayBag, DiceArrayBagTester) Every ~real~ gamer carries around a bag of dice just in case of a game-emergency. Your job in this question is to rewrite the IntArrayBag class so that it is generic and can hold any type of object. This means you need to change the data datafield and then correct all of the methods that use it (12 marks). Then, when this is done, write a GUI that allows you to create a bag of dice objects. This means your GUI has the following functionality:
a. Create an empty ArrayBag. It is ok to have it pre-set to use dice as in ArrayBag<Die>.
b. (3 marks) The GUI should have inputs so that you can create dice and add them to the bag. You can use text fields but this might be a good place for radio buttons or a drop down menu since there are only a few valid dice configurations.
c. (3 marks) The GUI should allow you to grab a random die from the bag and roll it. This can be done with a single click – grab-and-roll. Remember, the grab() method doesn’t actually remove the Die from the bag.
d. (3 marks) The GUI should allow you to remove a die from the bag. For this you’ll need to specify the configuration of die being removed – i.e., removing a 6-sided die.
e. (3 marks) The GUI should allow you to print the dice that are in the bag.
f. (3 marks) The GUI should display (on demand) the number of occurrences of a particular Die configuration (ask the user for the configuration of interest).
g. (3 marks) For good measure your GUI should also allow you to print out the number of dice in the bag and the current capacity of the bag.
You are given the Die class. No modifications should be needed for this class. You are given the IntArrayBag class to use as a reference. I recommend that you copy this class so you can use it as a reference.
***Leave this class with an array engine, DO NOT change it to a LinkedList.***

Solution Preview

This material may consist of step-by-step explanations on how to solve a problem or examples of proper writing, including the use of citations, references, bibliographies, and formatting. This material is made available for the sole purpose of studying and learning - misuse is strictly forbidden.

package question2;


public class ArrayBag<T> implements Cloneable {

    private T[] data;
    private int manyItems;

    /**
    * Initialize an empty bag with an initial capacity of 10. Note that the
    * addItem method works efficiently (without needing more memory) until this
    * capacity is reached.
    *
    * @param - none
    * @postcondition This bag is empty and has an initial capacity of 10.
    * @exception OutOfMemoryError Indicates insufficient memory for: new
    * T[10].
   *
    */
    public ArrayBag() {
       final int INITIAL_CAPACITY = 10;
       manyItems = 0;
       data = (T[])new Object[INITIAL_CAPACITY];
    }
   
    /**
    * Initialize an empty bag with a specified initial capacity. Note that the
    * addItem method works efficiently (without needing more memory) until this
    * capacity is reached.
    *
    * @param initialCapacity the initial capacity of this bag
    * @precondition initialCapacity is non-negative.
    * @postcondition This bag is empty and has the given initial capacity.
    * @exception IllegalArgumentException Indicates that initialCapacity is
    * negative.
    * @exception OutOfMemoryError Indicates insufficient memory for: new
    * T[initialCapacity].
   *
    */
    public ArrayBag(int initialCapacity) {
       if (initialCapacity < 0) {
            throw new IllegalArgumentException("The initialCapacity is negative: " + initialCapacity);
       }
       data = (T[])new Object[initialCapacity];
       manyItems = 0;
    }
...

This is only a preview of the solution. Please use the purchase button to see the entire solution

Assisting Tutor

Related Homework Solutions

ArrayList Fraction Objects in Java
Homework Solution
$15.00
Java
Programming
Computer Science
Coding
Arrays
Fractions
Numerator
Denominator
Decimal Values
Sorted List
Algorithms
Loops
Variables
Statements
Conditions
Parking Problem in Java
Homework Solution
$30.00
Java
Programming
Codes
Classes
Computer Science
Parking
OOP
Car Model
License Number
Owner Name
Strings
Integers
Statements
Input
Output
Constructor
Public Methods
Functions
Java Problems With Stack
Homework Solution
$40.00
Java
Programming
Coding
Computer Science
Stack
Generic Types
Push
Pop
Peek
Size
Linked List
Functions
Strings
Integers
Summation
Negative Values
Input
Output
Get help from a qualified tutor
Live Chats