Download presentation
Presentation is loading. Please wait.
Published byTodd Todd Modified over 8 years ago
1
Homework 10 Due ( MT sections ) ( WTh sections ) at midnight Sun., 11/10 Mon., 11/11 Problems http://www.cs.hmc.edu/courses/2002/fall/cs5/week_10/homework.html Tutors available 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) names and hours linked from the CS 5 Syllabus
2
Problem 2: Connect Four | | | | | | | |X|O| | | | |X| |X|O| | | |X|O|O|O|X| | | --------------- 0 1 2 3 4 5 6 An object of the Board class... underlying data: a 2d array of char s the user chooses the number of rows and columns (between 4 and 15 inclusive) each player is a char -- either ‘X’ or ‘O’
3
Problem 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
4
The Board class | | | | | | | |X|O| | | | |X| |X|O| | | |X|O|O|O|X| | | --------------- 0 1 2 3 4 5 6 Board b char[] char[][] data char char[]char int nRows int nCols void print() boolean isFull() boolean allowsMove(int c) void addMove(int c, char player) boolean winsFor(char player) b.print()
5
The 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 char[rows][cols]; for (int r=0 ; r<this.nRows ; ++r) for (int c=0 ; c<this.nCols ; ++c) this.data[r][c] = }
6
print class Board { public void print() { | | | | | | | |X|O| | | | |X| |X|O| | | |X|O|O|O|X| | | --------------- 0 1 2 3 4 5 6 this.data is the array of pieces or spaces other code here… this.nRows is the # rows this.nCols is the # cols
7
allowsMove class Board { // previous Board code … public boolean allowsMove(int col) { | | | | |X| | | | | | |X|O| | | | | | |O|X| | | | | | |X|O| | | | |X| |X|O| | | |X|O|O|O|X| | | --------------- 0 1 2 3 4 5 6
8
addMove class Board { // previous Board code … public void addMove(int col, char player) { | | | | | | | |X|O| | | | |X| |X|O| | | |X|O|O|O|X| | | --------------- 0 1 2 3 4 5 6
9
winsFor class Board { // previous Board code … public boolean winsFor(char player) { | | | | | | |O|X|O| | | |X|X|X|X|O| | | |X|O|O|O|X|O| | --------------- 0 1 2 3 4 5 6
10
Problem 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’
11
Problem 1 Similar to the last two problem 1’s, but using Date objects: (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 main class CS5App sets things up and runs a large while loop class Date Date printMenu prints print yesterday tomorrow isBefore diff dayOfWeek the “constructor” prints forward 1 day backward 1 day helper method # of days difference between 2 Date s returns a String Methods
12
if (choice==0) { // input mo, dy, yr again d = new Date(mo,day,yr) } Problem 1 (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 main sets things up and runs a large while loop // input integers: mo,dy,yr Date d = new Date(mo,dy,yr); while (choice != 9) { d.print(); printMenu(); printMenu prints Methods Date Calculator if (choice==2) { d.tomorrow() } …
13
The 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 other method(s) constructor initializes all of the data members used whenever a new object is created
14
tomorrow() 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 day int year int month data members
15
Comparing Date s 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.”); }
16
diff class Date { // previous Date code … public int diff(Date d2) { } What two dates are being compared here?
17
dayOfWeek class Date { // previous Date code … public String dayOfWeek() { }
18
Picture of a Date object Date int month int day int year Date d void print() void tomorrow()
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.