 |
|

|
 |

11/05/09 - Date class solution and TicTacToe
|
 |
 |
 |
  | /** * A class to represent a calendar date * with month, day, year attributes. * * @author Chuck Iverson * @version 11/4/09 */ public class Date { private static final int MIN_DAY = 1; private static final int MIN_MONTH = 1; private static final int MAX_MONTH = 12; private static final int DEFAULT_MONTH = 1; private static final int DEFAULT_DAY = 1; private static final int DEFAULT_YEAR = 1600; private static final int [] DAYS_IN_MONTH = {0,31,28,31,30,31,30,31,31,30,31,30,31}; private static final String [] DAY_NAMES = {"Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"}; private static final Date DAY_NAME_REFERENCE_DATE = new Date(1,1,1900);// a Monday private int month; private int day; private int year;
public Date() { month = DEFAULT_MONTH; day = DEFAULT_DAY; year = DEFAULT_YEAR; }
public Date(int month, int day, int year) { this.month = (month >= MIN_MONTH && month <= MAX_MONTH) ? month : MIN_MONTH; this.day = (day >= MIN_DAY && day <= getDaysInMonth(month)) ? day : MIN_DAY; this.year = (year >= DEFAULT_YEAR) ? year : DEFAULT_YEAR; } public void addDays(int days) { if (days > 0) { this.day += days; while (day > getDaysInMonth(month)) { day -= getDaysInMonth(month); ++month; if (month > MAX_MONTH) { month = MIN_MONTH; ++year; } } } else { this.day -= days; while (day < MIN_DAY) { if (month > MIN_MONTH) { --month; } else { month = MAX_MONTH; --year; } day += getDaysInMonth(month); } } } public void addWeeks(int weeks) { addDays(7*weeks); } public int daysTo(Date other) { int days = 0; Date copy = new Date(month,day,year); while (copy.lessThan(other)) { copy.addDays(1); ++days; } while (!copy.lessThan(other) && !copy.equals(other)) { copy.addDays(-1); --days; } return days; } private boolean lessThan(Date other) { if (year > other.year) { return false; } else if (year < other.year) { return true; } else if (month > other.month) { // years match return false; } else if (month < other.month) { return true; } else if (day > other.day) { // years and months match return false; } else if (day < other.day) { return true; } else { return false; // equal } } public boolean equals(Object other) { Date date = (Date)other; return (month == date.month && day == date.day && year == date.year); } public String getDayName() { int days = DAY_NAME_REFERENCE_DATE.daysTo(this); return DAY_NAMES[days % 7]; } public int getDay() { return day; } public int getMonth() { return month; } public int getYear() { return year; } public boolean isLeapYear() { return (year % 400 == 0) || (year % 4 == 0 && year % 100 != 0); } public String toString() { String s = getDayName() + ", "; if (month < 10) { s += "0"; } s += month + "/"; if (day < 10) { s += "0"; } s += day + "/" + year; return s; } private int getDaysInMonth(int month) { if (month != 2 || !isLeapYear()) { return DAYS_IN_MONTH[month]; } return 1 + DAYS_IN_MONTH[month]; } }
|
 |
 |
 |
 |
  | /** * A class to test the Date class. * * @author Chuck Iverson * @version 11/4/2009 */ public class DateTest { public static void main(String [] args) { Date lomaPrieta = new Date(10,17,1989); System.out.println("Loma Prieta Date:\n" + lomaPrieta); Date yesterday = new Date(11,3,2009); System.out.println("\nYesterday:\n" + yesterday); System.out.println("\nDays from Loma Prieta Date to yesterday:\n" + lomaPrieta.daysTo(yesterday));
lomaPrieta.addDays(2345); System.out.println("\n2345 Days from Loma Prieta Date:\n" + lomaPrieta); Date fool94 = new Date(4,1,1994); System.out.println("\nApril Fool's Day 1994:\n" + fool94); fool94.addWeeks(59); System.out.println("\nApril Fool's Day 1994 + 59 weeks:\n" + fool94); System.out.println("\nDays from April Fool's Day 1994 + 59 weeks to Loma Prieta Date + 2345 days:\n" + fool94.daysTo(lomaPrieta));
System.out.println("\nLast two dates are equal:\n" + fool94.equals(lomaPrieta)); } }
/* sample run
Loma Prieta Date: Tuesday, 10/17/1989
Yesterday: Tuesday, 11/03/2009
Days from Loma Prieta Date to yesterday: 7322
2345 Days from Loma Prieta Date: Tuesday, 03/19/1996
April Fool's Day 1994: Friday, 04/01/1994
April Fool's Day 1994 + 59 weeks: Friday, 05/19/1995
Days from April Fool's Day 1994 + 59 weeks to Loma Prieta Date + 2345 days: 305
Last two dates are equal: false
*/
|
 |
 |
 |
 |
  | Read pp. 475-491 of the text.
|
 |
 |
 |
 |
  | Create a class to play TicTacToe with the user. The user should be asked to enter two integers, a row number and a column number, for a 3-by-3 array. X's and O's should be entered alternately. Your program should check that the user is not overwriting an existing X or O. After each entry, your program should display the pattern of X's and O's. If three X's or three O's are in the same row, column or diagonal, then that person wins. Your program should check for and announce who wins. If all the cells of the array are filled without anyone getting three in a row (column or diagonal), the program should announce the result as a "Cat's game." At the end of each game, the user should be prompted to play again.
Here is a sample run of the program:
Enter the row and column numbers for an 'X' (e.g.,2 3): 2 2 - - - - X - - - - Enter the row and column numbers for an 'O' (e.g.,2 3): 2 1 - - - O X - - - - Enter the row and column numbers for an 'X' (e.g.,2 3): 1 1 X - - O X - - - - Enter the row and column numbers for an 'O' (e.g.,2 3): 2 2 That space is occupied. Try again. Enter the row and column numbers for an 'O' (e.g.,2 3): 1 1 That space is occupied. Try again. Enter the row and column numbers for an 'O' (e.g.,2 3): 3 3 X - - O X - - - O Enter the row and column numbers for an 'X' (e.g.,2 3): 3 1 X - - O X - X - O Enter the row and column numbers for an 'O' (e.g.,2 3): 1 3 X - O O X - X - O Enter the row and column numbers for an 'X' (e.g.,2 3): 2 3 X - O O X X X - O Enter the row and column numbers for an 'O' (e.g.,2 3): 1 2 X O O O X X X - O Enter the row and column numbers for an 'X' (e.g.,2 3): 3 2 X O O O X X X X O
Tie! Cat's game!
Play again (Y/N)? y
Enter the row and column numbers for an 'X' (e.g.,2 3): 2 2 - - - - X - - - - Enter the row and column numbers for an 'O' (e.g.,2 3): 3 2 - - - - X - - O - Enter the row and column numbers for an 'X' (e.g.,2 3): 1 1 X - - - X - - O - Enter the row and column numbers for an 'O' (e.g.,2 3): 3 3 X - - - X - - O O Enter the row and column numbers for an 'X' (e.g.,2 3): 3 1 X - - - X - X O O Enter the row and column numbers for an 'O' (e.g.,2 3): 2 1 X - - O X - X O O Enter the row and column numbers for an 'X' (e.g.,2 3): 1 3 X - X O X - X O O
X won!
Play again (Y/N)? y
Enter the row and column numbers for an 'X' (e.g.,2 3): 2 1 - - - X - - - - - Enter the row and column numbers for an 'O' (e.g.,2 3): 2 2 - - - X O - - - - Enter the row and column numbers for an 'X' (e.g.,2 3): 3 2 - - - X O - - X - Enter the row and column numbers for an 'O' (e.g.,2 3): 3 1 - - - X O - O X - Enter the row and column numbers for an 'X' (e.g.,2 3): 1 3 - - X X O - O X - Enter the row and column numbers for an 'O' (e.g.,2 3): 3 3 - - X X O - O X O Enter the row and column numbers for an 'X' (e.g.,2 3): 2 3 - - X X O X O X O Enter the row and column numbers for an 'O' (e.g.,2 3): 1 1 O - X X O X O X O
O won!
Play again (Y/N)? n
Done!
Here is the test program for the TicTacToe class:
/** * TicTacToeTest * * @author Chuck Iverson * @version 11/5/09 */
public class TicTacToeTest { public static void main(String [] args) { TicTacToe t = new TicTacToe(); } }
|
 |
 |
|


 |
 |
 |