diff --git a/Answers.md b/Answers.md new file mode 100644 index 0000000..bfc7311 --- /dev/null +++ b/Answers.md @@ -0,0 +1,20 @@ +# John Ferguson +# Testing Lab 2/12/15 + +>**Question 1:** List three mistakes we can make when using an array. + +**Answer 1:** Three ways we can makes mistakes when using an array is people use length in arrays, arrays already know their length. The a second common mistake is that the array always starts at 0 not 1. A third thing people mistake is storing a type that isn't compatible with the array type, it has to be the same type in the array. + +>**Question 2:** List at least one configuration of a Scoreboard for each of the above scenarios. + +**Answer 2:** For a new instance of the class, a configuration could be Class.newInstance(). The arrays are empty in the new class. An instance with a single mutation would increment the array by 1 at the begging of the score array and string array. An instance with multiple mutations depends what the scores are but there will be multiple scores in sorted order in the score array and the string array. + +>**Question 3:** Answer the "testing questions" descripted in the previous section. + +**Answer 3:** What would a brand new instance of the class look like? A brand new instance of the class for the range would look like a new range in the array set at 0 to the range we want. + +What is the simplest mutation possible for the class? The simplest mutation for the class would be to increment the range by 1. + +What else can we do to an instance of the class? Something else we can do to the instance of the class could be to get the Min and the Max. + +What are teh boundary conditions for the class? The boundary conditions for the class are also the min and max because they determine the starting the stopping points. \ No newline at end of file diff --git a/README.md b/README.md index 45c6b49..e1051df 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -# CSCI 121: Computer Science II +ls# CSCI 121: Computer Science II ## Testing Lab ### Overview diff --git a/src/Average.java b/src/Average.java new file mode 100644 index 0000000..acdb178 --- /dev/null +++ b/src/Average.java @@ -0,0 +1,24 @@ +public class Average { + private double sum = 0.0; + private int count = 0; + + + public void addValue(double value) { + sum += value; + count += 1; + } + + public double getAverage() { + if (count == 0) { + return 0; + } + + 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..7b6ab6f --- /dev/null +++ b/src/AverageTest.java @@ -0,0 +1,62 @@ +import junit.framework.TestCase; + + +public class AverageTest extends TestCase { + + + 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 testFiveValuesNonzeroAverage() { + Average avg = new Average(); + + avg.addValue(2.0); + avg.addValue(1.5); + avg.addValue(10.0); + avg.addValue(5.0); + avg.addValue(3.5); + + assertEquals(4.4, avg.getAverage(), 0.001); + assertEquals(5, avg.getCount()); + + } + + public void testFiveValuesZeroAverage() { + Average avg = new Average(); + + avg.addValue(0.0); + avg.addValue(0.0); + avg.addValue(0.0); + avg.addValue(0.0); + avg.addValue(0.0); + + assertEquals(0.0, avg.getAverage(), 0.001); + assertEquals(5, avg.getCount()); + } + + public void testFiveValuesSame() { + Average avg = new Average(); + + avg.addValue(9.0); + avg.addValue(9.0); + avg.addValue(9.0); + avg.addValue(9.0); + avg.addValue(9.0); + + assertEquals(9.0, avg.getAverage(), 0.001); + assertEquals(5, avg.getCount()); + } +} diff --git a/src/Range.java b/src/Range.java new file mode 100644 index 0000000..c4a38aa --- /dev/null +++ b/src/Range.java @@ -0,0 +1,65 @@ +public class Range { + private double start; + private double stop; + + + public Range(double start, double stop) { + int randI = (int)(Math.random()*1); + if (start <= stop) { + this.start = start; + this.stop = stop; + } + else { + this.start = stop; + this.stop = start; + } + + + } + + public boolean contains(double value) { + + if ((value >= start) && (value < stop)) { + return true; + } + return false; + + + } + + public double getWidth() { + return stop - start; + } + + public double getMin() { + return start; + } + + public double getMax() { + return stop; + } + + public Range intersection(Range other) { + double newMin = 0.0; + double newMax = 0.0; + + if (other.contains(start)) { + newMin = start; + } + if (other.contains(stop)) { + newMax = stop; + } + + if (contains(other.getMin())) { + newMin = other.getMin(); + } + + if (contains(other.getMax())) { + 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..848f357 --- /dev/null +++ b/src/RangeTest.java @@ -0,0 +1,52 @@ +import junit.framework.TestCase; + + +public class RangeTest extends TestCase { + + + public void testNewRange() { + Range rng = new Range(0.0, 0.0); + } + + public void testMin() { + Range r1 = new Range(1.0, 10.0); + + assertEquals(1.0, r1.getMin()); + + Range r2 = new Range(5.0, -1.0); + + assertEquals(-1.0, r2.getMin()); + } + + public void testMax() { + Range r1 = new Range(10.0, 1.0); + assertEquals(10.0, r1.getMax()); + + Range r2 = new Range(-1.0, 5.0); + assertEquals(5.0, r2.getMax()); + + } + + public void testWidth() { + Range r1 = new Range(10.0, 1.0); + assertEquals(9.0, r1.getWidth()); + + Range r2 = new Range(-1.0, 5.0); + assertEquals(6.0, r2.getWidth()); + + } + public void testbooleancontains() { + Range r1 = new Range(10.0, 1.0); + assertTrue(r1.contains(4.0)); + + Range r2 = new Range(5.0, -1.0); + assertFalse(r2.contains(7.0)); + } + + public void testRangeintersection() { + Range r1 = new Range(10.0, 1.0); + Range r2 = new Range(5.0, -1.0); + Range r3 = r1.intersection(r2); + assertEquals(4.0, r3.getWidth(), 0.001); + } +}