Title: Homework 10
1Homework 10
Sun., 11/10
( MT sections )
at midnight
Mon., 11/11
( WTh sections )
http//www.cs.hmc.edu/courses/2002/fall/cs5/week_1
0/homework.html
names and hours linked from the CS 5 Syllabus
Fri., Sat. afternoons Lac Lab and Parsons
(1-4) Sunday afternoons Lac Lab and
Parsons (1-4) Sunday evenings Lac Lab
and Parsons (7-12) Monday evenings Lac
Lab and Parsons (7-12)
2Problem 2 Connect Four
An object of the Board class...
XO X XO XOOOX
--------------- 0 1 2 3 4 5 6
each player is a char -- either X or O
underlying data a 2d array of chars
the user chooses the number of rows and columns
(between 4 and 15 inclusive)
3Problem 2 CS5App class
class CS5App public static void main(String
s) int rows H.in.nextInt() int
cols H.in.nextInt() Board b new
Board(rows,cols) while (playing)
b.print() // print out the board // get a
move from X // add it to the board
// see if X wins or b is full // do the
same for O
The constructor for objects of type Board
4The Board class
XO X XO XOOOX
--------------- 0 1 2 3 4 5 6
b.print()
char
char
char
char
char
data
char
char
char
char
Board b
int
void print()
int
nRows
nCols
boolean isFull()
boolean allowsMove(int c)
void addMove(int c, char player)
boolean winsFor(char player)
5The Board class
class Board private char data //
private data members private int nRows
private int nCols Board(int rows, int cols)
// CONSTRUCTOR ! this.nRows rows
this.nCols cols this.data new
charrowscols for (int r0
rltthis.nRows r) for (int c0
cltthis.nCols c) this.datarc
6 XO X XO XOOOX
--------------- 0 1 2 3 4 5 6
print
this.nRows is the rows
this.data is the array of pieces or spaces
class Board public void print()
other code here
this.nCols is the cols
7 X XO OX
XO X XO XOOOX
--------------- 0 1 2 3 4 5 6
allowsMove
class Board // previous Board code
public boolean allowsMove(int col)
8 XO X XO XOOOX
--------------- 0 1 2 3 4 5 6
addMove
class Board // previous Board code
public void addMove(int col, char player)
9 OXO XXXXO XOOOXO
--------------- 0 1 2 3 4 5 6
winsFor
class Board // previous Board code
public boolean winsFor(char player)
10Problem 2 main loop
Board b new Board(rows,cols) while
(playing) b.print() //
print out the board
// get a move from X
// add it to the board
// check
win or tie // repeat
for O
11Problem 1
Similar to the last two problem 1s, but using
Date objects
class CS5App
class Date
Methods
Methods
sets things up and runs a large while loop
Date
the constructor
main
prints
print
printMenu
prints
tomorrow
forward 1 day
(0) Enter a new date of interest (1) Print the
current date of interest (2) Move one day forward
in time (3) Move one day backward in time (4) Day
difference finder (5) Find the day of the
week (9) Quit
yesterday
backward 1 day
isBefore
helper method
of days difference between 2 Dates
diff
dayOfWeek
returns a String
12Problem 1
// input integers mo,dy,yr Date d new
Date(mo,dy,yr) while (choice ! 9)
d.print() printMenu()
Date Calculator
Methods
sets things up and runs a large while loop
main
printMenu
prints
if (choice0) // input mo, dy, yr again d
new Date(mo,day,yr)
(0) Enter a new date of interest (1) Print the
current date of interest (2) Move one day forward
in time (3) Move one day backward in time (4) Day
difference finder (5) Find the day of the
week (9) Quit
if (choice2) d.tomorrow()
13The Date class
class Date private int month private int
day private int year public Date(int mo,
int dy, int yr) this.month mo
this.day dy this.year yr public
void print() H.out.print(this.month
/ this.day /
this.year)
data members
- initializes all of the data members
constructor
- used whenever a new object is created
other method(s)
14tomorrow()
int
month
class Date // previous Date code public
void tomorrow() this.day // add one
to the day // check if we overflowed the
month! if (this.day 32 this.month
12) this.day 1 this.month 1
this.year else if (this.day 28
this.month 2
!this.isLeapYear() ) this.month
3 this.day 1 else if ( / lots of
other cases / )
int
int
day
year
data members
15Comparing Dates
class CS5App public static void main(String
args) Date d new Date(11,8,2002) //
today Date d2 new Date(11,28,2002) //
Thanksgiving int nDays d.diff(d2)
H.out.print(Between ) d.print()
H.out.print( and ) d2.print()
H.out.println( there are nDays days.)
16diff
class Date // previous Date code public
int diff(Date d2)
What two dates are being compared here?
17dayOfWeek
class Date // previous Date code public
String dayOfWeek()
18Date
Picture of a Date object
Date d
int day
int month
int year
void print()
void tomorrow()