|
|
CSCI125Code
DailyTempsTests.java
import java.util.Random;
/**
* Simple test of some of the methods
*/
class DailyTempsTests {
public static void runTests() {
int days = 12; // >= 10
DailyTemps dt = new DailyTemps(days);
// Predefined 24-hour template with wide swings (range = 30+)
final int[] TEMPLATE = {
50, 48, 46, 45, 47, 52, 60, 65,
72, 78, 81, 79, 74, 68, 62, 58,
55, 57, 62, 67, 70, 68, 62, 56
};
// Load deterministic data with variety:
// - Offset changes by day (staggered cold/warm days)
// - Phase shifts the template to vary the daily profile shape
for (int d = 0; d < days; d++) {
int offset = ((d % 6) - 3) * 3; // -9, -6, -3, 0, 3, 6 repeating
int shift = (2 * d) % 24; // phase shift by day
for (int h = 0; h < 24; h++) {
int val = TEMPLATE[(h + shift) % 24] + offset;
dt.set(h, d, val);
}
}
// Compute expected values by scanning the loaded data
double[] expAvg = new double[days];
int[] expHigh = new int[days];
int[] expLow = new int[days];
int[] expRange = new int[days];
for (int d = 0; d < days; d++) {
int min = Integer.MAX_VALUE, max = Integer.MIN_VALUE;
long sum = 0;
for (int h = 0; h < 24; h++) {
int v = dt.get(h, d); // using get() is fine; we are not testing averages here
sum += v;
min = Math.min(min, v);
max = Math.max(max, v);
}
expAvg[d] = sum / 24.0;
expHigh[d] = max;
expLow[d] = min;
expRange[d] = max - min;
}
// Invoke student methods
double[] gotAvg = dt.getAverageTemps();
int[] gotHigh = dt.getHighestDailyTemps();
int[] gotLow = dt.getLowestDailyTemps();
int[] gotRange = dt.getDailyDegreeChange(); // defined as high - low per current spec
// Report comparisons
System.out.println("--- Daily Averages (int division) ---");
compareArrays(gotAvg, expAvg, "avg");
System.out.println("--- Daily Highs ---");
compareArrays(gotHigh, expHigh, "high");
System.out.println("--- Daily Lows ---");
compareArrays(gotLow, expLow, "low");
System.out.println("--- Daily Range (high - low) ---");
compareArrays(gotRange, expRange, "range");
}
private static void compareArrays(double[] got, double[] expected, String label) {
if (got == null || got.length != expected.length) {
System.out.println(label + ": wrong length/null (got="
+ (got == null ? "null" : got.length) + ", expected=" + expected.length + ")");
return;
}
boolean ok = true;
for (int i = 0; i < got.length; i++) {
if (got[i] != expected[i]) {
System.out.println(label + " mismatch at day " + i +
": got " + got[i] + ", expected " + expected[i]);
ok = false;
}
}
if (ok) {
System.out.print(label + ": OK [");
for (int i = 0; i < got.length; i++) {
if (i > 0) System.out.print(", ");
System.out.print(got[i]);
}
System.out.println("]");
}
}
private static void compareArrays(int[] got, int[] expected, String label) {
if (got == null || got.length != expected.length) {
System.out.println(label + ": wrong length/null (got="
+ (got == null ? "null" : got.length) + ", expected=" + expected.length + ")");
return;
}
boolean ok = true;
for (int i = 0; i < got.length; i++) {
if (got[i] != expected[i]) {
System.out.println(label + " mismatch at day " + i +
": got " + got[i] + ", expected " + expected[i]);
ok = false;
}
}
if (ok) {
System.out.print(label + ": OK [");
for (int i = 0; i < got.length; i++) {
if (i > 0) System.out.print(", ");
System.out.print(got[i]);
}
System.out.println("]");
}
}
}
|