Computer Science 210
Data Structures

Fall 2017, Siena College

Lab 2: Working with Classes
Due: start of lab, Wednesday, September 27, 2017

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.

Practice Program: Your task is to add the functionality listed below to the Ratio class. For each method you write, you will also need to write at least three test cases in the main method in the Ratios class. (30 points, 5 of which are for the test cases)

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.

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);

  }

Question 1: Draw a memory diagram for a program where the main method above is about to return from the call to scaleNonDestr. If you draw the diagram by hand (which is perfectly fine) submit a scan or legible photo of your diagram as part of your submission for this lab. (15 points)

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.

Practice Program: Your task is to add the functionality listed below to the Matrix2D example and write code to test it in the main method there. (25 points, 5 of which are for test cases))

The functionality you are to add:

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:

> FeatureValueScore
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