Computer Science 252
Problem Solving with Java
Fall 2013, The College of Saint Rose
Lecture 20: Two-Dimensional Arrays
Date: Tuesday, December 3, 2013
Agenda
- Announcements
- Final exam in one week: 10:45 AM in our regular classroom.
- same ground rules as first two exams: closed book, closed
notes, closed neighbors
- practice questions out
- we can review on Thursday
- Lecture 19 assignment recap
- Completing the "SalesGraph" example
- Two-dimensional arrays
- In-class Exercise 20 - (20 lecture assignment
points) due before the end of class.
Your task is to add some functionality to the Matrix2D
example and write code to test it in the main method there. The
functionality you are to add:
- A method max that returns the largest number in the
matrix (5 points)
- A destructive method scale that multiplies each entry in
the matrix by a number (5 points)
- A non-destructive method multiply that works much like
add but computes the matrix-matrix multiplication result. If
you don't remember how to multiply matrices, see the "Matrix
Product" section of the Wikipedia entry to Matrix
multiplication.
(10 points)
Please submit only the file
Matrix2D.java in
Submission Box
under "InClass20" before you leave class.
Lecture 20 Assignment
Due at the start of class, Thursday, December 5.
Please submit answers to these questions
in Submission
Box under "LA20" by the start of our next
class. We will discuss these questions at the start of class, so no
late submissions are accepted. Please be sure that your name is
clearly indicated in all submissions.
The popular Sudoku puzzle game consists of a board that has 9 rows and
9 columns of numbers ranging from 1-9. Suppose we are writing a class
to store a Sudoku board. We will assume that filled-in entries
contain a number 1 through 9, and blank entries are represented by 0.
- Write a declaration of the instance variable board that
would store the board using a two-dimensional array of int.
Show also the construction of the array. (3 points)
- Write a method checkRow that takes a row number (let's use
rows numbered 0-8 for simplicity) and checks that no two filled-in
entries contain the same number within that row (this would be
illegal in the game). It should return true if the row is
legal, false if not. (6 points)
- Assume you have a similar method checkCol (no need to write
it). Using your checkRow and checkCol methods as building
blocks, write a method checkRowsCols that checks that all rows
and columns are valid in the current board. (6 points)
Examples