Lab 04
Matrix Subtraction
Objective:
Write a program that creates two matrices of whole numbers
and then subtracts them!
Lab Solution
Requirements:
- Functionality. (80pts)
- No Syntax, Major
Run-Time, or Major Logic Errors. (80pts*)
- *Code that cannot be
compiled due to syntax errors is nonfunctional code and will receive no
points for this entire section.
- *Code that cannot be
executed or tested due to major run-time or logic errors is
nonfunctional code and will receive no points for this entire section.
- Only use Arrays to
represent the Matrices. (80pts*)
- *Any other data
structure such as LinkedLists, ArrayLists, etc. will result in no points awarded
for this entire section.
- Creating the Matrices.
(20pts)
- We assume that our
matrices only have only two dimensions (a 2D Array).
- Users must be able to
specify the sizes of each Matrix – both length and width of both.
- All the above must
apply for full credit.
- Populating the
Matrices. (20pts)
- Users must be prompted
to enter the individual values of each matrix (called components).
- They must be prompted
value’s position (the indices) is being entered.
- All the above must
apply for full credit.
- Subtracting the
Matrices. (20pts)
- The program must check
to see if the length and width of one matrix matches the length and
width of the other matrix. If they do not match, then the user must be
prompted with an error message and the program must exit.
- The program must
create a resulting matrix that is the same length and width.
- The program must
subtract the values component wise and store the results in the
resulting matrix.
- Example. If we assume
we have two matrices, A and B, and a resulting matrix C, then C[0][0] = A[0][0] - B[0][0], C[0][1] = A[0][1] -
B[0][1], etc.
- All the above must
apply for full credit.
- Printing the Results.
(20pts)
- The contents of both
matrices entered by the user and the contents of the resulting matrix
must be shown to the user.
- Components of each
should be shown in their appropriate rows and columns.
- All the above must
apply for full credit.
- Coding Style. (10pts)
- Readable Code
- Meaningful identifiers
for data and methods.
- Proper indentation
that clearly identifies statements within the body of a class, a method,
a branching statement, a loop statement, etc.
- All the above must
apply for full credit.
- Comments. (10pts)
- Your name in the file.
(5pts)
- At least 5 meaningful
comments in addition to your name. These must describe the function of
the code it is near. (5pts)
Example Dialog:
Welcome to the matrix subtractor program!
Please enter the length and width of the first matrix
2
2
Enter value at index 0 0
4
Enter value at index 0 1
1
Enter value at index 1 0
8
Enter value at index 1 1
7
Please enter the length and width of the second matrix
2
2
Enter value at index 0 0
1
Enter value at index 0 1
3
Enter value at index 1 0
2
Enter value at index 1 1
4
4 1
8 7
-
1 3
2 4
=
3 -2
6 3
Solution Tests:
- Is your name written as
a comment in all source files?
- Does the solution
compile (no syntax errors)?
- If the user creates the
following matrices
Does the program output
4 1
8 7
-
1 3
2 4
=
3 -2
6 3
- If the user creates the
following matrices
Does the program output
1 2
3 4
-
5 6
7 8
=
-4 -4
-4 -4
- If the user creates the
following matrices
Does the program output
Invalid Dimensions. These cannot be
subtracted.
Lab Report
- Create a section named
“Problem” and describe this lab’s problem in your own words. (10pts).
- Create a section named
“Solution Description” and describe how the code solves the problem in
your own words. (10pts).
- Create a section named
“Problems Encountered” and describe the various syntax, run-time, and
logic errors that were encountered while implementing the solution.
(10pts).
- Draw a flow chart of the
solution and include that in the proposed solution section
- Is it possible to have
“ragged” / “jagged” arrays in Java?
- Given the following code
snippet, are there any kind of errors? If so, name the types, where, and
how this code can be fixed. Otherwise, what does this code exactly print
to the console?
- Given the following code
snippet, are there any kind of errors? If so, name the types, where, and
how this code can be fixed. Otherwise, what does this code exactly print
to the console?
- Given the following code
snippet, are there any kind of errors? If so, name the types, where, and
how this code can be fixed. Otherwise, what does this code exactly print
to the console?
- Given the following code
snippet, are there any kind of errors? If so, name the types, where, and
how this code can be fixed. Otherwise, what does this code exactly print
to the console?
- Given the following code
snippet, are there any kind of errors? If so, name the types, where, and
how this code can be fixed. Otherwise, what does this code exactly print
to the console?
Finally:
Upload the source code (.JAVA File Extension) and written
lab report (.DOC, .DOCX, or .PDF file extension) to the CSCE Dropbox.