Computer Science 252
Problem Solving with Java
Spring 2015, The College of Saint Rose
Lecture 23: Matrices
Date: Thursday, April 16, 2015
Agenda
- Announcements
- exams not graded yet, hope to get them back to you on Tuesday
- get Lab 9: Final Project designs in soon (no late penalties will apply before early next week, but the sooner you submit the sooner I can give you feedback)
- More two-dimensional array examples
- Matrices
- In-class Exercise 23 - (20 lecture assignment
points) due before the start of our next 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, that is, a method that modifies the
matrix on which it is called, named scale that multiplies
each entry in the matrix by a number (5 points)
- A non-destructive method, that is, one that does not change
the matrix on which it is called, named 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 "InClass23" before you leave class.
Lecture 23 Assignment
Due at the start of class, Tuesday, April 21.
Please submit answers to these questions
in Submission
Box under "LA23" or in hard copy 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.
Note: this is a somewhat longer lecture assignment than usual.
First, some review since so many seemed to have trouble with writing
methods on the exam.
- Write a method that takes an ArrayList of objects that
implement the Drawable2DInterface as its parameter (the
parameter will have type ArrayList<Drawable2DInterface>) and
returns the number of the objects in the array whose x-coordinates
are less than 200. (5 points)
- Write a method that takes an int and an ArrayList of
Integer as parameters and returns another ArrayList of
Integer that includes only those values from the original
ArrayList that are greater than the int parameter's
value. (5 points)
The popular Sudoku puzzle game (for example, see
here) 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 checkRowValidity 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 checkColValidity (no need
to write it). Using your checkRowValidity and
checkColValidity methods as building blocks, write a method
checkRowsColsValidity that checks that all rows and columns are
valid in the current board. (6 points)
Terminology
Examples