Computer Science 210
Data Structures
Fall 2018, Siena College
This week's lab is all about the ArrayList ADT.
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 (arraylists-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.
Improving Your git and GitHub Setup
Now that we are getting a bit more comfortable with the basics of git and GitHub, it's a good time to do a bit of extra configuration of your git environment and your GitHub account.
Default Editor
Many of you when working on your own computers have come across
situations where you are executing a command, often a git pull,
and git asks you for a log message by opening up a text editor.
By default, both in Git Bash for Windows and using git at the
terminal on Mac and other Unix-like systems, the editor it will use is
called "vim", which is a very powerful but old and cryptic editor.
While vim has many loyal devotees, no one can deny that it can be
confusing to the beginner. Usually what you'll do when git
launches vim on you is type ":q
" then hit Enter to exit the
editor.
Most likely, you will want to configure git to use a different editor in those situations. Windows users might want to use Notepad++ (install it if you want to use it and don't have it), which can be accomplished using the steps shown in this video. On Mac OS X and other Unix-like systems, I like to use Emacs in place of vim, but you might have your own preferred editor. For most purposes, though, the fairly simple "nano" editor is probably a good choice. This can be set up with the following command in your Terminal window:
git config --global core.editor "nano"
In either case, you can see if the configuration worked by making a
change to any repository then issuing a git commit command
without the "-m
" to specify a commit message. This causes
git to invoke its editor to obtain that message.
Improving your GitHub Profile
GitHub should be treated as a public display of your programming experience. As such, it should be configured to look professional. As part of this, you should make sure you have an appropriate profile picture and your real name entered (or at least first name plus last initial if you prefer to remain a bit more anonymous).
A double-specific ArrayList class
We talked in class about how great it is to write generic versions of classes (like GenericPair) rather than versions that work only with a specific datatype (like DoublePair). We have a fully generic version of the ArrayList class provided by the Java API, but we are going to examine how ArrayLists are implemented by writing our own class DoubleArrayList that can store only double values. The fundamentals are the same, but this will allow us to avoid a few technical complications that arise in a fully generic implementation.
Implementing the First Few Methods
In this section of the lab you will implement the first few methods in the class DoubleArrayList. Run the test method testDoubleArrayListA after implementing each method to catch errors early and see your points increase. See each method's documentation for details on what it does.
The following methods should be completed for this part of the lab:
(2 tests) public int size() (3 tests) public void set (int pos, double element) (7 tests) public double remove(int pos) (2 tests) public void clear()
Writing Test Code
It is a standard programming practice to create test code before you actually complete any method implementations. This was done for you for the previous step in this lab. Now you get the chance to do it for yourself.
Below is the method signature for the DoubleArrayList contains method.
public boolean contains (double element)
It returns true if the list contains the element, and false if it does not.
Create a new public static void method in TestDoubleArrayList called myTestCases. Modeling your method after the other test methods, write code that creates variables score and maxscore and a DoubleArrayList object. It should add various elements to the DoubleArrayList object using the addSimple method, then call the contains method and test the response for correctness. Use variables score and maxscore to count the correct responses and the total possible correct responses. Write 5 tests, so that the maximum score possible is 5. Make sure at least two of your tests check for elements that are not supposed to be in the list, and at least two check for elements that are supposed to be in the list. Also, be sure one of the tests checks for the very last element in the list (which is a case that sometimes causes problems.)
Then, add a dummy implementation of the contains method to DoubleArrayList. Do not implement it yet! This method should consist of the method signature and a body that just returns false. Compile and run your test program.
Do not continue past this point until you have demonstrated the above step!
Now implement the contains method.
More DoubleArrayList Methods
Now complete the implementations of the remaining methods, as listed below, in DoubleArrayList. Run the test method testDoubleArrayListB after implementing each method to catch errors early and see your points increase.
(1 test) public int getCapacity() (7 tests) public void ensureCapacity(int minCapacity) (4 tests) public void add(double element) (6 tests) public DoubleArrayList getCopy() (12 tests) public void add(int pos, double element) (9 tests) public void addAll(DoubleArrayList addend) (5 tests) public void addAll(int pos, DoubleArrayList addpos)
Don't forget to commit and push all of your code for this lab to your group's GitHub repository.