Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Initial commit for Testing Lab #16

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
8 changes: 8 additions & 0 deletions Answers.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Testing Lab Answers

**Question 1:** For the array that would be used in ```Scoreboard```, three possible mistakes that someone could make are returning a name's position even though the array is empty, returning a number of high scores outside of the index, and adding a value to the array with the wrong type.

**Question 2:** ```Scoreboard``` is created for the first time and it's empty. A name and a score are added to the array. To mutate the array multiple times, names and scores are added or removed.

**Question 3:**
A brand new instance of the ```Range``` class would contain only the values of start and stop that were passed to it. Nothing is mutated in the class. The class can also return a boolean for if a value is within the range, return the width, min, or max, and return the intersection of two ranges. The boundary conditions are min and max.
23 changes: 23 additions & 0 deletions src/Average.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
public class Average {

double sum = 0;
int count = 0;

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

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

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

avg.addValue(2);
avg.addValue(8);
avg.addValue(7);
avg.addValue(15);
avg.addValue(6);

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

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

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

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

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

avg.addValue(4);
avg.addValue(4);
avg.addValue(4);
avg.addValue(4);
avg.addValue(4);

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

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

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

public boolean contains(double value) {
return value >= min && value < max;
}

public double getWidth() {
if (min < max) {
return max - min;
}
else if (min > max) {
return min - max;
}
else {
return 0;
}
}

public double getMin() {
if(min < max) {
return min;
}
else {
return max;
}
}

public double getMax() {
if(min > max) {
return min;
}
else {
return max;
}
}

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

if (min > other.getMin()) {
newMin = min;
}
else {
newMin = other.getMin();
}

if (max < other.getMax()) {
newMax = max;
}
else {
newMax = other.getMax();
}

return new Range(newMin, newMax);
}
}
33 changes: 33 additions & 0 deletions src/RangeTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
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 range = new Range(5.0, 7.0);

assertTrue(range.contains(6.5));
assertEquals(2.0, range.getWidth());
assertEquals(5.0, range.getMin());
assertEquals(7.0, range.getMax());
}

public void testInstersection() {
Range range = new Range(3.0, 10.0);
Range other = new Range(5.0, 11.0);
Range inter = range.intersection(other); // 5.0, 10.0
assertEquals(5.0, inter.getWidth());
assertEquals(5.0, inter.getMin());
assertEquals(10.0, inter.getMax());
}
}
6 changes: 6 additions & 0 deletions src/Scoreboard.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
public class Scoreboard {
public static void main(String[] args) {
int[] scores = new int[100];
System.out.println(scores[0]);
}
}