diff --git a/Answers.md b/Answers.md new file mode 100644 index 0000000..21a6221 --- /dev/null +++ b/Answers.md @@ -0,0 +1,22 @@ +# Testing Lab Answers +### John Vonelli + +** Question 1 **: Three mistakes that we can make when using an array are as follows. When determining the type of the class, if you would want to have values inserted as a *double* and you would accidentally initialize the array as an *integer*, that would truncate your information so it would fit into the *integer*. Another mistake that one could make when using an array is that you could mistakenly add information to the wrong variable in the array. For example, if you were to have values that was supposed to be added to `array[0]`, but accidentally added to `array[1]`, you could distort your variables and information within your array. Lastly, it is possible that you could use `{}` instead of `[]` when initializing the array, causing a syntax error. + +**Question 2**: When making a new `Scoreboard()`, there are no variables and would have no new information besides zero values. When there is a new instance of `Scoreboard()` with one mutation, that would signify that there would be one value within that instance of the class. So the user would be able to utilize the `isHighScore()`, the `getName()`, the `getScore()`, and the `getNumScores()` methods to recieve information. When there is a new instance of `Scoreboard()` with multiple mutations, that would mean that the instance of the class would hold multiple values and information. The user then would be able to utilze the methods mentioned above to access and to understand that information. + +**Question 3**: Here are the answers to the testing questions for the `Range` class. + +- **1**. A brand new instance of the class would have zero values for the variables, and the class would be ready to be utilized. +- **2**. Besides initializing the constructor `Range()` (which you could initialize to `Range(0,2)`), there are no other mutators that I could manipulate in this class. Thus, there are no other simplier mutations possible for this class. +- **3**. We can check the other methods to find out other information for the instance of the class. +- **4**. The boundary conditions for the `Range()` constructor are as follows. The lower boundary (start) of `Range()` is inclusive, while the upper boundary (stop) of `Range()` is exclusive. So for example, if you initialize `Range(0,2)`, then the range will be valid for 0 through 1. + + + + + + + + + diff --git a/Average.class b/Average.class new file mode 100644 index 0000000..094281f Binary files /dev/null and b/Average.class differ diff --git a/Average.java b/Average.java new file mode 100644 index 0000000..85de47b --- /dev/null +++ b/Average.java @@ -0,0 +1,25 @@ +public class Average{ + private double sum = 0; + private int count = 0; + + public Average(){ + } + + public void addValue(double value){ + sum += value; + count++; + } + public double getAverage(){ + if(count == 0){ + return 0; + } + return (double)sum / count; + } + public int getCount(){ + return count; + } +} + + + + \ No newline at end of file diff --git a/AverageTest.class b/AverageTest.class new file mode 100644 index 0000000..29065a7 Binary files /dev/null and b/AverageTest.class differ diff --git a/AverageTest.java b/AverageTest.java new file mode 100644 index 0000000..bc2b3d4 --- /dev/null +++ b/AverageTest.java @@ -0,0 +1,53 @@ +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 testNotZero(){ + Average avg = new Average(); + avg.addValue(5.0); + avg.addValue(5.0); + avg.addValue(5.0); + avg.addValue(5.0); + avg.addValue(5.0); + assertEquals(5.0, avg.getAverage(), 0.001); + assertEquals(5, avg.getCount()); + } + public void testIsZero(){ + Average avg = new Average(); + avg.addValue(2.0); + avg.addValue(-2.0); + avg.addValue(1.0); + avg.addValue(-1.0); + avg.addValue(0.0); + assertEquals(0.0, avg.getAverage(), 0.001); + assertEquals(5, avg.getCount()); + } + public void testSameVal(){ + Average avg = new Average(); + avg.addValue(1.1); + avg.addValue(1.1); + avg.addValue(1.1); + avg.addValue(1.1); + avg.addValue(1.1); + assertEquals(1.1, avg.getAverage(), 0.001); + assertEquals(5, avg.getCount()); + } + + + +} diff --git a/Range.class b/Range.class new file mode 100644 index 0000000..2adfdc1 Binary files /dev/null and b/Range.class differ diff --git a/Range.java b/Range.java new file mode 100644 index 0000000..038c850 --- /dev/null +++ b/Range.java @@ -0,0 +1,92 @@ +public class Range{ + private double begin = 0.0; + private double end = 0.0; + private double begin2 = 0.0; + private double end2; + private double width = 0.0; + private double result = 0.0; + + public Range(double start, double stop){ + begin = start; + end = stop; + } + public boolean contains(double value){ + if(value >= begin && value < end){ + return true; + } + else{ + return false; + } + } + public double getWidth(){ + return Math.abs(begin - end); + } + public double getMin(){ + return begin; + } + public double getMax(){ + return end; + } + public Range intersection(Range other){ + begin2 = other.getMin(); + end2 = other.getMax(); + + if(begin == begin2 && end == end2){ + Range g = new Range(begin2, end2); + return g; + } + else if(begin2 > begin && end2 < end){ + Range g = new Range(begin2, end2); + return g; + } + else if(begin2 < begin && end2 > end){ + Range g = new Range(begin, end); + return g; + } + else if(begin2 == begin && end2 > end){ + return new Range(begin2, end); + } + else if(begin2 == begin && end2 < end){ + Range g = new Range(begin2, end2); + return g; + } + else if(begin2 < begin && end2 == end){ + Range g = new Range(begin, end2); + return g; + } + else if(begin2 > begin && end2 == end){ + Range g = new Range(begin2, end2); + return g; + } + // Wrong if + else if(begin2 > begin && end < end2){ + if(begin < end2 && begin2 < end && end2 - begin2 > begin) + { + Range g = new Range(begin2, end); + return g; + } + else if(begin < end2 && end < end2){ + Range g = new Range(0.0,0.0); + return g; + } + else{ + Range g = new Range(0.0, 0.0); + return g; + } + } + else if(begin2 < begin && end > end2){ + Range g = new Range(begin, end2); + return g; + } + // + else if(begin2 > end || begin2 < begin){ + Range g = new Range(0.0,0.0); + return g; + } + else{ + Range g = new Range(0.0,0.0); + return g; + } + + } + } diff --git a/RangeTest.class b/RangeTest.class new file mode 100644 index 0000000..6b3a7f9 Binary files /dev/null and b/RangeTest.class differ diff --git a/RangeTest.java b/RangeTest.java new file mode 100644 index 0000000..927fe7b --- /dev/null +++ b/RangeTest.java @@ -0,0 +1,86 @@ +import junit.framework.TestCase; + +public class RangeTest extends TestCase { + + + public void testNewRangeContains() { + Range r = new Range(0,2); + assertEquals(true, r.contains(0)); + assertEquals(true, r.contains(1)); + assertEquals(false, r.contains(2)); + + } + public void testNewWidth(){ + Range r = new Range(0,2); + assertEquals(2.0, r.getWidth()); + } + public void testMinMax(){ + Range r = new Range(0,2); + assertEquals(0.0, r.getMin()); + assertEquals(2.0, r.getMax()); + } + public void testIntersectionEqual(){ + Range r = new Range(1.0,3.0); + Range s = new Range(1.0,3.0); + Range intersect = r.intersection(s); + assertEquals(1.0, intersect.getMin()); + assertEquals(3.0, intersect.getMax()); + } + public void testIntersectionNotEqual(){ + Range r = new Range(1.0, 3.0); + Range s = new Range(5.0, 10.0); + Range intersect = r.intersection(s); + assertEquals(0.0, intersect.getMin()); + assertEquals(0.0, intersect.getMax()); + } + public void testIntersectionWithin(){ + Range r = new Range(2.0, 4.0); + Range s = new Range(1.0, 10.0); + Range intersect = r.intersection(s); + assertEquals(2.0, intersect.getMin()); + assertEquals(4.0, intersect.getMax()); + } + public void testIntersectionWithin2(){ + Range r = new Range(1.0, 10.0); + Range s = new Range(2.0, 4.0); + Range intersect = r.intersection(s); + assertEquals(2.0, intersect.getMin()); + assertEquals(4.0, intersect.getMax()); + } + public void testIntersectionBeginEqual(){ + Range r = new Range(2.0, 4.0); + Range s = new Range(2.0, 6.0); + Range intersect = r.intersection(s); + assertEquals(2.0, intersect.getMin()); + assertEquals(4.0, intersect.getMax()); + } + public void testIntersectionBeginEqual2(){ + Range r = new Range(2.0, 6.0); + Range s = new Range(2.0, 4.0); + Range intersect = r.intersection(s); + assertEquals(2.0, intersect.getMin()); + assertEquals(4.0, intersect.getMax()); + } + public void testIntersectionEndEqual(){ + Range r = new Range(2.0, 6.0); + Range s = new Range(1.0, 6.0); + Range intersect = r.intersection(s); + assertEquals(2.0, intersect.getMin()); + assertEquals(6.0, intersect.getMax()); + } + public void testIntersectionEndEqual2(){ + Range r = new Range(1.0, 4.0); + Range s = new Range(2.0, 4.0); + Range intersect = r.intersection(s); + assertEquals(2.0, intersect.getMin()); + assertEquals(4.0, intersect.getMax()); + } + public void testIntersectionOverlap(){ + Range r = new Range(1.0, 3.0); + Range s = new Range(2.0, 4.0); + Range intersect = r.intersection(s); + assertEquals(2.0, intersect.getMin()); + assertEquals(3.0, intersect.getMax()); + } +} +