Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions src/Average.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
public class Average {
private double sum = 0.0;
private int count = 0;

public void addValue(double value) {
sum += value;
count += 1;
}

public double getAverage() {
if (sum == 0 && count == 0) {
return 0.0;
}
else {
return sum/count;
}
}

public int getCount() {
return count;
}
}
58 changes: 58 additions & 0 deletions src/AverageTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
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 testNonZeroes() {
Average avg = new Average();

avg.addValue(1);
avg.addValue(2);
avg.addValue(3);
avg.addValue(4);
avg.addValue(5);

assertEquals(3.0, avg.getAverage(), 0.001);
assertEquals(5, avg.getCount());
}

public void testZeroes() {
Average avg = new Average();

avg.addValue(-2);
avg.addValue(-1);
avg.addValue(0);
avg.addValue(1);
avg.addValue(2);

assertEquals(0.0, avg.getAverage(), 0.001);
assertEquals(5, avg.getCount());
}

public void testSameValues() {
Average avg = new Average();

avg.addValue(1);
avg.addValue(1);
avg.addValue(1);
avg.addValue(1);
avg.addValue(1);

assertEquals(1.0, avg.getAverage(), 0.001);
assertEquals(5, avg.getCount());
}
}
42 changes: 42 additions & 0 deletions src/LabAnswers.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#####Question 1:
Might use the wrong type or leave the type empty. Might try to store the wrong type in an array. Might forget that indices are offset by 1.

#####Question 2:

**New instance of a class:**

scores = [0,0,0,0,0,0,0,0,0,0]

names = null

**Instance with a single mutation:**

scores = [0,0,0,0,0,1,0,0,0,0]

names = ["Harry Potter"]

**Instance with multiple mutations:**

scores = [1,0,0,0,0,1,0,0,0,0]

names = ["Mike Mulligan","Harry Potter"]

#####Question 3:

**Brand new instance of the class:**

min = 0.0

max = 10.0

**Simplest mutation possible:**

No mutator methods

**What else can we do with the class:**

We can ask it to return its values

**Boundary conditions for the class:**

min and max
51 changes: 51 additions & 0 deletions src/Range.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
public class Range {
private double min = 0.0;
private double max = 0.0;

public Range(double start, double stop) {
if (start > stop) {
min = stop;
max = start;
}
else {
min = start;
max = stop;
}
}

public boolean contains(double value) {
if (value < min || value > max) {
return false;
}
else {
return true;
}
}

public double getWidth() {
return getMax() - getMin();
}

public double getMin() {
return min;
}

public double getMax() {
return max;
}

public Range intersection(Range other) {
double newMin = 0.0;
double newMax = 0.0;

if (other.contains(min)) {
newMin = min;
newMax = other.getMax();
}
else if (other.contains(max)) {
newMin = other.getMin();
newMax = max;
}
return new Range(newMin, newMax);
}
}
24 changes: 24 additions & 0 deletions src/RangeTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import junit.framework.TestCase;

public class RangeTest extends TestCase {
public void testNewRange() {
Range rng = new Range(0.0, 0.0);
}

public void testMin() {
Range rng1 = new Range(1.0, 10.0);
assertEquals(1.0, rng1.getMin());

Range rng2 = new Range(15.0, 10.0);
assertEquals(10.0, rng2.getMin());
}

public void testIntersection() {
Range rng1 = new Range(1.0, 10.0);
Range rng2 = new Range(5.0, 15.0);
Range rng3 = rng1.intersection(rng2);

assertEquals(rng1.getMax(),rng3.getMax());
assertEquals(rng2.getMin(),rng3.getMin());
}
}