Computer Science 210

Data Structures

Fall 2017, Siena College

In this week's lab, you will practice working with custom classes.

You will be paired with a partner to complete this lab. Only one submission per group is needed.

Getting Set Up

You will receive an email with the
link to follow to set up your GitHub repository
`arraypractice-lab-yourgitname` for this Lab. One member of the
group should follow the link to set up the repository on GitHub,
then that person should email the instructor with the other group
members' GitHub usernames so they can be granted access. This will
allow all members of the group to clone the repository and commit
and push changes to the origin on GitHub. At least one group member
should make a clone of the repository to begin work.

In your git bash window, after you clone and cd to the repository, you can type the commands

touch Ratios/package.bluej touch Matrix2D/package.bluej

which will create the little BlueJ icons for you to click on to launch BlueJ.

Enhancements to `Ratio`

For the first part of this lab, you will be working with the Ratios example, a copy of which is in your repository. Your code for these will be graded as a practice program.

Much like we did for the Circles example, we will add functionality and tests to this class.

- Write a new mutator method
`scale`for the`Ratio`class that takes an`int`parameter and multiplies the value of the ratio by that number. Recall that you can multiply the value of a ratio by a value by multiplying its numerator by that number. Do not worry about reducing to lowest terms or any other simplifications. (5 points) - Write a new mutator method
`multBy`for the`Ratio`class in the Ratios example that takes a`Ratio`parameter and multiplies the value of this ratio (the one whose method you are executing) by that ratio (the one you received as a parameter). Recall that you can multiply a ratio by another by multiplying their numerators to find the new numerator, and their denominators to find the new denominator. (8 points)

The mutator methods you wrote for the tasks above multiply a
`Ratio` by an integer and to multiply a `Ratio` by another
`Ratio` were *destructive* methods. That is, calling them on
an instance of `Ratio` will modify (*i.e.*, destroy) the value of that
`Ratio`. The following code segment:

Ratio r = new Ratio(2, 7); r.scale(3);

would result in `r` having a value of 6/7. `r`'s original value
of 2/7 was destroyed by the call to `scale`.

Sometimes, we might want to compute something using a `Ratio` but
not change its original value. For example, the method below would be
a *non-destructive* version of `scale`:

public Ratio scaleNonDestr(int n) { Ratio newRat = new Ratio(numerator*n, denominator); return newRat; }

This one would be used like this:

Ratio r1 = new Ratio(2, 7); Ratio r2 = r1.scaleNonDestr(3);

Now, we still have `r1` with its original 2/7 value, and `r2`
will have the value 6/7.

- Write a
*non-destructive*method`copy`for the`Ratio`class that returns a copy of the`Ratio`. That it, is returns a new`Ratio`with the same value as the one whose`copy`method was called. (4 points) - Write a
*non-destructive*method`addNonDestr`for the`Ratio`class that takes a`Ratio`parameter and adds together the value of this ratio by that ratio, but*returns*a new`Ratio`that contains the sum. Neither this ratio (the one whose method you are executing) nor the other (the one you received as a parameter) should be modified. Recall that you can compute the sum of two fractions*a/b*and*c/d*by computing the numerator of the sum as*ad + bc*, and the denominator as*bd*. (8 points)

Practice with Memory Diagrams

Consider the `Ratio` class from the Ratios program,
with the above `scaleNonDestr` method added to it. Also consider
this `main` method:

public static void main(String args[]) { Ratio fred = new Ratio(4, 5); Ratio mary = new Ratio(2, 7); fred.setNumerator(2); Ratio alice = mary; Ratio joe = alice.scaleNonDestr(3); }

Matrices

Read the topic notes about two-dimensional arrays (linked from the schedule page) and study the Matrix2D example, which is also included in your repository. Don't worry about the details of the exception handling.

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)

Submitting

Your submission requires that all required deliverables are committed and pushed to the master for your repository's origin on GitHub. That's it! If you see everything you intend to submit when you visit your repository's page on GitHub, you're set.

Grading

This assignment is worth 70 points, which are distributed as follows:

> Feature | Value | Score |

Ratio scale method | 5 | |

Ratio multBy method | 8 | |

Ratio copy method | 4 | |

Ratio addNonDestr method | 8 | |

Ratios test cases | 5 | |

Lab question (memory diagram) | 15 | |

Matrix2D max method | 5 | |

Matrix2D scale method | 5 | |

Matrix2D multiply method | 10 | |

Matrix2D test cases | 5 | |

Total | 70 | |