diff options
| author | Alexander Pickering <alexandermpickering@gmail.com> | 2017-02-06 11:41:36 -0500 |
|---|---|---|
| committer | Alexander Pickering <alexandermpickering@gmail.com> | 2017-02-06 11:41:36 -0500 |
| commit | 89cdf3efb49335e7c07a68a5a64657eeec2288a6 (patch) | |
| tree | cdc0fd8165e65b1637fa54cac11c932acefc8a89 /labs/lab01_pair/Pair.java | |
| download | coe0445-master.tar.gz coe0445-master.tar.bz2 coe0445-master.zip | |
Diffstat (limited to 'labs/lab01_pair/Pair.java')
| -rw-r--r-- | labs/lab01_pair/Pair.java | 83 |
1 files changed, 83 insertions, 0 deletions
diff --git a/labs/lab01_pair/Pair.java b/labs/lab01_pair/Pair.java new file mode 100644 index 0000000..ef49c76 --- /dev/null +++ b/labs/lab01_pair/Pair.java @@ -0,0 +1,83 @@ + +public class Pair<T1,T2> implements PairInterface<T1,T2> +{ + // TO DO: Instance Variables + T1 one; + T2 two; + + public Pair(T1 aFirst, T2 aSecond) + { + one = aFirst; + two = aSecond; + } + + /** + * Gets the first element of this pair. + * @return the first element of this pair. + */ + public T1 fst() + { + return one; + } + + /** + * Gets the second element of this pair. + * @return the second element of this pair. + */ + public T2 snd() + { + return two; + } + + /** + * Sets the first element to aFirst. + * @param aFirst the new first element + */ + public void setFst(T1 aFirst) + { + one = aFirst; + } + + /** + * Sets the second element to aSecond. + * @param aSecond the new second element + */ + public void setSnd(T2 aSecond) + { + two = aSecond; + } + + /** + * Checks whether two pairs are equal. Note that the pair + * (a,b) is equal to the pair (x,y) if and only if a is + * equal to x and b is equal to y. + * @return true if this pair is equal to aPair. Otherwise + * return false. + */ + public boolean equals(Object otherObject) + { + if(otherObject == null) + { + return false; + } + + if(getClass() != otherObject.getClass()) + { + return false; + } + Pair pairObject = (Pair) otherObject; + return one.equals(pairObject.fst()) && two.equals(pairObject.snd()); + } + + /** + * Generates a string representing this pair. Note that + * the String representing the pair (x,y) is "(x,y)". There + * is no whitespace unless x or y or both contain whitespace + * themselves. + * @return a string representing this pair. + */ + public String toString() + { + return "(" + one.toString() + "," + two.toString() + ")"; + } +} |
