Skip to content
Open
26 changes: 26 additions & 0 deletions Answers.md
Original file line number Diff line number Diff line change
@@ -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.




27 changes: 27 additions & 0 deletions src/Average.java
Original file line number Diff line number Diff line change
@@ -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;
}

}
70 changes: 70 additions & 0 deletions src/AverageTest.java
Original file line number Diff line number Diff line change
@@ -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());

}

}
35 changes: 35 additions & 0 deletions src/Main.java
Original file line number Diff line number Diff line change
@@ -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());



}
}

65 changes: 65 additions & 0 deletions src/Range.java
Original file line number Diff line number Diff line change
@@ -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);
}
}
84 changes: 84 additions & 0 deletions src/RangeTest.java
Original file line number Diff line number Diff line change
@@ -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());
}



}