Computer Science 210
Data Structures
Fall 2018, 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.
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:
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.
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.
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).
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.
Your next task will be to design and implement a non-destructive addition method for ratios.
(a)/(b) + (c)/(d) = ( )/( )
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
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.
Don't forget to commit and push all of your code for this lab to your group's GitHub repository.