This class simulates the Sudoku board. Its important methods are setcell, dorow, docolumn and doblock.

Similar documents
Introduction to Programming Using Java (98-388)

EXAM Computer Science 1 Part 1

COMP 202 Java in one week

Type Hierarchy. Comp-303 : Programming Techniques Lecture 9. Alexandre Denault Computer Science McGill University Winter 2004

Birkbeck (University of London) Software and Programming 1 In-class Test Mar 2018

1.00 Introduction to Computers and Engineering Problem Solving. Quiz 1 March 7, 2003

Introduction to Computer Science Unit 2. Notes

Arrays OBJECTIVES. In this chapter you will learn:

PROGRAMMING FUNDAMENTALS

Constraint Satisfaction Problems (CSPs)

APCS Semester #1 Final Exam Practice Problems

Computer Science E-119 Fall Problem Set 1. Due before lecture on Wednesday, September 26

Backtracking. See Section 7.7 p of Weiss

AP CS Unit 3: Control Structures Notes

Loops. CSE 114, Computer Science 1 Stony Brook University

egrapher Language Reference Manual

Chapter 4 Defining Classes I

Introduction to Computer Science Unit 2. Notes

Homework 7: Sudoku. Preliminaries. Overview

COMP 250: Java Programming I. Carlos G. Oliver, Jérôme Waldispühl January 17-18, 2018 Slides adapted from M. Blanchette

BoredGames Language Reference Manual A Language for Board Games. Brandon Kessler (bpk2107) and Kristen Wise (kew2132)

CMPSCI 187: Programming With Data Structures. Lecture #15: Thinking Recursively David Mix Barrington 10 October 2012

Functions. Lecture 6 COP 3014 Spring February 11, 2018

Sudoku-4: Logical Structure

Practice Midterm 1. Problem Points Score TOTAL 50

Course Outline. Introduction to java

AP CS Unit 4: Classes and Objects Programs

Assignment 4. Aggregate Objects, Command-Line Arguments, ArrayLists. COMP-202B, Winter 2011, All Sections. Due: Tuesday, March 22, 2011 (13:00)

Birkbeck (University of London) Software and Programming 1 In-class Test Mar Answer ALL Questions

Practice Midterm 1 Answer Key

CMPSCI 187: Programming With Data Structures. Lecture #16: Thinking About Recursion David Mix Barrington 12 October 2012

Object-Oriented Design Lecture 3 CSU 370 Fall 2007 (Pucella) Friday, Sep 14, 2007

Le L c e t c ur u e e 3 To T p o i p c i s c t o o b e b e co c v o e v r e ed e Control Statements

School of Computer Science CPS109 Course Notes 6 Alexander Ferworn Updated Fall 15. CPS109 Course Notes 6. Alexander Ferworn

Lecture 14 CSE11 Fall 2013 For loops, Do While, Break, Continue

Due: 9 February 2017 at 1159pm (2359, Pacific Standard Time)

CS313D: ADVANCED PROGRAMMING LANGUAGE

News and information! Review: Java Programs! Feedback after Lecture 2! Dead-lines for the first two lab assignment have been posted.!

AP COMPUTER SCIENCE A

CmpSci 187: Programming with Data Structures Spring 2015

Creating Java Programs with Greenfoot

Assertions, pre/postconditions

CS 101 Exam 2 Spring Id Name

University of Cape Town ~ Department of Computer Science. Computer Science 1015F ~ 2007

Nested Loops ***** ***** ***** ***** ***** We know we can print out one line of this square as follows: System.out.

CSC System Development with Java. Exception Handling. Department of Statistics and Computer Science. Budditha Hettige

Sequence structure. The computer executes java statements one after the other in the order in which they are written. Total = total +grade;

Lecture 3. Lecture

CS111: PROGRAMMING LANGUAGE II

Static Methods. Why use methods?

Review questions. Review questions, cont d. Class Definition. Methods. Class definition: methods. April 1,

CS 177 Recitation. Week 8 Methods

We cover recursion in 150. Why do it again in 151?

Unit 7. 'while' Loops

AP Computer Science Unit 1. Programs

About This Lecture. Repetition Control Structures - while. Outline. Repetition

We have written lots of code so far It has all been inside of the main() method What about a big program? The main() method is going to get really

Week 6 CS 302. Jim Williams, PhD

Java Methods. Lecture 8 COP 3252 Summer May 23, 2017

Chapter 7 Functions. Now consider a more advanced example:

CS180. Exam 1 Review

CSCI 135 Exam #1 Fundamentals of Computer Science I Fall 2013

Object-Oriented Design Lecture 11 CS 3500 Spring 2010 (Pucella) Tuesday, Feb 16, 2010

Exceptions and Design

CS108, Stanford Handout #3. HW1 CodeCamp

Flexible data exchange with a method Parameters and return value

Equality for Abstract Data Types

Question 1 [20 points]

Top-Down Program Development

School of Computer Science CPS109 Course Notes 5 Alexander Ferworn Updated Fall 15

AP Computer Science Chapter 10 Implementing and Using Classes Study Guide

Week 3. Function Definitions. Example: Function. Function Call, Return Statement. Functions & Arrays. Gaddis: Chapters 6 and 7.

CS 61B, Spring 1999 MT3 Professor M. Clancy

Discover how to get up and running with the Java Development Environment and with the Eclipse IDE to create Java programs.

COMP-202. Recursion. COMP Recursion, 2011 Jörg Kienzle and others

Pace University. Fundamental Concepts of CS121 1

CSE 331 Summer 2017 Final Exam. The exam is closed book and closed electronics. One page of notes is allowed.

CSCI 136 Data Structures & Advanced Programming. Fall 2018 Instructors Bill Lenhart & Bill Jannen

COMP-202 Unit 4: Programming With Iterations. CONTENTS: The while and for statements

6.005 Elements of Software Construction Fall 2008

3. Convert 2E from hexadecimal to decimal. 4. Convert from binary to hexadecimal

McGill University School of Computer Science COMP-202A Introduction to Computing 1

Java Class Design. Eugeny Berkunsky, Computer Science dept., National University of Shipbuilding

Java Programming. Atul Prakash

Programming Project: Game of Life

Midterm Examination (MTA)

1. Short circuit AND (&&) = if first condition is false then the second is never evaluated

3.Constructors and Destructors. Develop cpp program to implement constructor and destructor.

SPRING 13 CS 0007 FINAL EXAM V2 (Roberts) Your Name: A pt each. B pt each. C pt each. D or 2 pts each

Introduction to the Java Basics: Control Flow Statements

CS5000: Foundations of Programming. Mingon Kang, PhD Computer Science, Kennesaw State University

Collections, Maps and Generics

CS-201 Introduction to Programming with Java

3. You are writing code for a business application by using C#. You write the following statement to declare an array:

NATIONAL UNIVERSITY OF SINGAPORE

Assignment 2.4: Loops

Good Luck! CSC207, Fall 2012: Quiz 3 Duration 25 minutes Aids allowed: none. Student Number: Lecture Section: L0101. Instructor: Horton

Data dependent execution order data dependent control flow

CSCI 135 Exam #1 Fundamentals of Computer Science I Fall 2014

1.00 Introduction to Computers and Engineering Problem Solving Quiz 1 March 4, 2005

Transcription:

Contents: We studied a code for implementing Sudoku for the major portion of the lecture. In the last 10 minutes, we also looked at how we can call one constructor from another using this (), the use of break and continue statements. Class Sudoku This class simulates the Sudoku board. Its important methods are setcell, dorow, docolumn and doblock. Attributes BLKSIZE = 3 Block size - Each block is made up of 3 3 cells. SIZE The Sudoku board is a square of blocks (defined above). Thus, if BLKSIZE = 3 then, a Sudoku board is a square of 3 3 blocks, or 9 9 cells. board The Sudoku game board. Methods Empty constructor setcell (int i, int j, String val) This method sets the specified cell to the value in String val. if ((i<0) (i >= SIZE) (j < 0) (j > SIZE)) return; Do nothing if the indices are invalid. if ((val.length() == 0) (val.length() > 1)) { board[i][j] = new SudokuCell(); return; if ((val.charat(0) < 0 ) (val.charat(0) > ( 0 + Sudoku.SIZE))) { 1

board[i][j] = new SudokuCell(); return; val is of type String. Since it should contain a single digit signifying the value at the cell, it should be of length exactly equal to 1. This is checked by the first if condition. Now we know that the length of the string val is 1. But this should also be a digit. That is checked by the next if condition. So, now we know that val has a single digit at index 0. board[i][j] = new SudokuCell(val.charAt(0) - 0 ); The cell is set with the integer value of val. The constructor of SudokuCell takes an integer as input. If we had not performed the above checks, then this line (and hence the method) would not have worked as expected by the caller. Please note that 0 is not a valid input. But this line will be executed if val = "0" as well. However, this situation is taken care of in the constructor of SudokuCell. dorow(int i) If a value is frozen for any cell in the ith row, then this method removes this value from the contents[] array of all the cells in that row. If the contents[] array of atleast one cell is modified in this manner, then the method returns true. Else, it returns false. if ((i < 0) (i >= SIZE)) return change; change has been set to false. Thus, if an invalid row number is given, then the method returns false, saying that no changes have been made in any cell. for (j=0; j<size; j++) { t = board[i][j].getvalue(); j holds the column number. getvalue will return the value at location [i][j] if the value there has been frozen. Else it returns 0. if (t!= 0) { int k; for (k=0; k<size; k++) { if (k!= j) change = board[i][k].block(t); If the value at [i][j] has been frozen to t, then we proceed to block the value t from all the cells in that row. That is, the contents[] array of all the cells in that row (except the one in which the value was found to be frozen, j) will not contain t after this. 2

If the contents[] array of any cell has been modified in this manner, then the boolean indicator, change becomes true. docolumn(int i) The functionality of this method is the same as that of dorow. Now we check the cells in each row of the ith column. Hence, we have, t = board[j][i].getvalue(); and change = board[k][i].block(t);. That is, we read the frozen value in each cell in the ith column using board[j][i].getvalue(). Once we find that a value has been frozen, we check eack cell in the ith row using board[k][i].block(t). doblock(int xblk, int yblk) A block is defined to be a square of BLKSIZE BLKSIZE cells. And, BLKSIZE BLKSIZE such blocks make up a Sudoku board. While playing Sudoku, we also need to check that if a value is frozen in any cell of the block, then it does not appear in the contents[] array of any other cell in that block. The functionality of this method is the same as that of dorow and docloumn. That is, if a value is frozen then it must be ensured that it does not appear in any other cell in the block. If a cell s contents[] array has been modified, return true, else return false. However, this method is the trickiest of all other methods in the class. Reason: The cells are numbered according to the row number and column number in the whole of Sudoku board, and not according to their locations in the block. The argument for the method specifies which block we should be checking. There are BLKSIZE number of rows and BLKSIZE number of columns. e.g, if BLKSIZE = 3, then there are 3 rows and 3 columns of blocks. if ((xblk<0) (xblk>=blksize) (yblk<0) (yblk>=blksize)) return change; change is initialized to false. Hence, if invalid block number is passed as argument, then we return false. for (i=0; i<blksize; i++) for (j=0; j<blksize; j++) Check for each row and each column in the block. t = board[xblk*blksize + i][yblk*blksize + j].getvalue(); i holds the row number in the block and ranges from 0 to BLKSIZE 1. j holds the column number in the block and ranges from 0 to BLKSIZE 1. We 3

now need to calculate the absolute row and column numbers (in the Sudoku board). Please observe that the 0th row, 0th column cell in block [xblk, yblk] could be accessed in the Sudoku board using the index [xblk BLKSIZE, yblk BLKSIZE]. When the row index or column index in the block increases by 1, the index in the board also increases by exactly 1. Thus, the ith row, jth column cell in block [xblk, yblk] could be accessed in the Sudoku board using the index [xblk BLKSIZE + i, yblk BLKSIZE + j]. Method getvalue will return the value at specified location if the value there has been frozen. Else it returns 0. if (t!= 0) { int k, l; for (k=0; k<blksize; k++) for (l=0; l<blksize; l++) if ((k!=i) (l!=j)) change = board[xblk*blksize + k][yblk*blksize + l].block(t); If the value at [i][j] in the specified block has been frozen to t, then we proceed to block the value t from all the cells in that block. That is, the contents[] array of all the cells in that block (except the one in which the value was found to be frozen, [i, j]) will not contain t after this. If the contents[] array of any cell has been modified in this manner, then the boolean indicator, change becomes true. Since we need to check cells in various rows and columns when we are checking a block, we need two for loops, one for rows and one for columns. Here, k is used to specify the row in the block and l is used to specify the column in the block. public void Display(SudokuDisplay sd) This method is used to display the board. It calls a method in the class SudokuDisplay. We dont need to concern ourselves with the implementation of this class. public String tostring() This method is used to convert the Sudoku board to a string. Class test This class contains the main method. 4

Methods static void pause(scanner s) This method is called from main. It ensures that the user can see the output of each iteration of dorow, docolumn or doblock. After each call to one of these methods, the user has to press the return key. Only then will the main method proceed. main method The objects of various classes are initialized. When the object sd of class SudokuDisplay is created, the sudoku board panel gets displayed since that is the functionality of the constructor of class SudokuDisplay. The user enters the puzzle in this panel. pause(sc); is used, so that once the user has entered the puzzle, he presses the return key. This signifies to the program that it should start workingon the data. for (i=0; i<sudoku.size; i++) for (j=0; j<sudoku.size; j++) s.setcell(i, j, sd.getvalue(i, j)); s.display(sd); The values entered by the user are given to the respective cell. do { change = false; while (change); Continue performing the operations until the program is making progress, ie, atleast one change in the contents[] array of atleast one cell occurs in each loop. for (i=0; i<sudoku.size; i++) { pause(sc); change = s.dorow(i); s.display(sd); System.out.println( After Row + i + /n ); On each of the rows, dorow is called. ie, we check if any cell in that row has been frozen, and change the contents[] array of the other cells of that row. The same is done by considering each column at a time, and each block at a time. 5

Thus, in the main method, we keep modifying the contents[] array of cells in each row, each column and the each block until, no more modifications can be made based on the current data. End of program Calling one constructor from another Another constructor of the same class can be called using the keyword this. E.g. if (val > SIZE) this (); else... Here, the constructor without any arguments has been called. Statement to change the flow of execution: break There are statements which change the flow of execution. We have already seen the use of return statement to return from any method. Let us now look at another statement: break. You can use a break statement to terminate a for, while, or do-while loop, as shown in the following example: Scanner sc =... while (true){ int i = sc.nextint (); if (i < 0 i > 20) break; System.out.println (fact (i)); Assume that the method which returns the factorial, fact (i) is already written. Let us call this code 1. Now consider Code 2 as follows: boolean done = false; while!done { int i = sc.nextint (); if (i <0 i > 20) done = true; else System.out.println (fact (i)); The control follows a straight flow in code 2. This makes it easier to write test cases and debug. Hence, it is difficult to make errors here. In code 1, the break statement makes it a bit more difficult to understand the flow of control. Hence, it is more difficult to write test cases for code 2. However, in code 1, the compiler does not have to write any statements to check if the condition of while (true) statement is true. But, in code 2, 6

the condition while (!done) needs to be checked everytime. We can thus say that code 1 is more efficient, while code 2 is more userfriendly. 7