From 59cff4057ee3fb953c60e99f82b56802d5d0a8b4 Mon Sep 17 00:00:00 2001 From: Andrew Date: Wed, 11 Feb 2015 15:53:16 -0500 Subject: [PATCH 1/2] These labs are preeeetty long when you can't log onto your computer for the first half hour --- Answers.md | 8 +++ src/Average.java | 39 ++++++++++++ src/AverageTest.java | 77 +++++++++++++++++++++++ src/Range.java | 118 +++++++++++++++++++++++++++++++++++ src/RangeTest.java | 143 +++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 385 insertions(+) create mode 100644 Answers.md create mode 100644 src/Average.java create mode 100644 src/AverageTest.java create mode 100644 src/Range.java create mode 100644 src/RangeTest.java diff --git a/Answers.md b/Answers.md new file mode 100644 index 0000000..dc32f08 --- /dev/null +++ b/Answers.md @@ -0,0 +1,8 @@ +#Testing Lab +##Andrew Reed + +**Question 1:** One mistake that can be made when making an array is trying to fit two or more data types into an array (which has a specific data type) like trying to put a score and a name (string) in the same array. Another forgetting order the scores in the array from greatest to least. A third problem that might arise is, if using parallel rays instead of an object to record the scores and names, then one might forget to order both arrays at the same time, leading to the scores and the names to be out of order, thus the data being incorrect. + +**Question 2:** If a new scoreboard were to be created, then it would simply be a scoreboard with no values in it. If a single mutation were to occur to the scoreboard, then the scoreboard would be a scoreboard with one value and one name in it. If multiple mutations were to occur then the scoreboard would have multiple scores (with a maximum of 10 scores) each attached with a corresponding name, and ordered by top score to lowest score. + +**Question 3:** A brand new instance of the class would be a range of [0, 0), which would be empty. There is no mutation being done in this class, however the interesting thing that this class can do is create a new range based on where two ranges start and end (where they intersect). All other methods are call methods. The boundary conditions of this lab are any two numbers so long as they aren't equal to each other, if they are they will return an empty range. \ No newline at end of file diff --git a/src/Average.java b/src/Average.java new file mode 100644 index 0000000..e81ee42 --- /dev/null +++ b/src/Average.java @@ -0,0 +1,39 @@ +public class Average +{ + private double sum; + private int count; + + public Average() + { + sum = 0.0; + count = 0; + } + + public void addValue(double value) + { + sum += value; + count += 1; + } + + public double getAverage() + { + double s = 0; + + if (sum == 0) + { + s = 0; + } + + if (sum != 0) + { + s = sum/count; + } + + return s; + } + + public int getCount() + { + return count; + } +} \ No newline at end of file diff --git a/src/AverageTest.java b/src/AverageTest.java new file mode 100644 index 0000000..091ab88 --- /dev/null +++ b/src/AverageTest.java @@ -0,0 +1,77 @@ +import junit.framework.TestCase; + +/** + * A JUnit test case class. + * Every method starting with the word "test" will be called when running + * the test with JUnit. + */ +public class AverageTest extends TestCase { + + /** + * A test method. + * (Replace "X" with a name describing the test. You may write as + * many "testSomething" methods in this class as you wish, and each + * one will be called when running JUnit over this class.) + */ + public void testNewAverage() + { + Average avg = new Average(); + + assertEquals(0, avg.getCount()); + assertEquals(0, avg.getAverage(), 0.001); + } + +public void testSingleValue() + { + Average avg = new Average(); + + avg.addValue(5.5); + + assertEquals(5.5, avg.getAverage(), 0.001); + assertEquals(1, avg.getCount()); + } + +public void testDifferentValues() + { + Average avg = new Average(); + + avg.addValue(5.5); + avg.addValue(6.5); + avg.addValue(7.5); + avg.addValue(8.5); + avg.addValue(9.5); + + assertEquals(7.5, avg.getAverage(), 0.001); + assertEquals(5, avg.getCount()); + } + +public void testZeroValue() + { + Average avg = new Average(); + + avg.addValue(5.5); + avg.addValue(-5.5); + avg.addValue(3.5); + avg.addValue(-1.5); + avg.addValue(-2); + + assertEquals(0, avg.getAverage(), 0.001); + assertEquals(5, avg.getCount()); + } + +public void testSameValue() + { + Average avg = new Average(); + + avg.addValue(5.5); + avg.addValue(5.5); + avg.addValue(5.5); + avg.addValue(5.5); + avg.addValue(5.5); + + assertEquals(5.5, avg.getAverage(), 0.001); + assertEquals(5, avg.getCount()); + } + +} + diff --git a/src/Range.java b/src/Range.java new file mode 100644 index 0000000..865498c --- /dev/null +++ b/src/Range.java @@ -0,0 +1,118 @@ +public class Range +{ + private double min; + private double max; + + public Range(double start, double stop) + { + if (start > stop) + { + max = start; + min = stop; + } + + if (stop > start) + { + max = stop; + min = start; + } + + if (stop == start) + { + min = 0; + max = 0; + } + } + + public boolean contains(double value) + { + boolean s = false; + if (value >= min && value < max) + { + s = true; + } + + if (value < min || value >= max) + { + s = false; + } + + return s; + + } + + public double getWidth() + { + return max - min; + } + + public double getMin() + { + return min; + } + + public double getMax() + { + return max; + } + + public Range intersection(Range other) + { + double min2 = other.getMin(); + double max2 = other.getMax(); + + double min3 = 0.0; + double max3 = 0.0; + + if (min > min2) + { + min3 = min; + } + + if (min2 > min) + { + min3 = min2; + } + + if (min2 == min) + { + min3 = min; + } + + if (max < max2) + { + max3 = max; + } + + if (max2 < max) + { + max3 = max2; + } + + if (max2 == max) + { + max3 = max; + } + + if (max2 <= min) + { + min3 = 0; + max3 = 0; + } + + if (max <= min2) + { + min3 = 0; + max3 = 0; + } + + if (min == min2 && max == max2) + { + min3 = 0; + max3 = 0; + } + + Range result = new Range(min3, max3); + return result; + } +} \ No newline at end of file diff --git a/src/RangeTest.java b/src/RangeTest.java new file mode 100644 index 0000000..b8abb17 --- /dev/null +++ b/src/RangeTest.java @@ -0,0 +1,143 @@ +import junit.framework.TestCase; + +/** + * A JUnit test case class. + * Every method starting with the word "test" will be called when running + * the test with JUnit. + */ +public class RangeTest extends TestCase { + + /** + * A test method. + * (Replace "X" with a name describing the test. You may write as + * many "testSomething" methods in this class as you wish, and each + * one will be called when running JUnit over this class.) + */ + public void testZeroRange() { + + Range r = new Range(0, 0); + + assertEquals(0.0, r.getWidth()); + assertEquals(0.0, r.getMin()); + assertEquals(0.0, r.getMax()); + assertEquals(false, r.contains(0)); + } + + public void testPositiveRange() { + + Range r = new Range(3, 7); + + assertEquals(4.0, r.getWidth()); + assertEquals(3.0, r.getMin()); + assertEquals(7.0, r.getMax()); + assertEquals(true, r.contains(3.5)); + } + + public void testFlipPositiveRange() { + + Range r = new Range(3, 7); + + assertEquals(4.0, r.getWidth()); + assertEquals(3.0, r.getMin()); + assertEquals(7.0, r.getMax()); + assertEquals(true, r.contains(3.5)); + + } + + public void testNegativeRange() { + + Range r = new Range(-4, -1); + + assertEquals(3.0, r.getWidth()); + assertEquals(-4.0, r.getMin()); + assertEquals(-1.0, r.getMax()); + assertEquals(true, r.contains(-2.5)); + + } + + public void testFlipNegativeRange() { + + Range r = new Range(-1, -4); + + assertEquals(3.0, r.getWidth()); + assertEquals(-4.0, r.getMin()); + assertEquals(-1.0, r.getMax()); + assertEquals(true, r.contains(-2.5)); + } + //We now know that there isn't anything wrong with getWidth getMin getMax or contains methods. + //If a problem arises in testing of intersection method + //it will be because of a bug in the intersection method and not any other method + + public void testIntersectionRange() { + + Range r = new Range(3, 7); + Range s = new Range(2, 4); + Range bleh = r.intersection(s); + + assertEquals(1.0, bleh.getWidth()); + assertEquals(3.0, bleh.getMin()); + assertEquals(4.0, bleh.getMax()); + assertEquals(true, bleh.contains(3.5)); + } + + public void testReverseIntersectionRange() { + + Range r = new Range(3, 7); + Range s = new Range(2, 4); + Range bleh = s.intersection(r); + + assertEquals(1.0, bleh.getWidth()); + assertEquals(3.0, bleh.getMin()); + assertEquals(4.0, bleh.getMax()); + assertEquals(true, bleh.contains(3.5)); + } + + public void testSameRange() { + + Range r = new Range(3, 7); + Range s = new Range(2, 4); + Range bleh = r.intersection(r); + + assertEquals(0.0, bleh.getWidth()); + assertEquals(0.0, bleh.getMin()); + assertEquals(0.0, bleh.getMax()); + assertEquals(false, bleh.contains(0.0)); + } + + public void testNoIntersectionRange() { + + Range r = new Range(3, 7); + Range s = new Range(2, 3); + Range bleh = s.intersection(r); + + assertEquals(0.0, bleh.getWidth()); + assertEquals(0.0, bleh.getMin()); + assertEquals(0.0, bleh.getMax()); + assertEquals(false, bleh.contains(0.0)); + } + + public void testReverseNoIntersectionRange() { + + Range r = new Range(3, 7); + Range s = new Range(2, 3); + Range bleh = r.intersection(s); + + assertEquals(0.0, bleh.getWidth()); + assertEquals(0.0, bleh.getMin()); + assertEquals(0.0, bleh.getMax()); + assertEquals(false, bleh.contains(0.0)); + } + + public void testOneIsSmallerRange() { + + Range r = new Range(-3, 7); + Range s = new Range(2, 3); + Range bleh = r.intersection(s); + + assertEquals(1.0, bleh.getWidth()); + assertEquals(2.0, bleh.getMin()); + assertEquals(3.0, bleh.getMax()); + assertEquals(false, bleh.contains(3.0)); + } +} + From 66bd379cb45f8ee6982a0d1fae4185edadfcf1d5 Mon Sep 17 00:00:00 2001 From: Andrew Date: Tue, 24 Feb 2015 14:27:06 -0500 Subject: [PATCH 2/2] Finished the optional part --- Answers.md | 4 +- src/averageRangeIntersection.java | 34 ++++++++ src/averageRangeIntersectionTest.java | 113 ++++++++++++++++++++++++++ 3 files changed, 150 insertions(+), 1 deletion(-) create mode 100644 src/averageRangeIntersection.java create mode 100644 src/averageRangeIntersectionTest.java diff --git a/Answers.md b/Answers.md index dc32f08..8624752 100644 --- a/Answers.md +++ b/Answers.md @@ -5,4 +5,6 @@ **Question 2:** If a new scoreboard were to be created, then it would simply be a scoreboard with no values in it. If a single mutation were to occur to the scoreboard, then the scoreboard would be a scoreboard with one value and one name in it. If multiple mutations were to occur then the scoreboard would have multiple scores (with a maximum of 10 scores) each attached with a corresponding name, and ordered by top score to lowest score. -**Question 3:** A brand new instance of the class would be a range of [0, 0), which would be empty. There is no mutation being done in this class, however the interesting thing that this class can do is create a new range based on where two ranges start and end (where they intersect). All other methods are call methods. The boundary conditions of this lab are any two numbers so long as they aren't equal to each other, if they are they will return an empty range. \ No newline at end of file +**Question 3:** A brand new instance of the class would be a range of [0, 0), which would be empty. There is no mutation being done in this class, however the interesting thing that this class can do is create a new range based on where two ranges start and end (where they intersect). All other methods are call methods. The boundary conditions of this lab are any two numbers so long as they aren't equal to each other, if they are they will return an empty range. + +**Optional Question:** After running a 1,000,000,000 trial Monte Carlo simulation, I found that the probability of ranges intersecting when all points of the ranges are randomly generated between 0 and 1 is 0.666670703. The average length of these intersections was equal to 0.20001405957047122. \ No newline at end of file diff --git a/src/averageRangeIntersection.java b/src/averageRangeIntersection.java new file mode 100644 index 0000000..902bf36 --- /dev/null +++ b/src/averageRangeIntersection.java @@ -0,0 +1,34 @@ +public class averageRangeIntersection +{ + public static void main(String[] args) + { + Average probOverlap = new Average(); + Average avgOverlapLength = new Average(); + + int i = 0; + + while (i < 1000000000) + { + Range a = new Range(Math.random(), Math.random()); + Range b = new Range(Math.random(), Math.random()); + + Range c = a.intersection(b); + if (c.getWidth() == 0.0) + { + probOverlap.addValue(0); + } + + if (c.getWidth() != 0.0) + { + probOverlap.addValue(1); + avgOverlapLength.addValue(c.getWidth()); + } + + i += 1; + } + + System.out.println("The probability of an overlap: " + probOverlap.getAverage()); + System.out.println("The average length of all overlaps: " + avgOverlapLength.getAverage()); + + } +} \ No newline at end of file diff --git a/src/averageRangeIntersectionTest.java b/src/averageRangeIntersectionTest.java new file mode 100644 index 0000000..a9eff98 --- /dev/null +++ b/src/averageRangeIntersectionTest.java @@ -0,0 +1,113 @@ +import junit.framework.TestCase; + +/** + * A JUnit test case class. + * Every method starting with the word "test" will be called when running + * the test with JUnit. + */ +public class averageRangeIntersectionTest extends TestCase { + + /** + * A test method. + * (Replace "X" with a name describing the test. You may write as + * many "testSomething" methods in this class as you wish, and each + * one will be called when running JUnit over this class.) + */ + public void testAvgNoOverlap() { + Average probOverlap = new Average(); + Average avgOverlapLength = new Average(); + + int i = 0; + + while (i < 100) + { + Range a = new Range(.1, .3); + Range b = new Range(.5, .6); + + Range c = a.intersection(b); + if (c.getWidth() == 0.0) + { + probOverlap.addValue(0); + } + + if (c.getWidth() != 0.0) + { + probOverlap.addValue(1); + avgOverlapLength.addValue(c.getWidth()); + } + + i += 1; + } + + assertEquals(0.0, probOverlap.getAverage()); + assertEquals(0.0, avgOverlapLength.getAverage()); + } + + public void testAvgAllOverlap() { + Average probOverlap = new Average(); + Average avgOverlapLength = new Average(); + + int i = 0; + + while (i < 100) + { + Range a = new Range(.1, .6); + Range b = new Range(.5, .7); + + Range c = a.intersection(b); + if (c.getWidth() == 0.0) + { + probOverlap.addValue(0); + } + + if (c.getWidth() != 0.0) + { + probOverlap.addValue(1); + avgOverlapLength.addValue(c.getWidth()); + } + + i += 1; + } + + assertEquals(1.0, probOverlap.getAverage()); + assertEquals(.1, avgOverlapLength.getAverage(), .0001); + } + + public void testAvgOneOverlap() { + Average probOverlap = new Average(); + Average avgOverlapLength = new Average(); + + + Range a = new Range(.1, .3); + Range b = new Range(.5, .7); + Range d = new Range(.1, .6); + + Range c = a.intersection(b); + Range e = b.intersection(d); + + if (e.getWidth() == 0.0) + { + probOverlap.addValue(0); + } + + if (e.getWidth() != 0.0) + { + probOverlap.addValue(1); + avgOverlapLength.addValue(e.getWidth()); + } + + if (c.getWidth() == 0.0) + { + probOverlap.addValue(0); + } + + if (c.getWidth() != 0.0) + { + probOverlap.addValue(1); + avgOverlapLength.addValue(c.getWidth()); + } + + assertEquals(.5, probOverlap.getAverage()); + assertEquals(.1, avgOverlapLength.getAverage(), .0001); + } +}