Skip to content

Commit 97bd7dc

Browse files
committed
Merge pull request jmburges#1 from learn-co-curriculum/wip-master
Wip master
2 parents ae247ed + 8da79c5 commit 97bd7dc

File tree

11 files changed

+883
-4
lines changed

11 files changed

+883
-4
lines changed

.learn

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
tags:
2+
- List
3+
- ArrayList
4+
languages:
5+
- Java
6+
resources:
7+
- 0

CONTRIBUTING.md

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# Contributing to Learn.co Curriculum
2+
3+
We're really exited that you're about to contribute to the [open curriculum](https://learn.co/content-license) on [Learn.co](https://learn.co). If this is your first time contributing, please continue reading to learn how to make the most meaningful and useful impact possible.
4+
5+
## Raising an Issue to Encourage a Contribution
6+
7+
If you notice a problem with the curriculum that you believe needs improvement
8+
but you're unable to make the change yourself, you should raise a Github issue
9+
containing a clear description of the problem. Include relevant snippets of
10+
the content and/or screenshots if applicable. Curriculum owners regularly review
11+
issue lists and your issue will be prioritized and addressed as appropriate.
12+
13+
## Submitting a Pull Request to Suggest an Improvement
14+
15+
If you see an opportunity for improvement and can make the change yourself go
16+
ahead and use a typical git workflow to make it happen:
17+
18+
* Fork this curriculum repository
19+
* Make the change on your fork, with descriptive commits in the standard format
20+
* Open a Pull Request against this repo
21+
22+
A curriculum owner will review your change and approve or comment on it in due
23+
course.
24+
25+
# Why Contribute?
26+
27+
Curriculum on Learn is publicly and freely available under Learn's
28+
[Educational Content License](https://learn.co/content-license). By
29+
embracing an open-source contribution model, our goal is for the curriculum
30+
on Learn to become, in time, the best educational content the world has
31+
ever seen.
32+
33+
We need help from the community of Learners to maintain and improve the
34+
educational content. Everything from fixing typos, to correcting
35+
out-dated information, to improving exposition, to adding better examples,
36+
to fixing tests—all contributions to making the curriculum more effective are
37+
welcome.

LICENSE.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#Learn.co Educational Content License
2+
3+
Copyright (c) 2015 Flatiron School, Inc
4+
5+
The Flatiron School, Inc. owns this Educational Content. However, the Flatiron School supports the development and availability of educational materials in the public domain. Therefore, the Flatiron School grants Users of the Flatiron Educational Content set forth in this repository certain rights to reuse, build upon and share such Educational Content subject to the terms of the Educational Content License set forth [here](http://learn.co/content-license) (http://learn.co/content-license). You must read carefully the terms and conditions contained in the Educational Content License as such terms govern access to and use of the Educational Content.
6+
7+
Flatiron School is willing to allow you access to and use of the Educational Content only on the condition that you accept all of the terms and conditions contained in the Educational Content License set forth [here](http://learn.co/content-license) (http://learn.co/content-license). By accessing and/or using the Educational Content, you are agreeing to all of the terms and conditions contained in the Educational Content License. If you do not agree to any or all of the terms of the Educational Content License, you are prohibited from accessing, reviewing or using in any way the Educational Content.

README.md

Lines changed: 117 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,121 @@
11
# cs-implementing-an-arraylist-lab
22

3-
# learning goal
4-
Students can write a class that implements an interface.
53

6-
# notes
7-
Students can write a class that implements an interface.
4+
## Objectives
5+
6+
1. Write an implementation of an ArrayList.
7+
8+
9+
## Implementing an array-backed list
10+
11+
For this lesson we provide a partial implementation of an ArrayList that uses a Java array to store the elements. We left four of the methods incomplete; your job is to fill them in. We provide JUnit tests you can use to check your work.
12+
13+
14+
## Instructions
15+
16+
When you check out the repository for this lab, you should find a file structure similar to what you saw in the previous lab. The top level directory contains `CONTRIBUTING.md`, `LICENSE.md`, `README.md`, and the directory that contains the code for this lab, `javacs-lab02`.
17+
18+
In the subdirectory `javacs-lab02/src/com/flatironschool/javacs` you'll find the source files you need for this lab:
19+
20+
* `MyArrayList.java` contains a partial implementation of the `List` interface using a Java array to store the elements.
21+
22+
* `MyArrayListTest.java` contains JUnit tests for `MyArrayList`.
23+
24+
In `javacs-lab02`, you'll find the Ant build file `build.xml`. If you are in this directory, you should be able to run `ant MyArrayList` to run `MyArrayList.java`, which contains a few simple tests. Or you can run `ant MyArrayListTest` to run the JUnit test.
25+
26+
When you run the tests, several of them should fail. If you examine the source code, you'll find four `TODO` comments indicating which methods you will fill in.
27+
28+
## `MyArrayList` code
29+
30+
Before you start filling in the missing methods, let's walk through some of the code. Here are the instance variables and the constructor.
31+
32+
```java
33+
public class MyArrayList<E> implements List<E> {
34+
int size; // keeps track of the number of elements
35+
private E[] array; // stores the elements
36+
37+
public MyArrayList() {
38+
array = (E[]) new Object[10];
39+
size = 0;
40+
}
41+
}
42+
```
43+
44+
As the comments indicate, `size` keeps track of how many elements are in `MyArrayList`, and `array` is the array that actually contains the elements.
45+
46+
The constructor creates an array of 10 elements, which are initially `null`, and sets `size` to 0. Most of the time, the length of the array is bigger than `size`, so there are unused slots in the array.
47+
48+
One detail about Java: You can't instantiate an array of T[], so you have to instantiate an array of Object and then typecast it. You can [read more about this issue here](http://www.ibm.com/developerworks/java/library/j-jtp01255/index.html).
49+
50+
Next we'll look at the method that adds elements to the list. Here's my implementation of `add`:
51+
52+
```java
53+
public boolean add(E element) {
54+
if (size >= array.length) {
55+
// make a bigger array and copy over the elements
56+
E[] bigger = (E[]) new Object[array.length * 2];
57+
System.arraycopy(array, 0, bigger, 0, array.length);
58+
array = bigger;
59+
}
60+
array[size] = element;
61+
size++;
62+
return true;
63+
}
64+
```
65+
66+
If there are no unused spaces in the array, we have to create a bigger array and copy over the elements. Then we can store the element in the array and increment `size`.
67+
68+
It might not be obvious why this method returns a boolean, since it seems like it always returns `true`. As always, [you can find the answer in the documentation](https://docs.oracle.com/javase/7/docs/api/java/util/Collection.html#add(E)).
69+
70+
It's also not obvious how to analyze the performance of this method. In the normal case, it's constant time, but if we have to resize the array, it's linear. In the next README I'll explain how we can handle this.
71+
72+
We'll look at `get` next, and then you can fill in `set`. Actually, `get` is pretty simple:
73+
74+
```java
75+
public T get(int index) {
76+
if (index < 0 || index >= size) {
77+
throw new IndexOutOfBoundsException();
78+
}
79+
return array[index];
80+
}
81+
```
82+
83+
If the index is out of bounds, it throws an exception; otherwise it reads and returns an element of the array. Notice that it checks whether the index is less than `size`, not `array.length`, so it's not possible to access the unused elements of the array.
84+
85+
## Instructions
86+
87+
* In `MyArrayList.java`, you'll find a stub for `set` that looks like this:
88+
89+
```java
90+
public T set(int index, T element) {
91+
// TODO: fill in this method.
92+
return null;
93+
}
94+
```
95+
96+
Read [the documentation of set](https://docs.oracle.com/javase/7/docs/api/java/util/List.html#set(int,%20E)), then fill in the body of this method. If you run `MyArrayListTest` again, `testSet` should pass.
97+
98+
HINT: Try to avoid repeating the index-checking code.
99+
100+
101+
* Your next mission is to fill in `indexOf`. As usual, you should [read the documentation](https://docs.oracle.com/javase/7/docs/api/java/util/List.html#indexOf(java.lang.Object)) so you know what it's supposed to do. In particular, notice how it is supposed to handle `null`.
102+
103+
To make things a little easier, I've provided a helper method called `equals` that compares an element from the array to a target value and returns `true` if they are equal (and it handles `null` correctly).
104+
Notice that this method is private because it is used inside this class but it is not part of the `List` interface.
105+
106+
When you are done, run `MyArrayListTest` again; `testIndexOf` should pass now, as well as the other tests that depend on it.
107+
108+
* Only two more methods to go, and you'll be done with this lab. The next one is an overloaded version of `add` that takes an index and stores the new value at the given index, shifting the other elements to make room, if necessary.
109+
110+
Again, [read the documentation](https://docs.oracle.com/javase/7/docs/api/java/util/List.html#add(int,%20E)), write an implementation, and run the tests for confirmation.
111+
112+
HINT: Avoid repeating the code that makes the array bigger.
113+
114+
115+
* Last one: fill in the body of `remove`. [The documentation is here](https://docs.oracle.com/javase/7/docs/api/java/util/List.html#remove(int)). When you finish this one, all tests should pass.
116+
117+
118+
Once you have your implementation working, compare it to mine, which you can find by checking out the solutions branch of the repo, or [you can read it on GitHub](https://TODO: add_this_later).
119+
120+
8121

6.29 KB
Binary file not shown.
5.73 KB
Binary file not shown.

javacs-lab02/build.xml

Lines changed: 192 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,192 @@
1+
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
2+
<!-- WARNING: Eclipse auto-generated file.
3+
Any modifications will be overwritten.
4+
To include a user specific buildfile here, simply create one in the same
5+
directory with the processing instruction <?eclipse.ant.import?>
6+
as the first entry and export the buildfile again. --><project basedir="." default="build" name="JavaCS">
7+
<property environment="env"/>
8+
<property name="junit.output.dir" value="junit"/>
9+
<property name="debuglevel" value="source,lines,vars"/>
10+
<property name="target" value="1.6"/>
11+
<property name="source" value="1.6"/>
12+
<path id="JavaCS.classpath">
13+
<pathelement location="bin"/>
14+
<pathelement location="lib/jedis-2.8.0.jar"/>
15+
<pathelement location="lib/jsoup-1.8.3.jar"/>
16+
<pathelement location="lib/junit-4.12.jar"/>
17+
<pathelement location="lib/hamcrest-core-1.3.jar"/>
18+
<pathelement location="lib/jcommon-1.0.23.jar"/>
19+
<pathelement location="lib/jfreechart-1.0.19.jar"/>
20+
<pathelement location="lib/servlet.jar"/>
21+
<pathelement location="lib/ant-junit.jar"/>
22+
</path>
23+
<target name="init">
24+
<mkdir dir="bin"/>
25+
<copy includeemptydirs="false" todir="bin">
26+
<fileset dir="src">
27+
<exclude name="**/*.launch"/>
28+
<exclude name="**/*.java"/>
29+
</fileset>
30+
</copy>
31+
</target>
32+
<target name="clean">
33+
<delete dir="bin"/>
34+
</target>
35+
<target depends="clean" name="cleanall"/>
36+
<target depends="build-subprojects,build-project" name="build"/>
37+
<target name="build-subprojects"/>
38+
<target depends="init" name="build-project">
39+
<echo message="${ant.project.name}: ${ant.file}"/>
40+
<javac debug="true" debuglevel="${debuglevel}" destdir="bin" includeantruntime="false" source="${source}" target="${target}">
41+
<src path="src"/>
42+
<classpath refid="JavaCS.classpath"/>
43+
</javac>
44+
</target>
45+
<target description="Build all projects which reference this project. Useful to propagate changes." name="build-refprojects"/>
46+
<target description="copy Eclipse compiler jars to ant lib directory" name="init-eclipse-compiler">
47+
<copy todir="${ant.library.dir}">
48+
<fileset dir="${ECLIPSE_HOME}/plugins" includes="org.eclipse.jdt.core_*.jar"/>
49+
</copy>
50+
<unzip dest="${ant.library.dir}">
51+
<patternset includes="jdtCompilerAdapter.jar"/>
52+
<fileset dir="${ECLIPSE_HOME}/plugins" includes="org.eclipse.jdt.core_*.jar"/>
53+
</unzip>
54+
</target>
55+
<target description="compile project with Eclipse compiler" name="build-eclipse-compiler">
56+
<property name="build.compiler" value="org.eclipse.jdt.core.JDTCompilerAdapter"/>
57+
<antcall target="build"/>
58+
</target>
59+
<target name="CrawlerTest">
60+
<mkdir dir="${junit.output.dir}"/>
61+
<junit fork="yes" printsummary="withOutAndErr">
62+
<formatter type="xml"/>
63+
<test name="com.flatironschool.javacs.CrawlerTest" todir="${junit.output.dir}"/>
64+
<classpath refid="JavaCS.classpath"/>
65+
</junit>
66+
</target>
67+
<target name="Indexer (1)">
68+
<java classname="com.flatironschool.javacs.Indexer" failonerror="true" fork="yes">
69+
<classpath refid="JavaCS.classpath"/>
70+
</java>
71+
</target>
72+
<target name="ListLinks">
73+
<java classname="com.flatironschool.javacs.ListLinks" failonerror="true" fork="yes">
74+
<classpath refid="JavaCS.classpath"/>
75+
</java>
76+
</target>
77+
<target name="Crawler">
78+
<java classname="com.flatironschool.javacs.Crawler" failonerror="true" fork="yes">
79+
<classpath refid="JavaCS.classpath"/>
80+
</java>
81+
</target>
82+
<target name="ListClientExample">
83+
<java classname="com.flatironschool.javacs.ListClientExample" failonerror="true" fork="yes">
84+
<classpath refid="JavaCS.classpath"/>
85+
</java>
86+
</target>
87+
<target name="ListClientExampleTest">
88+
<mkdir dir="${junit.output.dir}"/>
89+
<junit fork="yes" printsummary="withOutAndErr">
90+
<formatter type="xml"/>
91+
<test name="com.flatironschool.javacs.ListClientExampleTest" todir="${junit.output.dir}"/>
92+
<classpath refid="JavaCS.classpath"/>
93+
</junit>
94+
</target>
95+
<target name="MyArrayList">
96+
<java classname="com.flatironschool.javacs.MyArrayList" failonerror="true" fork="yes">
97+
<classpath refid="JavaCS.classpath"/>
98+
</java>
99+
</target>
100+
<target name="test">
101+
<mkdir dir="${junit.output.dir}"/>
102+
<junit fork="yes" printsummary="withOutAndErr">
103+
<formatter type="xml"/>
104+
<formatter type="plain" usefile="no"/>
105+
<test name="com.flatironschool.javacs.MyArrayListTest" todir="${junit.output.dir}"/>
106+
<classpath refid="JavaCS.classpath"/>
107+
</junit>
108+
</target>
109+
<target name="SelectionSort">
110+
<java classname="com.flatironschool.javacs.SelectionSort" failonerror="true" fork="yes">
111+
<classpath refid="JavaCS.classpath"/>
112+
</java>
113+
</target>
114+
<target name="MyArrayListSoln">
115+
<java classname="com.flatironschool.javacs.MyArrayListSoln" failonerror="true" fork="yes">
116+
<classpath refid="JavaCS.classpath"/>
117+
</java>
118+
</target>
119+
<target name="MyArrayListTest.testGet">
120+
<mkdir dir="${junit.output.dir}"/>
121+
<junit fork="yes" printsummary="withOutAndErr">
122+
<formatter type="xml"/>
123+
<test name="com.flatironschool.javacs.MyArrayListTest" todir="${junit.output.dir}"/>
124+
<classpath refid="JavaCS.classpath"/>
125+
</junit>
126+
</target>
127+
<target name="MyArrayListTest.testIndexOf">
128+
<mkdir dir="${junit.output.dir}"/>
129+
<junit fork="yes" printsummary="withOutAndErr">
130+
<formatter type="xml"/>
131+
<test name="com.flatironschool.javacs.MyArrayListTest" todir="${junit.output.dir}"/>
132+
<classpath refid="JavaCS.classpath"/>
133+
</junit>
134+
</target>
135+
<target name="PieChart">
136+
<java classname="com.flatironschool.javacs.PieChart" failonerror="true" fork="yes">
137+
<classpath refid="JavaCS.classpath"/>
138+
</java>
139+
</target>
140+
<target name="LineChart_AWT">
141+
<java classname="com.flatironschool.javacs.LineChart_AWT" failonerror="true" fork="yes">
142+
<classpath refid="JavaCS.classpath"/>
143+
</java>
144+
</target>
145+
<target name="Profiler">
146+
<java classname="com.flatironschool.javacs.Profiler" failonerror="true" fork="yes">
147+
<classpath refid="JavaCS.classpath"/>
148+
</java>
149+
</target>
150+
<target name="XYLogAxesDemo">
151+
<java classname="com.flatironschool.javacs.XYLogAxesDemo" failonerror="true" fork="yes">
152+
<classpath refid="JavaCS.classpath"/>
153+
</java>
154+
</target>
155+
<target name="MyLinkedListTest">
156+
<mkdir dir="${junit.output.dir}"/>
157+
<junit fork="yes" printsummary="withOutAndErr">
158+
<formatter type="xml"/>
159+
<test name="com.flatironschool.javacs.MyLinkedListTest" todir="${junit.output.dir}"/>
160+
<classpath refid="JavaCS.classpath"/>
161+
</junit>
162+
</target>
163+
<target name="MyLinkedList">
164+
<java classname="com.flatironschool.javacs.MyLinkedList" failonerror="true" fork="yes">
165+
<classpath refid="JavaCS.classpath"/>
166+
</java>
167+
</target>
168+
<target name="MyLinkedListTest.testAddT">
169+
<mkdir dir="${junit.output.dir}"/>
170+
<junit fork="yes" printsummary="withOutAndErr">
171+
<formatter type="xml"/>
172+
<test name="com.flatironschool.javacs.MyLinkedListTest" todir="${junit.output.dir}"/>
173+
<classpath refid="JavaCS.classpath"/>
174+
</junit>
175+
</target>
176+
<target name="MyLinkedListTest.testAddIntT">
177+
<mkdir dir="${junit.output.dir}"/>
178+
<junit fork="yes" printsummary="withOutAndErr">
179+
<formatter type="xml"/>
180+
<test name="com.flatironschool.javacs.MyLinkedListTest" todir="${junit.output.dir}"/>
181+
<classpath refid="JavaCS.classpath"/>
182+
</junit>
183+
</target>
184+
<target name="junitreport">
185+
<junitreport todir="${junit.output.dir}">
186+
<fileset dir="${junit.output.dir}">
187+
<include name="TEST-*.xml"/>
188+
</fileset>
189+
<report format="frames" todir="${junit.output.dir}"/>
190+
</junitreport>
191+
</target>
192+
</project>
44 KB
Binary file not shown.

javacs-lab02/lib/junit-4.12.jar

308 KB
Binary file not shown.

0 commit comments

Comments
 (0)