Java Reference
In-Depth Information
When designing methods within classes, always think about delegation of
responsibility . A class is only responsible for manipulating its own data. If Java
code manipulates data from a different class, then consider moving the code to the
other class.
The convert method remains unchanged. The novelty is that a DiceCup object is created
in the main method. The existence of the DiceCup class allows us to now create a game
with several dice cups, if needed.
6.8 Multi-Class Solution to the Battleship Game
Next, we will examine a multi-class solution to the Battleship game, which was described
in Chapter 5. We will implement a simple version of the program that supports a single
board that is controlled by the computer. Here is an excerpt from an example run of the
program (user input is in italic).
0123456789
----------
0|OOOOOOOOOO
1|OOOOOOOOOO
2|OOOOOOOOOO
3|OOOOOOOOOO
4|OOOOOOOOOO
5|OOOOOOOOOO
6|OOOOOOOOOO
7|OOOOOOOOOO
8|OOOOOOOOOO
9|OOOOOOOOOO
Enter guess:
55
Ship Damaged!
0123456789
----------
0|OOOOOOOOOO
1|OOOOOOOOOO
2|OOOOOOOOOO
3|OOOOOOOOOO
4|OOOOOOOOOO
5|OOOOOXOOOO
6|OOOOOOOOOO
7|OOOOOOOOOO
8|OOOOOOOOOO
9|OOOOOOOOOO
Enter guess: 56
Ship Damaged!
0123456789
----------
0|OOOOOOOOOO
 
Search WWH ::




Custom Search