From f4013768762d0501a437d3089c34f3ad038b8192 Mon Sep 17 00:00:00 2001 From: Ryan Romano Date: Wed, 11 Mar 2015 22:44:11 -0400 Subject: [PATCH] Initial commit for Testing Lab --- Answers.md | 8 +++++ src/Average.java | 23 ++++++++++++++ src/AverageTest.java | 71 ++++++++++++++++++++++++++++++++++++++++++ src/Range.java | 74 ++++++++++++++++++++++++++++++++++++++++++++ src/RangeTest.java | 33 ++++++++++++++++++++ src/Scoreboard.java | 6 ++++ 6 files changed, 215 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 create mode 100644 src/Scoreboard.java diff --git a/Answers.md b/Answers.md new file mode 100644 index 0000000..23d04a8 --- /dev/null +++ b/Answers.md @@ -0,0 +1,8 @@ +# Testing Lab Answers + +**Question 1:** For the array that would be used in ```Scoreboard```, three possible mistakes that someone could make are returning a name's position even though the array is empty, returning a number of high scores outside of the index, and adding a value to the array with the wrong type. + +**Question 2:** ```Scoreboard``` is created for the first time and it's empty. A name and a score are added to the array. To mutate the array multiple times, names and scores are added or removed. + +**Question 3:** +A brand new instance of the ```Range``` class would contain only the values of start and stop that were passed to it. Nothing is mutated in the class. The class can also return a boolean for if a value is within the range, return the width, min, or max, and return the intersection of two ranges. The boundary conditions are min and max. diff --git a/src/Average.java b/src/Average.java new file mode 100644 index 0000000..3f0a9ae --- /dev/null +++ b/src/Average.java @@ -0,0 +1,23 @@ +public class Average { + + double sum = 0; + int count = 0; + + public void addValue(double value) { + sum += value; + count += 1; + } + + public double getAverage() { + if(count == 0) { + return 0; + } + else { + return sum / count; + } + } + + 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..36b12ca --- /dev/null +++ b/src/AverageTest.java @@ -0,0 +1,71 @@ +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 testFiveValues() { + Average avg = new Average(); + + avg.addValue(2); + avg.addValue(8); + avg.addValue(7); + avg.addValue(15); + avg.addValue(6); + + assertEquals(7.6, avg.getAverage(), 0.001); + assertEquals(5, avg.getCount()); + } + + public void testAverageZero() { + Average avg = new Average(); + + avg.addValue(-2); + avg.addValue(2); + avg.addValue(-5); + avg.addValue(5); + avg.addValue(0); + + assertEquals(0, avg.getAverage(), 0.001); + assertEquals(5, avg.getCount()); + } + + public void testSameValues() { + Average avg = new Average(); + + avg.addValue(4); + avg.addValue(4); + avg.addValue(4); + avg.addValue(4); + avg.addValue(4); + + assertEquals(4, avg.getAverage(), 0.001); + assertEquals(5, avg.getCount()); + } + +} diff --git a/src/Range.java b/src/Range.java new file mode 100644 index 0000000..0f5b507 --- /dev/null +++ b/src/Range.java @@ -0,0 +1,74 @@ +public class Range { + private double min = 0; + private double max = 0; + + public Range(double start, double stop) { + if (start < stop) { + min = start; + max = stop; + } + else if (start > stop) { + min = stop; + max = start; + } + else { + min = 0; + max = 0; + } + } + + public boolean contains(double value) { + return value >= min && value < max; + } + + public double getWidth() { + if (min < max) { + return max - min; + } + else if (min > max) { + return min - max; + } + else { + return 0; + } + } + + public double getMin() { + if(min < max) { + return min; + } + else { + return max; + } + } + + public double getMax() { + if(min > max) { + return min; + } + else { + return max; + } + } + + public Range intersection(Range other) { + double newMin = 0; + double newMax = 0; + + if (min > other.getMin()) { + newMin = min; + } + else { + newMin = other.getMin(); + } + + if (max < other.getMax()) { + newMax = max; + } + else { + newMax = other.getMax(); + } + + return new Range(newMin, newMax); + } +} \ No newline at end of file diff --git a/src/RangeTest.java b/src/RangeTest.java new file mode 100644 index 0000000..7f1b475 --- /dev/null +++ b/src/RangeTest.java @@ -0,0 +1,33 @@ +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 testNewRange() { + Range range = new Range(5.0, 7.0); + + assertTrue(range.contains(6.5)); + assertEquals(2.0, range.getWidth()); + assertEquals(5.0, range.getMin()); + assertEquals(7.0, range.getMax()); + } + + public void testInstersection() { + Range range = new Range(3.0, 10.0); + Range other = new Range(5.0, 11.0); + Range inter = range.intersection(other); // 5.0, 10.0 + assertEquals(5.0, inter.getWidth()); + assertEquals(5.0, inter.getMin()); + assertEquals(10.0, inter.getMax()); + } +} diff --git a/src/Scoreboard.java b/src/Scoreboard.java new file mode 100644 index 0000000..d575813 --- /dev/null +++ b/src/Scoreboard.java @@ -0,0 +1,6 @@ +public class Scoreboard { + public static void main(String[] args) { + int[] scores = new int[100]; + System.out.println(scores[0]); + } +} \ No newline at end of file