The more you program, the more time you will spend debugging. Even with carefully planned programs, there will be errors that will be difficult to find. Especially difficult to find are the bugs that do not cause compilation errors, but instead affect the integrity of the program. A debugger is a tool within the IDE (a program like DrJava) that can help you walk through a program and keep track of certain values so that you can hopefully figure out where you went wrong.
> Practice p = new Practice() > p.loop()
> Practice p = new Practice() > p.loop()
Download ScrabbleScorer.java and open it in DrJava. ScrabbleScorer.java compiles, but does not always run properly. The inputs provided below should illuminate most of the problems with the program. You should observe the following:
| Input | Expected output | Actual output |
|---|---|---|
| "HiThere" | 13 | 28 |
| "HelloWorld" | 17 | 47 |
| "score+=1;" | 7 | 50 |
| "kahnnnn!" | 14 | 24 |
The "Expected output" comes from manually adding the scores for alphabetical characters, as listed in the comments in ScrabbleScorer.java. Note that punctuation characters should score 0 points. The "Actual output" is the value output by the program.
The code in the file ScrabbleScorer.java contains at least three logical errors (also called "bugs"). Try to find them all with the debugger, make sure to test other inputs besides just those provided, they may not lead you to all the bugs. Think about where it makes sense to put your breakpoints and which variables you need to track.
Download Blackjack.java and open it in DrJava. Blackjack.java compiles, but does not run properly. The following interactions illustrate how it should run. Use the debugger to find the errors. (Note: because it involves the use of a psuedo-random number generator, the exact numbers in the outputs will not match, but the general functionality should)
> Blackjack game = new Blackjack()
> game.getRecord()
You have won: 0 games and lost: 0 games
> game.dealHand()
You have: 5 1 for a total of: 6
Dealer shows: 10, what would you like to do?
> game.hit()
You have: 5 1 7 for a total of: 13
Dealer shows: 10, what would you like to do?
> game.hit()
You busted.
You have: 5 1 7 10 for a total of: 23
Dealer shows: 10, 10, for a total of: 20
You lost.
> game.dealHand()
You have: 5 3 for a total of: 8
Dealer shows: 1, what would you like to do?
> game.hit()
You have: 5 3 2 for a total of: 10
Dealer shows: 1, what would you like to do?
> game.hit()
You have: 5 3 2 3 for a total of: 13
Dealer shows: 1, what would you like to do?
> game.stay()
You have: 5 3 2 3 for a total of: 13
Dealer shows: 2, 1, 8, 3, 8, for a total of: 22
You won.
> game.dealHand()
You have: 6 7 for a total of: 13
Dealer shows: 10, what would you like to do?
> game.hit()
You busted.
You have: 6 7 10 for a total of: 23
Dealer shows: 4, 10, for a total of: 14
You lost.
> game.getRecord()
You have won: 1 games and lost: 2 games
> game.getCardsUsed()
20
> game.reset()
> game.getRecord()
You have won: 0 games and lost: 0 games
> game.getCardsUsed()
0
> game.dealHand()
You have: 3 6 for a total of: 9
Dealer shows: 3, what would you like to do?
> game.hit()
You have: 3 6 1 for a total of: 10
Dealer shows: 3, what would you like to do?
> game.hit()
You have: 3 6 1 7 for a total of: 17
Dealer shows: 3, what would you like to do?
> game.stay()
You have: 3 6 1 7 for a total of: 17
Dealer shows: 6, 3, 10, for a total of: 19
You lost.
> game.getRecord()
You have won: 0 games and lost: 1 games
> game.getCardsUsed()
7
>
AGAIN NOTE: Simply following the interactions above will not necessary lead you to all the errors. There are some cases not illustrated above that are faulty in the program.