import java.util.Random;
/**
* Temps2D - exercise with 24 hourly temperature rows by N days (columns).
* All methods are instance (non-static). Use Random to populate and analyze.
*
* Rows = hours [0..23]
* Cols = days [0..days-1]
*/
public class DailyTemps {
private final int days; // number of days (columns)
private final int[][] temps; // temps[hour][day]
private final Random rand = new Random();
/**
* Construct a temps table with 24 rows (hours) and the given number of days.
*/
public DailyTemps(int days) {
this.days = days;
this.temps = new int[24][days];
}
/**
* Randomly populate the table.
* Rule: pick an initial random temperature in [20,70]. Then go hour-by-hour,
* day-by-day, choosing the next temp within +/-5 degrees of the previous temp.
*
* Traversal order: day 0..days-1, hour 0..23.
* - For the very first cell (hour=0, day=0), choose any random in [20,70].
* - Otherwise, choose a delta in [-5,5] and add to the previous cell's value.
*/
public void randomPopulate() {
int prev = randBetween(20, 70); // start with a moderate temp
for (int d = 0; d < days; d++) {
for (int h = 0; h < 24; h++) {
int next = prev + randBetween(-5, 5);
temps[h][d] = next;
prev = next;
}
}
}
/**
* Pretty-print a single day's temps on one line.
*/
public void printDay(int day) {
// TODO: print temps for the given day across all 24 hours.
}
/**
* Print the table in a readable grid.
* Example (2 days):
* Hour 0: 71 73
* Hour 1: 70 74
* ...
*/
public void printTemps() {
// TODO Implement me!
}
/**
* Return an array of the average temperature for each day.
*/
public double[] getAverageTemps() {
// TODO Implement me!
return null; // So it compiles.
}
/**
* Return an array with the highest temperature for each day.
*/
public int[] getHighestDailyTemps() {
// TODO Implement me!
return null; // So it compiles.
}
/**
* Return an array with the lowest temperature for each day.
*/
public int[] getLowestDailyTemps() {
// TODO Implement me!
return null; // So it compiles.
}
/**
* Return an array with the difference between the low and high temperature for each day.
*/
public int[] getDailyDegreeChange() {
// TODO Implement me!
return null; // So it compiles.
}
/**
* Convenience: get the temperature at a specific hour/day.
* If an invalid day/time is specified, return -100.
*/
public int get(int hour, int day) {
if (hour < 0 || hour >= 24)
return -100;
if (day < 0 || day >= days)
return -100;
return temps[hour][day];
}
public void set(int hour,int day, int temp) {
if(hour>=0 && hour<24 && day>=0 && day<days) {
temps[hour][day]=temp;
}
}
private int randBetween(int lo, int hi) {
// inclusive bounds
return lo + rand.nextInt(hi - lo + 1);
}
}