Hello! And a massive thank in advance for any help!
I've been banging my head against this for quite a while now. I'm trying to figure out why the output for this testdriver I created for this card class keeps showing an extra card. I need it to print five random cards (which it does), and then four more random cards and one invalid, which it also does, but it adds one more random card at the end which shouldn't be there. Just learning so all help so appreciated.
Here is the code I've written so far with the Card class at the top and the TestDriver at the bottom.
Your program isn't printing an extra card, it's printing the default card you set after you print after the warning.
What you want is a way to stop the creation of the Card if it's invalid. You do this by throwing an exception. Do you know how to do that? Give it a try.
All things are lawful, but not all things are profitable.
To clarify what Knute said, on line 124 you're invoking the Card constructor on line 16. Because the parameters are invalid, line 19 is executed which displays the "Invalid" message. However, as Knute said, the Card is created anyway with a face and suit of 1. When line 125 is executed, it causes line 97 to be executed thus printing your "extra" card, the Ace of Clubs.
The best ideas are the crazy ones. If you have a crazy idea and it works, it's really valuable.—Kent Beck