From 7b97a1974d06e7c3769b5a47fb38e1293d16cb0c Mon Sep 17 00:00:00 2001 From: Josh Russett Date: Tue, 10 Feb 2015 13:19:32 -0500 Subject: [PATCH 01/11] Initial commit, setup of the Ansers.md file for this lab. --- Answers.md | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 Answers.md diff --git a/Answers.md b/Answers.md new file mode 100644 index 0000000..79d149b --- /dev/null +++ b/Answers.md @@ -0,0 +1,12 @@ +``` +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: \ No newline at end of file From ef21dead7e96c601d7b186f11b0540602c2c7a1b Mon Sep 17 00:00:00 2001 From: Josh Russett Date: Tue, 10 Feb 2015 13:38:58 -0500 Subject: [PATCH 02/11] Answered Question 2. --- Answers.md | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/Answers.md b/Answers.md index 79d149b..370d35c 100644 --- a/Answers.md +++ b/Answers.md @@ -9,4 +9,12 @@ CSCI II Lab 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: \ No newline at end of file +####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: + + From 8d624620814e65ccdb8e8be88ffdf427756381e5 Mon Sep 17 00:00:00 2001 From: Josh Russett Date: Tue, 10 Feb 2015 13:49:24 -0500 Subject: [PATCH 03/11] Completed the skeleton for the Average class. --- src/Average.java | 19 +++++++++++++++++++ src/Average.java~ | 17 +++++++++++++++++ 2 files changed, 36 insertions(+) create mode 100644 src/Average.java create mode 100644 src/Average.java~ diff --git a/src/Average.java b/src/Average.java new file mode 100644 index 0000000..a6473c5 --- /dev/null +++ b/src/Average.java @@ -0,0 +1,19 @@ +public class Average{ + private double average; + private int count; + + class Average(){ + } + + public void addValue(double value){ + } + + public double getAverage(){ + return 0; + } + + public int getCount(){ + return 0; + } + +} \ No newline at end of file diff --git a/src/Average.java~ b/src/Average.java~ new file mode 100644 index 0000000..af38e3b --- /dev/null +++ b/src/Average.java~ @@ -0,0 +1,17 @@ +public class Average{ + private double average; + private int count; + + class Average(){ + } + + public void addValue(double value){ + } + + double getAverage(){ + } + + int getCount(){ + } + +} \ No newline at end of file From e0e67704cd81a09c47488278bb8d2c76f5f9e6d7 Mon Sep 17 00:00:00 2001 From: Josh Russett Date: Tue, 10 Feb 2015 13:51:03 -0500 Subject: [PATCH 04/11] Fixed the skeleton of the Average class. --- src/Average.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Average.java b/src/Average.java index a6473c5..37acfd2 100644 --- a/src/Average.java +++ b/src/Average.java @@ -2,7 +2,7 @@ public class Average{ private double average; private int count; - class Average(){ + public Average() { } public void addValue(double value){ From b6cfb0e9f04d08456a821412ac7e2bdee8a46f13 Mon Sep 17 00:00:00 2001 From: Josh Russett Date: Tue, 10 Feb 2015 14:02:17 -0500 Subject: [PATCH 05/11] Finished TDD -- Step 2. --- src/Average.java | 14 +++++++++++--- src/Average.java~ | 17 ----------------- 2 files changed, 11 insertions(+), 20 deletions(-) delete mode 100644 src/Average.java~ diff --git a/src/Average.java b/src/Average.java index 37acfd2..7621560 100644 --- a/src/Average.java +++ b/src/Average.java @@ -1,19 +1,27 @@ public class Average{ - private double 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(){ - return 0; + if (count == 0) + return 0; + else + return sum/count ; } public int getCount(){ - return 0; + return count; } } \ No newline at end of file diff --git a/src/Average.java~ b/src/Average.java~ deleted file mode 100644 index af38e3b..0000000 --- a/src/Average.java~ +++ /dev/null @@ -1,17 +0,0 @@ -public class Average{ - private double average; - private int count; - - class Average(){ - } - - public void addValue(double value){ - } - - double getAverage(){ - } - - int getCount(){ - } - -} \ No newline at end of file From c7833234812673d0427737edf44fffb91ee07aad Mon Sep 17 00:00:00 2001 From: Josh Russett Date: Tue, 10 Feb 2015 14:30:32 -0500 Subject: [PATCH 06/11] Finished TDD -- Step 3, done constructing all the tests for the Average class. --- src/AverageTest.java | 70 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 src/AverageTest.java 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()); + + } + +} From e7539bb56e16481971a3e887c2c3962bd68e9377 Mon Sep 17 00:00:00 2001 From: Josh Russett Date: Tue, 10 Feb 2015 15:24:05 -0500 Subject: [PATCH 07/11] Started the Range class and RangeTest --- Answers.md | 6 ++++++ src/Range.java | 28 ++++++++++++++++++++++++++++ src/RangeTest.java | 31 +++++++++++++++++++++++++++++++ 3 files changed, 65 insertions(+) create mode 100644 src/Range.java create mode 100644 src/RangeTest.java diff --git a/Answers.md b/Answers.md index 370d35c..ba0a33c 100644 --- a/Answers.md +++ b/Answers.md @@ -16,5 +16,11 @@ CSCI II Lab ####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/Range.java b/src/Range.java new file mode 100644 index 0000000..a0099d5 --- /dev/null +++ b/src/Range.java @@ -0,0 +1,28 @@ +public class Range{ + private double stop; + private double start; + + public Range(double start, double stop){ + + } + + public boolean contains(double value){ + return true; + } + + public double getWidth(){ + return Math.abs(start - stop); + } + + public double getMin(){ + return start; + } + + public double getMax(){ + return stop; + } + + public Range intersection(Range other){ + return new Range(0.0, 0.0); + } +} \ No newline at end of file diff --git a/src/RangeTest.java b/src/RangeTest.java new file mode 100644 index 0000000..c3418b7 --- /dev/null +++ b/src/RangeTest.java @@ -0,0 +1,31 @@ +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(true, 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,1)); + assertEquals(0.0, rng.getMin()); + assertEquals(0.0, rng.getMax()); + + + } + +} From 4c2386e1a499ef70f832ea219843fd86b490f0a2 Mon Sep 17 00:00:00 2001 From: Josh Russett Date: Tue, 10 Feb 2015 15:48:39 -0500 Subject: [PATCH 08/11] Finished rewriting the Range Class, need to vigorously test using the RangeTest class. --- src/Range.java | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/src/Range.java b/src/Range.java index a0099d5..fd4635c 100644 --- a/src/Range.java +++ b/src/Range.java @@ -3,11 +3,20 @@ public class Range{ private double start; public Range(double start, double stop){ - + if (start == stop) { + start = 0.0; + stop = 0.0; + } else { + stop = Math.max(start, stop); + start = Math.min(start, stop); + } } public boolean contains(double value){ - return true; + if (value > start && value < stop) + return true; + else + return false; } public double getWidth(){ @@ -23,6 +32,13 @@ public double getMax(){ } public Range intersection(Range other){ - return new Range(0.0, 0.0); + if (stop < other.getMin) + return new Range(other.getMin, stop); + else if (other.getMax > start) + return new Range(start, other.getMax); + else if (start == other.getMin && stop == other.getMax) + return new Range(start, stop); + else + return new Range(0.0, 0.0); } } \ No newline at end of file From 9ec8bdefe3ddf8f201f7c89d56d6a127720716d6 Mon Sep 17 00:00:00 2001 From: Josh Russett Date: Sun, 15 Feb 2015 22:03:26 -0500 Subject: [PATCH 09/11] Final Commit; this lab should be complete. --- src/Range.java | 43 +++++++++++++++++++++++--------- src/RangeTest.java | 61 +++++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 89 insertions(+), 15 deletions(-) diff --git a/src/Range.java b/src/Range.java index fd4635c..1ea7539 100644 --- a/src/Range.java +++ b/src/Range.java @@ -1,19 +1,23 @@ public class Range{ private double stop; private double start; + private double a; + private double b; + private double newMax; + private double newMin; - public Range(double start, double stop){ - if (start == stop) { + public Range(double a, double b){ + if (a == b) { start = 0.0; stop = 0.0; } else { - stop = Math.max(start, stop); - start = Math.min(start, stop); + start = Math.min(a, b); + stop = Math.max(a, b); } } public boolean contains(double value){ - if (value > start && value < stop) + if (value >= start && value < stop) return true; else return false; @@ -32,13 +36,30 @@ public double getMax(){ } public Range intersection(Range other){ - if (stop < other.getMin) - return new Range(other.getMin, stop); - else if (other.getMax > start) - return new Range(start, other.getMax); - else if (start == other.getMin && stop == other.getMax) + //Are they equal? + if (start == other.getMin() && stop == other.getMax()) return new Range(start, stop); - else + + //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 index c3418b7..ee6e962 100644 --- a/src/RangeTest.java +++ b/src/RangeTest.java @@ -16,16 +16,69 @@ public class RangeTest extends TestCase { public void testNewRange() { Range rng = new Range(0,0); - assertEquals(true, rng.contains(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,1)); - 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()); + } + + + } + From 8a215f649ed2998752c3fa935c002132244a3d54 Mon Sep 17 00:00:00 2001 From: Josh Russett Date: Tue, 24 Feb 2015 14:09:05 -0500 Subject: [PATCH 10/11] Lab Finishing Day, did the optional Average Range Intersection. --- src/Main.java | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 src/Main.java diff --git a/src/Main.java b/src/Main.java new file mode 100644 index 0000000..af20166 --- /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 = 100000*100000; + + 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 From 79fe6536107d49a7078f3c6efdc2a48ef63e7d87 Mon Sep 17 00:00:00 2001 From: Josh Russett Date: Tue, 24 Feb 2015 14:11:39 -0500 Subject: [PATCH 11/11] Fixed Main.java, should be finished now. --- src/Main.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Main.java b/src/Main.java index af20166..ae868a1 100644 --- a/src/Main.java +++ b/src/Main.java @@ -3,7 +3,7 @@ public static void main(String[] args){ Average avgOverlap = new Average(); Average avgLength = new Average(); - int numSimulations = 100000*100000; + int numSimulations = 10000000; for(int i = 0; i < numSimulations; i++) { //Create two new ranges with random start and stop values