Skip to content

Instantly share code, notes, and snippets.

@jasonnicholson
Last active August 29, 2015 13:58
Show Gist options
  • Save jasonnicholson/10015027 to your computer and use it in GitHub Desktop.
Save jasonnicholson/10015027 to your computer and use it in GitHub Desktop.
Exam 2 Review Proposed Student Solutions (Problem 6)
package exam2Review;
public class ArrayProcessor
{
// Processes values in an array using the given Processor
public static double runProcessor(double[] arr, Processor p)
{
for (int i = 0; i < arr.length; ++i)
{
p.process(arr[i]);
}
return p.getResult();
}
}
package exam2Review;
public class MaxProcessor implements Processor
{
private double maxValue;
public MaxProcessor(){
maxValue = Double.NEGATIVE_INFINITY;
}
public void process(double value){
maxValue = Math.max(maxValue, value);
}
public double getResult(){
return maxValue;
}
}
package exam2Review;
public class Problem6Tester {
public static void main(String[] args) {
// Data
double[] arrayOfDoubles = {83.82072588, 79.06909553, -19.32759285, 62.01153403, -20.52662657, 57.89045724, 94.66377192, 80.0163969, 30.87834387, 49.25945775, 24.19614326, -24.64202999, 13.73394567, 57.79859793, 47.56516579, 62.50330916, 96.37163676, 81.23606573, -25.45816438, -6.283612409, 57.30437988, 21.64743784, 55.48922371, 95.63340087, -12.74511854, 44.5431559, 7.172229891};
// check maxProcesser
double actualMax = 96.3716367622209;
Processor maxFinder = new MaxProcessor();
double max = ArrayProcessor.runProcessor(arrayOfDoubles, maxFinder);
if (Math.abs(actualMax-max) < 1e-8){
System.out.println("The correct max was found, " + max);
}
else {
System.out.println("failed to find correct max, " + actualMax + ". Instead this is what was found: " + max + "\n");
}
// Check range counter
double minOfRange = -20.0;
double maxOfRange = 32.0;
double actualCountInRange = 8;
Processor rangeFinder = new RangeCounter(minOfRange, maxOfRange);
double countInRange = ArrayProcessor.runProcessor(arrayOfDoubles, rangeFinder);
if (Math.abs(countInRange - actualCountInRange) < 1e-8)
{
System.out.println("The correct count within the range of ("
+ minOfRange + ", " + maxOfRange + ") was found. Count = "
+ (int)actualCountInRange);
}
else {
System.out.println("The correct count was NOT found in range of ("
+ minOfRange + ", " + maxOfRange +
"). correct count = " + (int)actualCountInRange);
}
}
}
/**
*
*/
package exam2Review;
/**
* @author Jason Nicholson
*
*/
public interface Processor {
public void process(double value);
public double getResult();
}
package exam2Review;
public class RangeCounter implements Processor{
double minRange;
double maxRange;
int count;
public RangeCounter(double minRange, double maxRange){
this.minRange = minRange;
this.maxRange = maxRange;
this.count = 0;
}
public void process(double value) {
// I am considering the bounds as noninclusive.
if (value > this.minRange && value < this.maxRange)
{
count++;
}
}
public double getResult(){
// cast the count to a double and return
return (double)count;
}
}
@jasonnicholson
Copy link
Author

Problem6Tester.java is the file that you should run once you have added all these files to your project.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment