Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Purpose The purpose of this assignment is get you familiar with overloaded and default constructors. It is also geared towards the use of delegating constructors

Purpose

The purpose of this assignment is get you familiar with overloaded and default constructors. It is also geared towards the use of delegating constructors to create one code path. In addition this assignment will have you create a Deck class that is composed of an ArrayList of Cards.

Step 1 - The Constructors

In the Card class create an overloaded constructor that takes as an argument a String that represents the path to an individual Card. To keep one code path this constructor should call the setImage method which in turn calls the loadCard method

Create a default constructor that simply sets the Card to back of the card image. This should be image 155. Again you should keep one code path by delegating the constructor.

Step 2 -Modify The Game

Now that you have working constructors you can remove the call to setImage. Where the game is being reset you can simply create the Card by calling the default constructor. In the handle method you can use the overloaded constructor to load the card from the random number that was generated.

Before moving on, test to make sure that your game is not broken. If it is go back and see if you can fix whatever error you are having.

About Composition

In Object Oriented Programming there are differing types of object relationships. The first one we are going to discuss and probably the most simple is that of composition. Composition is also known as a "hasa" realtionship. It is nothing more than an object being composed of other objects. In other words having another object in the field of the class. In this next part of the assignment we are going to create a Deck class that is composed of an ArrayList that holds Cards. In other words, a Deck "hasa" ArrayList.

Step 3 - The Deck Class

Create a class called Deck. You will have to import

java.util.ArrayList

Java.util.Collection

Inside the field of the class create an int variable called index that will serve to keep track of the card to be dealt. Create a final int called LAST_CARD. A final variable is a constant and cannot be changed. Set this to a value like 45. We are going to use this value to determine when the Deck needs to be reshuffled.

We have not talked about generics yet so we will consider this a peek ahead. Create an ArrayList called deck that will hold cards. This will declared like this:

ArrayList deck = new ArrayList();

This declaration allows the ArrayList called deck to use Cards exclusively.

Step 4 Deck Methods

Create a public void method called shuffle that can be used to shuffle the deck. One of the nice things about generics in Java is that they have a common set of algorithms that can be used to perform some very useful tasks. One of those algorithms is called shuffle. This means to shuffle the deck all you need to do is call:

Collections.shuffle(deck);

inside your shuffle method.

Create a private void method called loadCards that takes as an argument the path to where the cards are stored. This path should not contain the name of the card. You are going to derive that. The loadCards method should use a loop that will add all 52 Cards to the ArrayList called deck. To add a new Card to the ArrayList you will simply use the add method and supply a new Card as an argument. It is a good thing we created the overloaded constructor.

Create a public method called deal that will return a Card. This method is an accessor so it does not need any arguments. Inside this method check to see if the index is greater than or equal to LAST_CARD. If it is then shuffle the deck and reset index.

Return the next Card in the deck by calling the get method that is part of deck. You need to supply the index as an argument. Also, don't forget to increment the index or you will always return the same Card.

Step 5 Deck Constructors

Create an overloaded constructor that takes the path to where the cards are stored. Again, this path should not contain the name of the Card. Because we want to keep one code path have this constructor call loadCards

Create a default constructor that sets the default path to "file:img\\". Again, keep one code path by delegating the constructors.

At this point you should have a working Deck of Cards.

Step 6 Test The Deck

Inside the field of the game create an instance of the Deck class. You should be able to use the default constructor. Inside the handle method you will use the deal method of deck to get a card to use. This should be as simple as calling deal, getting the value of the Card, and setting the graphics on the Label.

At this point you should have a working game that incorporates the Deck.

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_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

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

Get Started

Recommended Textbook for

Machine Learning And Knowledge Discovery In Databases European Conference Ecml Pkdd 2018 Dublin Ireland September 10 14 2018 Proceedings Part 1 Lnai 11051

Authors: Michele Berlingerio ,Francesco Bonchi ,Thomas Gartner ,Neil Hurley ,Georgiana Ifrim

1st Edition

3030109240, 978-3030109240

More Books

Students also viewed these Databases questions