Fall 2018, Siena College

Lab 2: Working with Classes
Due: 3:45 PM, Monday, September 24, 2018

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.

As you finish each step, please have your instructor initial the hard copy of this lab handout you were issued to indicate successful completion.

Names:

Learning goals:

1. To practice adding functionality and tests to custom classes.
2. To learn about the difference between destructive and non-destructive methods of a class.
3. To practice writing thorough test cases.

Submitting

As you complete each programming task, commit with a meaningful commit message and push to your group's GitHub repository for this lab.

Once all written items are initialed to indicate completion, turn in the hard copy of this lab handout you were issued.

Getting Set Up

You will receive an email with the link to follow to set up your GitHub repository for this Lab (classes-lab-yourgitname). 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.

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.

The first new functionality you will add to Ratio is the ability to scale the Ratio by a given integer factor. You need not be concerned about reducing the result to lowest terms.

Question 1: Briefly describe, in English, what you will need to do to the numerator and/or denominator of a ratio to scale it by an integer factor s. (3 points)

Question 2: Write down at least 5 test cases, each of which should give the initial value of a ratio, a scaling factor, and the correct result. Be sure to include tests for a variety of cases, both in the kinds of values in the initial ratios and the scaling factors. (5 points)

Your method will be a mutator, so it is a destructive method. That means that once your method has been called (assuming a scaling factor other then 1), the Ratio's old value is gone.

Question 3: Add the method scale to the Ratio class that takes an int parameter and multiplies the value of the ratio by that number. Also add code to the main method of the Ratios class to implement your test cases listed above. Demonstrate your working method and test cases. (10 points)

Your next task is to design and implement a new mutator method multBy for the Ratio class 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).

Question 4: Briefly describe, in English, what you will need to do to the numerator and/or denominator of a ratio to multiply it by another ratio r. (3 points)

Question 5: Write down at least 5 test cases, each of which should give the initial value of a ratio, the value of another ratio to multiply by, and the correct result. Be sure to include tests for a variety of cases, both in the kinds of values in the initial ratios and the ones to multiply by. (5 points)

Question 6: Add the method multBy to the Ratio class. Also add code to the main method of the Ratios class to implement your test cases listed above. Demonstrate your working method and test cases. (10 points)

Each of 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.

Next, you will write a couple of non-destructive methods for the Ratio class.

Question 7: Write a non-destructive method copy for the Ratio class that returns a copy of the Ratio. That is, it returns a new Ratio with the same value as the one whose copy method was called. Include two test cases in the main method of Ratios. Demonstrate your methods. (6 points)

Question 8: Complete the following formula for the addition of ratios. (3 points)

(a)/(b) + (c)/(d) = (            )/(            )

Question 9: Write down at least 5 test cases, each of which should give the initial value of a ratio, the value of another ratio to add to it, and the correct result that should be returned. Be sure to include tests for a variety of cases, both in the kinds of values in the initial ratios and the ones to add in. (5 points)

Question 10: Add the method addNonDestr to the Ratio class. Also add code to the main method of the Ratios class to implement your test cases listed above. Also print the original ratio and the ratio passed as a parameter after the method call in at least one of your tests to show that the method is in fact non-destructive. Demonstrate your working method and test cases. (12 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);

}
```

Question 11: On the back of a page of this packet or on a separate sheet of paper, draw a memory diagram for a program where the main method above is about to return from the call to scaleNonDestr. (18 points)

Matrices

If you need a refresher, 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.

You will be working more with this example on the next problem set. For now, you will write just one method and associated tests to familiarize you with the example.

Question 12: Describe, in English, how you would find the largest value in any element of a given matrix. (2 points)

Question 13: What parameters would a method to find the maximum value in a Matrix2D object need to take? (2 points)

Question 14: What is the correct return type for a method to find the maximum value in a Matrix2D object need to take? (2 points)

Question 15: Describe the kinds of test cases that should be included to perform a thorough test of a max method for a Matrix2D object. (4 points)

Question 16: Implement the method max in the Matrix2D class. Include appropriate test cases in the main method of Matrix2D. Demonstrate your method and test cases. (10 points)

Don't forget to commit and push all of your code for this lab to your group's GitHub repository.