diff --git a/Answers.md b/Answers.md new file mode 100644 index 0000000..ba0a33c --- /dev/null +++ b/Answers.md @@ -0,0 +1,26 @@ +``` +Josh Russett +CSCI II Lab +10 Feb 2015 +``` +#Testing Lab Answers +####Question 1: +1. Declaring an array of incorrect type or length. +2. Using the wrong number to reference a particular position on the array (i.e. I want to reference the fourth position so I accidentally use array[4] instead array[3]). +3. Trying to put incompatible types into an array (i.e. trying to store a double in an integer array). + +####Question 2: +* In a new instance of the class, there should only be a newly created array. The numScores variable should be 0. +* In an instance with a single mutation, there should be an array with one score and the score's corresponding name. The numScores variable should be 1. +* In an instance with multiple mutations, there should be, at a max, 10 different scores and the score's corresponding names in a sorted order (ascending or decending). Also, the scores and names within the array should only be in the array if they were already compared to the rest of scores in the the array and confirmed to be higher than at least one of the scores in the array (with that lower value being replaced or moved). The numScores variable should be reflective to how many times the Scoreboard was mutated, so the value should be between 2 and 10. + + +####Question 3: +1. A brand new object would would have a start and a stop value, that's it. +2. This class does not contain any mutator methods. +3. We can query whether or not a a value lies within the range, query the length/width of a range, query the min/max values that define the range, and find out if a given range overlaps with the current range. +4. The boundary condition of this class is when something other than a double is passed to the range. + + + + diff --git a/src/Average.java b/src/Average.java new file mode 100644 index 0000000..7621560 --- /dev/null +++ b/src/Average.java @@ -0,0 +1,27 @@ +public class Average{ + private double sum; + private int count; + + public Average() { + sum = 0.0; + count = 0; + + } + + public void addValue(double value){ + sum += value; + count ++; + } + + 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..032cb5a --- /dev/null +++ b/src/AverageTest.java @@ -0,0 +1,70 @@ +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 test5DifferentAvgGreaterThanZero() { + Average avg = new Average(); + + int[] values = {2, 17, 16, 99, 34}; + + for (int i = 0; i < values.length; i ++){ + avg.addValue(values[i]); + } + + assertEquals(33.6, avg.getAverage(), 0.001); + assertEquals(5, avg.getCount()); + } + + public void test5DifferentAvgIsZero() { + Average avg = new Average(); + + int[] values = {-10, -5, -1, 9, 7}; + + for (int i = 0; i < values.length; i ++){ + avg.addValue(values[i]); + } + + assertEquals(0.0, avg.getAverage(), 0.001); + assertEquals(5, avg.getCount()); + } + + public void test5samevalues() { + Average avg = new Average(); + + for (int i = 0; i < 5; i ++){ + avg.addValue(5.0); + } + + assertEquals(5, avg.getAverage(), 0.001); + assertEquals(5, avg.getCount()); + + } + +} diff --git a/src/Main.java b/src/Main.java new file mode 100644 index 0000000..ae868a1 --- /dev/null +++ b/src/Main.java @@ -0,0 +1,35 @@ +public class Main{ + public static void main(String[] args){ + Average avgOverlap = new Average(); + Average avgLength = new Average(); + + int numSimulations = 10000000; + + for(int i = 0; i < numSimulations; i++) { + //Create two new ranges with random start and stop values + Range rng1 = new Range(Math.random(), Math.random()); + Range rng2 = new Range(Math.random(), Math.random()); + + //Create the intersection of the two ranges + Range rngIntersection = rng1.intersection(rng2); + + //Check if the two ranges overlapped, add 1 if they did, otherwise add 0. + if (rngIntersection.getWidth() != 0.0) + avgOverlap.addValue(1.0); + else + avgOverlap.addValue(0.0); + + //Add the width of the intersection width to the average. + avgLength.addValue(rngIntersection.getWidth()); + } + + //Print the result + System.out.println("After " + numSimulations + " simulations..."); + System.out.println("The probablity that the two random ranges will overlap is: " + avgOverlap.getAverage()); + System.out.println("The average length of overlap is: " + avgLength.getAverage()); + + + + } +} + \ No newline at end of file diff --git a/src/Range.java b/src/Range.java new file mode 100644 index 0000000..1ea7539 --- /dev/null +++ b/src/Range.java @@ -0,0 +1,65 @@ +public class Range{ + private double stop; + private double start; + private double a; + private double b; + private double newMax; + private double newMin; + + public Range(double a, double b){ + if (a == b) { + start = 0.0; + stop = 0.0; + } else { + start = Math.min(a, b); + stop = Math.max(a, b); + } + } + + public boolean contains(double value){ + if (value >= start && value < stop) + return true; + else + return false; + } + + public double getWidth(){ + return Math.abs(start - stop); + } + + public double getMin(){ + return start; + } + + public double getMax(){ + return stop; + } + + public Range intersection(Range other){ + //Are they equal? + if (start == other.getMin() && stop == other.getMax()) + return new Range(start, stop); + + //Make sure they actually overlap + if (other.getMin() > stop || start > other.getMax()) + return new Range(0.0, 0.0); + + //Determine Max, if any + if (other.getMax() > stop) + newMax = stop; + else if ( stop > other.getMax()) + newMax = other.getMax(); + else + newMax = stop; + + //Determine Min, if any + if ( other.getMin() > start) + newMin = other.getMin(); + else if (other.getMin() < start) + newMin = start; + else + newMin = start; + + 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..ee6e962 --- /dev/null +++ b/src/RangeTest.java @@ -0,0 +1,84 @@ +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 rng = new Range(0,0); + + assertEquals(false, rng.contains(0)); + assertEquals(0.0, rng.getWidth()); + assertEquals(0.0, rng.getMin()); + assertEquals(0.0, rng.getMax()); + + Range rng2 = rng.intersection(new Range(1.0,1.0)); + assertEquals(0.0, rng2.getMin()); + assertEquals(0.0, rng2.getMax()); + } + + public void testEqualRanges() { + Range rng1 = new Range(0.0, 5.0); + + assertEquals(true, rng1.contains(2.0)); + assertEquals(5.0, rng1.getWidth()); + assertEquals(0.0, rng1.getMin()); + assertEquals(5.0, rng1.getMax()); + + Range rng2 = rng1.intersection(new Range(0.0, 5.0)); + assertEquals(0.0, rng2.getMin()); + assertEquals(5.0, rng2.getMax()); + } + + public void testBiggerRange() { + Range rng1 = new Range(0.0, 5.0); + + assertEquals(true, rng1.contains(2.0)); + assertEquals(5.0, rng1.getWidth()); + assertEquals(0.0, rng1.getMin()); + assertEquals(5.0, rng1.getMax()); + + Range rng2 = rng1.intersection(new Range(2.0, 10.0)); + assertEquals(2.0, rng2.getMin()); + assertEquals(5.0, rng2.getMax()); + } + + public void testSmallerRange() { + Range rng1 = new Range(0.0, 5.0); + + assertEquals(true, rng1.contains(2.0)); + assertEquals(5.0, rng1.getWidth()); + assertEquals(0.0, rng1.getMin()); + assertEquals(5.0, rng1.getMax()); + + Range rng2 = rng1.intersection(new Range(-10.0, 2.0)); + assertEquals(0.0, rng2.getMin()); + assertEquals(2.0, rng2.getMax()); + } + + public void testNonIntersection() { + Range rng1 = new Range(0.0, 5.0); + + assertEquals(true, rng1.contains(2.0)); + assertEquals(5.0, rng1.getWidth()); + assertEquals(0.0, rng1.getMin()); + assertEquals(5.0, rng1.getMax()); + + Range rng2 = rng1.intersection(new Range(-10.0, -1.0)); + assertEquals(0.0, rng2.getMin()); + assertEquals(0.0, rng2.getMax()); + } + + + +} +