diff --git a/Answers.md b/Answers.md new file mode 100644 index 0000000..164fb1e --- /dev/null +++ b/Answers.md @@ -0,0 +1,27 @@ +**Question 1** There common mistakes that you can make with arrays are: + + -trying to access an index that doesn't exit + -forgetting that arrays start at index 0 + -trying to store different types in an array + + +**Question 2** In each of the scenarios the instance variables associated with the object will contain different data. + +in a new instance of the class: the arrays holding the scores and the names of the players will be empty, so if you try to query data you will get 0 for the scores and null for the String objects. + +with a single mutation: the added score will be added to the score array and the name will be added in the name array. + +with multiple mutations: the integers or Strings in the array will depend on the parameters being passed. If there are more than 10 mutations not all the data could be shown in the array because some could be bumped down to make room for others. + +**Question 3** + +in a new instance of the class: the two instance variables "start" and "stop" would be initialized in the constructor. + +There are no mutations in the range class so i can't really answer this question. + +The range class can query data about the range set up in the constructor. This includes returning whether or not the value contains a range, the width, the min, the max, and an intersection between this range and another range. + +the boundary conditions are the range's min and max + + + \ No newline at end of file diff --git a/src/Average.java b/src/Average.java new file mode 100644 index 0000000..145286e --- /dev/null +++ b/src/Average.java @@ -0,0 +1,21 @@ +public class Average { + private int count; + private double sum; + + public void addValue(double value) { + sum += value; + count++; + } + + public double getAverage() { + if (count == 0 ) { + return 0.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..bcc9133 --- /dev/null +++ b/src/AverageTest.java @@ -0,0 +1,56 @@ +import junit.framework.TestCase; + +public class AverageTest extends TestCase { + public void testNewAverage() { + Average avg = new Average(); + + assertEquals(0, avg.getCount()); + assertEquals(0.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 testMultValues1() { + Average avg = new Average(); + + avg.addValue(2); + avg.addValue(4); + avg.addValue(6); + avg.addValue(8); + avg.addValue(10); + + assertEquals(6.0, avg.getAverage(), 0.001); + assertEquals(5, avg.getCount()); + } + + public void testMultValues2() { + Average avg = new Average(); + + avg.addValue(-3); + avg.addValue(-2); + avg.addValue(-1); + avg.addValue(3); + avg.addValue(3); + + assertEquals(0.0, avg.getAverage(), 0.001); + assertEquals(5, avg.getCount()); + } + + public void testMultValues3() { + Average avg = new Average(); + + for (int i = 0; i < 5; i++) { + avg.addValue(6); + } + + assertEquals(6.0, avg.getAverage(), 0.001); + } + +} diff --git a/src/Range.java b/src/Range.java new file mode 100644 index 0000000..15d128c --- /dev/null +++ b/src/Range.java @@ -0,0 +1,55 @@ +public class Range { + private double start; + private double stop; + + Range(double start, double stop) { + 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 newStart = 0.0; //start for intersection + double newStop = 0.0; //stop for intersection + + if (other.contains(this.getMin())) { + newStart = this.getMin(); + } else if (this.contains(other.getMin())) { + newStart = other.getMin(); + } + + if (other.contains(this.getMax())) { + newStop = this.getMax(); + } else if (this.contains(other.getMax())) { + newStop = this.getMax(); + } + + return new Range(newStart, newStop); + } + +} + \ No newline at end of file diff --git a/src/RangeTest.java b/src/RangeTest.java new file mode 100644 index 0000000..44f577a --- /dev/null +++ b/src/RangeTest.java @@ -0,0 +1,21 @@ +import junit.framework.TestCase; + +public class RangeTest extends TestCase { + + public void testNewRange() { + Range r1 = new Range(1.0, 10.0); + assertEquals(1.0, r1.getMin(), 0.001); + Range r2 = new Range(5.0, -1.0); + assertEquals(-1.0, r2.getMin(), 0.001); + } + + public void testRangeIntersection() { + Range r1 = new Range(0.0, 10.0); + Range r2 = new Range(5.0, 15.0); + Range r3 = new Range(5.0, 10.0); + assertEquals(5.0, r3.getWidth()); + assertEquals(5.0, r3.getMin()); + assertEquals(10.0, r3.getMax()); + } + +}