19.09.2015 Views

Prentice.Hall.Introduction.to.Java.Programming,.Brief.Version.9th.(2014).[sharethefiles.com]

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

390 Chapter 10 Thinking in Objects<br />

Is your birthday in Set3?<br />

4 5 6 7<br />

12 13 14 15<br />

20 21 22 23<br />

28 29 30 31<br />

Enter 0 for No and 1 for Yes:<br />

Is your birthday in Set4?<br />

8 9 10 11<br />

12 13 14 15<br />

24 25 26 27<br />

28 29 30 31<br />

Enter 0 for No and 1 for Yes:<br />

Is your birthday in Set5?<br />

16 17 18 19<br />

20 21 22 23<br />

24 25 26 27<br />

28 29 30 31<br />

Enter 0 for No and 1 for Yes:<br />

Your birthday is 26<br />

0<br />

1<br />

1<br />

Since getValue is a static method, you don’t need <strong>to</strong> create an object in order <strong>to</strong> invoke it.<br />

GuessDate.getValue(i, j, k) (line 15) returns the date at row j and column k in Set i.<br />

The GuessDate class can be implemented as in Listing 10.10.<br />

static field<br />

private construc<strong>to</strong>r<br />

LISTING 10.10<br />

GuessDate.java<br />

1 public class GuessDate {<br />

2 private final static int[][][] dates = {<br />

3 {{ 1, 3, 5, 7},<br />

4 { 9, 11, 13, 15},<br />

5 {17, 19, 21, 23},<br />

6 {25, 27, 29, 31}},<br />

7 {{ 2, 3, 6, 7},<br />

8 {10, 11, 14, 15},<br />

9 {18, 19, 22, 23},<br />

10 {26, 27, 30, 31}},<br />

11 {{ 4, 5, 6, 7},<br />

12 {12, 13, 14, 15},<br />

13 {20, 21, 22, 23},<br />

14 {28, 29, 30, 31}},<br />

15 {{ 8, 9, 10, 11},<br />

16 {12, 13, 14, 15},<br />

17 {24, 25, 26, 27},<br />

18 {28, 29, 30, 31}},<br />

19 {{16, 17, 18, 19},<br />

20 {20, 21, 22, 23},<br />

21 {24, 25, 26, 27},<br />

22 {28, 29, 30, 31}}};<br />

23<br />

24 /** Prevent the user from creating objects from GuessDate */<br />

25<br />

26<br />

private GuessDate() {<br />

}<br />

27<br />

28 /** Return a date at the specified row and column in a given set */

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!