This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
I am currently writing a simulation for dealing with a deck of 96 cards. 5 cards from the deck are taken initially before the do loop and put face up in a sequence next to the deck (this would be done in real life). What I want the program to do is to pick two cards in the following way per cycle in the do-while loop:
(a) pick two cards randomly from the deck
(b) pick two cards randomly from the sequence (there are conditions to this, as you will see in the code below)
(c) pick one card from the deck and one from the sequence (again, conditions, as you can see in my code below).
However, I am having difficulty in getting one particular method to work. It is my pickOneFromDeck() method, which seems to have some trouble with the cardPicked String variable. The compiler requires it to be initialized, which is fine. But whatever or whenever I DO initialize it, instead of placing a card name in the variable, it just puts the initial value (i.e., "empty space" or NULL if I set to null), in and keeps doing this.
I want it to have a name of the color card picked in that space. What am I doing incorrectly that's causing this runtime error. Here is my code below:
Here is the exception class code if you require it...
I know it is a lot to ask it seems, but this is the only part of the code that I am having trouble with. Other than that, it would work fine.
Thank you so much for your assistance and help everyone.
I look forward to hearing from all of you.
In lines 121-124
low = 0, high = 9, high-low+1 = 10
expression (Math.random()*(high-low+1) returns values between 0 and 9.999999
9.99999 casted to (int) gives 9
Since the array cardType has 9 elements (from 0 to 8), sometimes the program aborts with index out of bound exception
trying to get cell 9 from the array.