Computer Science 210
Data Structures

Fall 2018, Siena College

Lab 1: Array Practice
Due: 4:00 PM, Wednesday, September 19, 2018

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 learn more about collaboration with the git version control system and the GitHub hosting service.
  2. To review and strengthen understanding of Java arrays.
  3. To review and strengthen skills writing methods that take arrays as a parameter.
  4. To practice writing thorough test cases.

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.

Question 1: Demonstrate the results of your group's work on Collaborate.java, both in the file and in the commit history on your group's GitHub repository. (8 points)

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

Question 2: In the Java program below and on the next page, label an example of each of these constructs: (i) a method call, (ii) a return type, (iii) a local variable declaration, (iv) a formal parameter, (v) a class name, (vi) a constructor definition, (vii) a method header, (viii) an object construction, (ix) an actual parameter, (x) an instance variable/field declaration. (10 points, 1 each)

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.

Question 3: Write a method sum that takes an array of double values as its parameter and returns the sum of all elements in the array. Include thorough test cases in main. (7 points)

Question 4: Write a method sum that takes an array of int values as its parameter and returns the sum of all elements in the array. Include thorough test cases in main. (7 points)

Question 5: Write a method largest that takes an array of int values as its parameter and returns the value of the largest element in the array. Include thorough test cases in main. (7 points)

Question 6: Write a method countLarger that takes an array of int values and an additional int as its parameters, and returns the number of entries in the array that are at least as large as the additional int. Include thorough test cases in main. (10 points)

Question 7: Write a method countTrues that takes an array of boolean values as its parameter and returns the number of entries that contain true. Include thorough test cases in main. (7 points)

Question 8: Write a method stringLengths that takes an array of String values as its parameter and returns an array of int that contains the lengths of those Strings. That is, each element in the returned array contains the length of the String in the corresponding element of the parameter array. Include thorough test cases in main. (10 points)

Question 9: You have two methods named sum. How does Java know which one to execute when you call sum in your main method? (3 points)

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.

Question 10: Briefly describe, in English, precisely how your "add" command will work. (2 points)

Question 11: Complete the "add" command and demonstrate its correctness to your instructor. (6 points)

Question 12: Briefly describe, in English, precisely how your "insert" command will work. (2 points)

Question 13: Complete the "insert" command and demonstrate its correctness to your instructor. (8 points)

Question 14: Briefly describe, in English, precisely how your "remove" command will work. (2 points)

Question 15: Complete the "remove" command and demonstrate its correctness to your instructor. (6 points)

Question 16: Briefly describe, in English, precisely how your "clear" command will work. (2 points)

Question 17: Complete the "clear" command and demonstrate its correctness to your instructor. (3 points)