CS 201 Advanced Object-Oriented Programming Lab 6 - Sudoku, Part 2 Due: March 10/11, 11:30 PM

Similar documents
CS 201 Advanced Object-Oriented Programming Lab 5 - Sudoku, Part 1 Due: March 3/4, 11:30 PM

Lab 4: Super Sudoku Solver CSCI 2101 Fall 2017

CS 201 Advanced Object-Oriented Programming Lab 3, Asteroids Part 1 Due: February 17/18, 11:30 PM

CMPSCI 187 / Spring 2015 Hangman

To gain experience using GUI components and listeners.

CS 134 Test Program #2

Assignment 1. Application Development

Prototyping a Swing Interface with the Netbeans IDE GUI Editor

CS 134 Programming Exercise 2:

CMPSCI 187 / Spring 2015 Hanoi

CS 134 Programming Exercise 9:

CS Exam 1 Review Suggestions

Homework Assignment: Sudoku Board

CS180 Spring 2010 Exam 2 Solutions, 29 March, 2010 Prof. Chris Clifton

CS 201 Advanced Object-Oriented Programming Lab 4 - Asteroids, Part 2 Due: February 24/25, 11:30 PM

In this lab we will practice creating, throwing and handling exceptions.

EXAM Computer Science 1 Part 1

CS 051 Homework Laboratory #2

Our second exam is Monday, April 3. Note that it will not be possible to get all the homework submissions graded before the exam.

Instructions for Crossword Assignment CS130

} Each object in a Java program has an identifier (name) } This includes:

CS Fall Homework 5 p. 1. CS Homework 5

Assignment 2. Application Development

Event Driven Programming

Be sure check the official clarification thread for corrections or updates to this document or to the distributed code.

CS 61B Data Structures and Programming Methodology. July 7, 2008 David Sun

CSE 143: Computer Programming II Winter 2019 HW6: AnagramSolver (due Thursday, Feb 28, :30pm)

Critical Thinking Assignment #6: Java Program #6 of 6 (70 Points)

CS 315 Software Design Homework 3 Preconditions, Postconditions, Invariants Due: Sept. 29, 11:30 PM

Premium POS Pizza Order Entry Module. Introduction and Tutorial

Programming Assignment #1 (Java)

Programming assignment A

Homework 7: Sudoku. Preliminaries. Overview

CSE 143: Computer Programming II Summer 2017 HW5: Anagrams (due Thursday, August 3, :30pm)

Week 5 Creating a Calendar. About Tables. Making a Calendar From a Table Template. Week 5 Word 2010

CS 177 Recitation. Week 8 Methods

CS 2110 Fall Instructions. 1 Installing the code. Homework 4 Paint Program. 0.1 Grading, Partners, Academic Integrity, Help

Page 1 of 7. public class EmployeeAryAppletEx extends JApplet

Software Testing Prof. Meenakshi D Souza Department of Computer Science and Engineering International Institute of Information Technology, Bangalore

CS 209 Sec. 52 Spring, 2006 Lab 6 - B: Inheritance Instructor: J.G. Neal

Remaining Enhanced Labs

Assignment #4 Minesweeper

CS 315 Software Design Homework 1 First Sip of Java Due: Sept. 10, 11:30 PM

This homework has an opportunity for substantial extra credit, which is described at the end of this document.

You Can Make a Difference! Due April 11/12 (Implementation plans due in class on 4/9)

Exceptions in Java

Using MS Publisher. Launch MS Publisher: Start > All Programs > Microsoft Office > Publisher. Setting up Document Size and Orientation

Lab Exercise 6: Abstract Classes and Interfaces CS 2334

The Fundamentals. Document Basics

Eclipse Setup. Opening Eclipse. Setting Up Eclipse for CS15

CSSE 220 Day 19. Object-Oriented Design Files & Exceptions. Check out FilesAndExceptions from SVN

Introduction. Create a New Project. Create the Main Form. Assignment 1 Lights Out! in C# GUI Programming 10 points

Lab 12: Lijnenspel revisited

CS161: Introduction to Computer Science Homework Assignment 10 Due: Monday 11/28 by 11:59pm

Programming Project 1

CSSE 220 Day 19. Object-Oriented Design Files & Exceptions. Check out FilesAndExceptions from SVN

Programming for Mobile Computing

Review: Using Imported Code. What About the DrawingGizmo? Review: Classes and Object Instances. DrawingGizmo pencil; pencil = new DrawingGizmo();

CSE 143: Computer Programming II Spring 2015 HW7: 20 Questions (due Thursday, May 28, :30pm)

Lab 3 Due March 18 at midnight

CMPSCI 187 / Spring 2015 Sorting Kata

Initial Coding Guidelines

CS 170 Java Programming 1. Week 13: Classes, Testing, Debugging

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

CS 2110 Summer 2011: Assignment 2 Boggle

CMSC 201 Spring 2019 Lab 06 Lists

You must define a class that represents songs. Your class will implement the Song interface in $master/proj2/cs61b/song.java.

Project 3: Implementing a List Map

Here are the steps to get the files for this project after logging in on acad/bill.

CS1110 Spring 2010 Assignment A6. Images. Due Sat. Apr 17, 11:59pm on the CMS 1

Topic 9: Swing. Swing is a BIG library Goal: cover basics give you concepts & tools for learning more

Topic 9: Swing. Why are we studying Swing? GUIs Up to now: line-by-line programs: computer displays text user types text. Outline. 1. Useful & fun!

15100 Fall 2005 Final Project

Due Friday, March 20 at 11:59 p.m. Write and submit one Java program, Sequence.java, as described on the next page.

Tips from the experts: How to waste a lot of time on this assignment

Excel Basics Fall 2016

CS61BL: Data Structures & Programming Methodology Summer Project 1: Dots!

16-Dec-10. Consider the following method:

Project 1 Computer Science 2334 Spring 2016 This project is individual work. Each student must complete this assignment independently.

To complete this database, you will need the following file:

Soda Machine Laboratory

1) Merge the cells that contain the title and center the title

PART 7. Getting Started with Excel

Spring CS Homework 3 p. 1. CS Homework 3

Chapter 3 Syntax, Errors, and Debugging. Fundamentals of Java

Computer and Programming: Lab 1

CS 215 Software Design Homework 3 Due: February 28, 11:30 PM

CS108, Stanford Handout #3. HW1 CodeCamp

Our second exam is Thursday, November 10. Note that it will not be possible to get all the homework submissions graded before the exam.

4. Use a loop to print the first 25 Fibonacci numbers. Do you need to store these values in a data structure such as an array or list?

Word 1 Module 2. Word 1. Module 2

CPSC 217 Assignment 3

Welcome to Technology Class. 7 th Grade: Web Design 8 th Grade: Digital Animation

Title: Jan 29 11:03 AM (1 of 23) Note that I have now added color and some alignment to the middle and to the right on this example.

Note: This is a miniassignment and the grading is automated. If you do not submit it correctly, you will receive at most half credit.

Java Programming Lecture 6

Programming Exercise

CMSC 150 Lab 8, Part II: Little PhotoShop of Horrors, Part Deux 10 Nov 2015

CSC 160 LAB 8-1 DIGITAL PICTURE FRAME. 1. Introduction

ACORN.COM CS 1110 SPRING 2012: ASSIGNMENT A1

Transcription:

CS 201 Advanced Object-Oriented Programming Lab 6 - Sudoku, Part 2 Due: March 10/11, 11:30 PM Introduction to the Assignment In this lab, you will finish the program to allow a user to solve Sudoku puzzles. In this lab, you will gain experience reading from files and working with exception handling. Here are some snapshots from my implementation of Sudoku: This is the starting screen. The grid is drawn and the partial solution is displayed in blue. The user must fill in the empty squares. The user cannot change the values in the squares provided by the program. Notice that the Check and Reveal solution buttons are enabled while Next game is disabled. Here the user has entered some values in squares. Notice that the user s entries are in black. 1

If the user clicks the Check button and the puzzle is incomplete or not solved correctly, this message pops up. If the user clicks the Check button and the puzzle is solved correctly, this message pops up. After hitting ok, the Check and Reveal solution buttons should be disabled and Next game should be enabled. If the user clicks the Reveal solution button all blank and incorrect squares are filled in, using a blue font. Next game is enabled and the other buttons are disabled. 2

When the user clicks Next game the starting grid for the next puzzle is displayed. When the user clicks Next game if all known puzzles have been solved, this message pops up. After hitting ok, all the buttons are disabled. The Program Design This project will have 4 classes. This week, you will add two more classes: SudokuSolution - this class holds the correct solution. It will be responsible for reading the solution from the file and helping to determine if a user s answer is correct. SolutionBadException - this is an exception class that you will create to help deal with problems that arise if the solution file has the wrong format. Writing the Program Step 0: Create new project You should create a copy of your Sudoku project so that you can leave your first week s solution intact. You should download the solution files from the website and import them into your Eclipse project. These should not go inside the src folder. These are the files named solution1.txt, solution2.txt, etc. 3

Step 1: Reading a solution from a file and displaying it To determine if the user has solved the puzzle, you will be reading the solution from a file along with the location of where to display numbers when the game starts. The contents of the file is formatted as follows: Each row of the solution is displayed on a single line, with each number separated by a single space. After all 9 rows are displayed, there is a line containing a single @. Following that are a number of rows containing a row number and column number identifying which values should be displayed to the user when the game starts. For example, on the left is the contents of a file and on the right is a picture of the starting screen. 1 8 6 5 2 3 9 4 7 2 9 7 4 1 8 3 6 5 4 5 3 6 7 9 8 2 1 9 7 1 8 6 5 4 3 2 8 4 5 3 9 2 1 7 6 3 6 2 7 4 1 5 8 9 5 2 4 1 3 6 7 9 8 7 1 9 2 8 4 6 5 3 6 3 8 9 5 7 2 1 4 @ 0 1 0 2 0 5 1 2 1 3 1 4 1 6 1 7 1 8 2 3 2 5 2 6 2 7 2 8 3 1 3 2 3 3 3 4 3 6 3 7 4 1 4 7 5 0 5 3 5 4 4

5 5 5 7 5 8 6 0 6 4 6 8 7 0 7 1 7 5 7 8 8 0 8 3 8 5 Solutions are stored in files named solution1.txt, solution2.txt, etc. You can look at these by double-clicking on them in Eclipse. You should create a class called SudokuSolution. The constructor for this class should be passed the name of a file containing a solution. It should read the first 9 rows from the file and save it in a 2-dimensional 9 x 9 array. (For now, you can ignore the remainder of the solution files.) In writing the code to read the solution from the file, you will see a compiler error message like this: Unhandled exception type FileNotFoundException. This is a warning to you that you need to figure out what to do if the file you try to open does not exist. You will need to put the call to the SudokuSolution constructor into a try block. In the corresponding catch block, you should handle the FileNotFoundException by displaying an error message to the user in a pop-up dialog box. To display a message in a pop-up window, say: JOptionPane.showMessageDialog (panel, Your message goes here. ); Instead of panel, you should send in the name of some Swing component, generally a JPanel or JFrame is used, to tell Java where on the screen the dialog box should appear. It will be placed on top of the component you specify. You can also pass in null for this parameter, in which case the dialog box will be in the middle of the screen. You should also disable all 3 buttons so that the user can t try to play the game if the file is not found. SudokuSolution should also define a method with this signature: public int getcorrectvalue (int row, int col) This method should be passed a value between 0 and 8 for each of the row and column and return the value in the solution at that location. Now add an action listener to the Reveal solution button. This action listener should walk the entire 9 x 9 grid, get the correct value from the solution and use the setvalue methods from last week to display the solution on the grid. Run your program and make sure that the solution displayed to the user is the same as what the file contains. The values that get set with this method are the hints that your program will provide to the user. You should therefore pick a font that makes them look different from the ones the user enters. You will also want to prevent the user from changing those values. To do that, call the seteditable method defined for JTextField. (Look at the online Java API for details.) Call the setforeground method to change the color of the font used to display the hints. 5

Step 2: Displaying hints to the user Next, you should read the pairs of values that correspond to the hints that should be displayed. Modify the SudokuSolution constructor to also read these values from the file. Note that in this case, you do not know how many lines to read from the file. You need to just read until there are no more ints left in the file. When you read in a row, column pair, you should call the setvalue method defined in Sudoku so that the hint is displayed to the user. To do this, you will need to pass the Sudoku object to the SudokuSolution constructor so that it can make that call. Step 3: Making the file reading robust One problem with reading files is that various errors might exist in the file. In step 3, you dealt with the problem of the file not existing. Now, consider various ways in which the contents of the file might be different from what you expect. For example, The file might have more or fewer than 81 values for the solution. A number in the solution might be something other than 0 through 8. It might even be a letter. Similarly, the hints might not be what you expect. Think about all the different ways that the file might be formatted incorrectly and modify the code that reads from the file to identify these errors. Create a new class SolutionBadException as a subclass of Exception. If you find any errors in the file contents, construct an instance of this new exception class and throw it. The Sudoku class should handle this exception by again displaying an error message that is suitable for the user and disabling the buttons. Test this by creating more solution files but introducing errors into them. Step 4: Checking the user s answers Add an action listener to the Check button that walks through the grid and checks the user s answer. If every cell is correct, display a You won pop-up message. If there are any mistakes, display some other message. Extra Credit (Simple): Next game I am providing multiple solution files., all named in sequence solution1.txt, solution2.txt, etc. When the user clicks the next game button, you should clear the grid and start over with the next solution file. You should also update your code to enable and disable buttons as follows: At the start and when a new game is loaded, the next game button should be disabled. If the user solves a puzzle or reveals the solution, next game should be enabled and the other 2 disabled. When the file is not found, all the buttons should be disabled and you should display a message in a pop-up window telling the user that the user has solved all of the puzzles. 6

Grading 15 Step 1: Revealing solution 15 Step 2: Displaying hints 15 Step 3: Robust file reading 15 Step 4: Checking the user s solution 15 Comments (including javadoc comments) 5 Assert statements 20 Style 5 Extra credit: next game 7