From 27aae26546e8d140289e4372c1cfd85da19860b0 Mon Sep 17 00:00:00 2001 From: Jacob Fritz Date: Thu, 26 Feb 2015 13:42:44 -0500 Subject: [PATCH] Submission for the Testing Lab --- Average.java | 21 +++++++++++++ AverageTest.java | 69 +++++++++++++++++++++++++++++++++++++++++ FritzAnswers.md | 37 ++++++++++++++++++++++ Range.java | 80 ++++++++++++++++++++++++++++++++++++++++++++++++ RangeTest.java | 42 +++++++++++++++++++++++++ 5 files changed, 249 insertions(+) create mode 100644 Average.java create mode 100644 AverageTest.java create mode 100644 FritzAnswers.md create mode 100644 Range.java create mode 100644 RangeTest.java diff --git a/Average.java b/Average.java new file mode 100644 index 0000000..e011275 --- /dev/null +++ b/Average.java @@ -0,0 +1,21 @@ +public class Average +{ + private double sum = 0; + private int count = 0; + + public void addValue(double value) + { + sum += value; + count ++; + } + + public double getAverage() + { + return sum/count; + } + + public int getCount() + { + return count; + } +} \ No newline at end of file diff --git a/AverageTest.java b/AverageTest.java new file mode 100644 index 0000000..0ecadd1 --- /dev/null +++ b/AverageTest.java @@ -0,0 +1,69 @@ +import junit.framework.TestCase; + +public class AverageTest extends TestCase +{ + public void testNewAverage() + { + Average avg = new Average(); + + avg.addValue(5.5); + + assertEquals(1, avg.getCount()); + assertEquals(5.5, 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 testMultipleValues() + { + Average avg = new Average(); + + avg.addValue(5.0); + avg.addValue(3.0); + avg.addValue(4.0); + avg.addValue(8.0); + avg.addValue(1.0); + avg.addValue(6.0); + + assertEquals(6, avg.getCount()); + assertEquals(4.5, avg.getAverage(), 0.001); + } + + public void testMultipleAverageZero() + { + Average avg = new Average(); + + avg.addValue(5.5); + avg.addValue(-5.5); + avg.addValue(2.0); + avg.addValue(-2.0); + avg.addValue(1.5); + avg.addValue(-1.5); + + assertEquals(6, avg.getCount()); + assertEquals(0.0, avg.getAverage(), 0.001); + } + + public void testSameValues() + { + 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, avg.getCount()); + assertEquals(5.5, avg.getAverage(), 0.001); + } + +} \ No newline at end of file diff --git a/FritzAnswers.md b/FritzAnswers.md new file mode 100644 index 0000000..95b0c21 --- /dev/null +++ b/FritzAnswers.md @@ -0,0 +1,37 @@ +## Testing Lab Jacob Fritz + +**Question 1:** List three mistakes we can make when using an array. + 1. Using 1 as the first index, not 0. + 2. Going out of bounds when looping through an array. + 3. Forgetting to create the array - only declaring it (int[ ] nums;). + +**Question 2:** List at least one configuration of a Scoreboard for each of the scenarios below. + a) A new instance of the class. + + ```Scoreboard sb = new Scoreboard();``` + + b) An instance with a single mutation. + + ```sb.addScore(5);``` + + c) An instance with multiple mutations. + + ```sb.addScore(String name, int score)``` + +**Question 3:** Answer the "Testing Questions" descripted in the previous section. + a) What would a brand new instance of the class look like? + + ```Range r = new Range(0.0,2.0);``` + + b) What is the simplest mutation possible for the class? + ```getMin()``` or ```getMax()``` + + c) What else can we do to an instance of the class? + Check to see if the range contains a value. + Get the width of the range. + Get the minimum number found in the range. + Get the maximum number found in the range. + Determine if a range intersects with another range. + + d) What are the boundary conditions for the class? + The values entered must be doubles, and the values must be known when the class is created, as they cannot be changed later. \ No newline at end of file diff --git a/Range.java b/Range.java new file mode 100644 index 0000000..cc7d9bd --- /dev/null +++ b/Range.java @@ -0,0 +1,80 @@ +public class Range +{ + private double min = 0.0; + private double max = 0.0; + + public Range(double start, double stop) + { + if(start > stop) + min = stop; + else + min = start; + + if(start > stop) + max = start; + else + max = stop; + } + + public boolean contains(double value) + { + if(value >= min && value <= max) + return true; + else + return false; + } + + public double getWidth() + { + return max - min; + } + + public double getMin() + { + return min; + } + + public double getMax() + { + return max; + } + + public Range intersection(Range other) + { + double newMin = 0; + double newMax = 0; + + double minB = min; + double maxB = max; + + boolean stopMin = false; + boolean stopMax = false; + + while (stopMin == false) + { + if(other.contains(minB) == true) + { + newMin = minB; + stopMin = true; + } + else + minB++; + } + + while (stopMax == false) + { + if(other.contains(maxB)) + { + newMax = maxB; + stopMax = true; + } + else + maxB--; + } + + Range newRange = new Range(newMin,newMax); + + return newRange; + } + +} \ No newline at end of file diff --git a/RangeTest.java b/RangeTest.java new file mode 100644 index 0000000..28d9ab8 --- /dev/null +++ b/RangeTest.java @@ -0,0 +1,42 @@ +import junit.framework.TestCase; + +public class RangeTest extends TestCase +{ + public void testNewRange() + { + Range r = new Range(2.0,0.0); + } + + public void testContains() + { + Range r = new Range(2.0,0.0); + assertEquals(true, r.contains(1.5)); + } + + public void testWidth() + { + Range r = new Range(2.0,0.0); + assertEquals(2.0, r.getWidth()); + } + + public void testMin() + { + Range r = new Range(2.0,0.0); + assertEquals(0.0, r.getMin()); + } + + public void testMax() + { + Range r = new Range(2.0,0.0); + assertEquals(2.0, r.getMax()); + } + + public void testIntersection() + { + Range r1 = new Range(0.0,5.0); + Range r2 = new Range(1.0,4.0); + Range r3 = r1.intersection(r2); + assertEquals(1.0, r3.getMin()); + assertEquals(4.0, r3.getMax()); + } +} \ No newline at end of file