Computer Science 210
Data Structures
Fall 2018, Siena College
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
Commit and push all code you have written 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 (arraypractice-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 command
touch package.bluej
which will create the little BlueJ icon for you to click on to launch BlueJ.
Collaborating with git and GitHub
Once your repository has been set up to grant access to all group members, complete this section to learn how git and GitHub can help with group work.
Each group member should clone a copy of the repository and make changes to the file Collaborate.java. Be careful to modify in different parts. Each group member should modify one of the lines in the class comment with his or her name (but each should choose a different one). And each group member should add an additional printout (but each should be in a different place) in the main method.
When done, each can commit to his/her own clone of the repository. One group member can then push to the origin on GitHub (which should be successful). If a second member then tries to push, an error message will be printed, stating that the clone is not up to date. A pull is needed, which will then merge the changes made by the group member who already pushed to the origin with the changes made by the group member trying to pull. So do that git pull, and as long as changes are made in different parts of the file, git will be able to do this automatically. If you have a third group member, repeat for the third member's changes. Now, the origin has everyone's changes. Group members can each then perform a pull to update their own clone to have everyone's changes.
This process will be very helpful when working in groups on labs and projects in this course, and in all of your programming careers in school and beyond. No emailing files back and forth or trying to figure out who changed what since the last time files were exchanged.
Terminology
Note: since this is a stripped-down version of the example that has many more comments, see if you can do this without looking at those comments.
/* * This class encapsulates a numerator and denominator and * includes the capability to set the numerator or denominator, * retrieve the numerator or denominator, retrieve the decimal * equivalent of the ratio, and return a "pretty" String * representation of the ratio */ public class Ratio { private int numerator; private int denominator; // let's make a Ratio public Ratio(int num, int den) { numerator = num; denominator = den; } // we can set things public void setNumerator(int num) { numerator = num; } public void setDenominator(int den) { denominator = den; } public int getNumerator() { return numerator; } public int getDenominator() { return denominator; } // we can compute stuff too public double getDecimalValue() { return 1.0 * numerator / denominator; } // printable representation public String toString() { return numerator + "/" + denominator; } public static void main(String args[]) { Ratio fred = new Ratio(4, 5); Ratio mary = new Ratio(2, 7); fred.setNumerator(2); Ratio alice = mary; mary.setDenominator(9); System.out.println(alice); } }
Practicing with Arrays
For this section, you will be writing some methods that operate on arrays. You are responsible for writing the methods, and adding code to the main method to test them thoroughly. Thorough testing means including test cases for a variety of array sizes (including 0-length arrays, where it makes sense), and for different distributions of numbers within the array (think about all the same, ascending, descending, all unique, mix of positive and negative, etc.). Place all of these in the provided ArrayPractice class. Note that your methods should not print their answers. Instead, they should return their answers so they can be printed by your test cases in main.
Modifying Arrays
In many of our examples so far, we have constructed and populated an array and but not changed it. In situations where this is necessary, we will need to make the distinction between the length of the array and the number of elements currently stored within it, which the provided code calls its size.
When managing arrays such as these, keep in mind that any value can be a valid array entry. An array of int can contain zeros, and an array of objects can contain nulls.
The ArrayModifier class includes a main method that will allow you to implement some array modifications. In the while loop in the main method, fill in the missing functionality for the commands below.