MODIFYING AND TESTING A BUILDING ENVIRONMENT MONITOR PROBLEM (GOAL) STATEMENT :
ID: 3686174 • Letter: M
Question
MODIFYING AND TESTING A BUILDING ENVIRONMENT MONITOR
PROBLEM (GOAL) STATEMENT: A program that stores temperature, humidity, and light levels for a building of 3 floors with 25 rooms per floor. The program reports the average values of every floor. The program fills every room with test data that is randomly generated (temperatures between 66 and 82, humidity between 35% and 50%, light levels between 400 and 600 lux).
You have been provided with a program that can store temperature and humidity data for a 5floor office building. (In two files: Building.java and Room.java) It already calculates the average values for temperature and humidity on each floor, and it already generates a report for those things.
Building.java
public class Building {
// Declare the two-dimensional room array
// a static variable belongs to the class
private static Room rooms[][];
// This is the main method, where the program runs
public static void main(String[] args) {
initializeRooms();
printReport();
}
/**
* A method for initializing the room array and creating each individual room
*/
private static void initializeRooms() {
// Initialize the room array so that the building has 5 floors
// and 25 rooms in each floor
rooms = new Room[5][25];
// Initialize all the rooms in all the floors.
for (int i= 0; i<rooms.length; i++)
for(int j=0; j<rooms[i].length; j++) {
rooms[i][j] = new Room();
}
}
/**
* This method prints a report of the average values to the console.
*/
private static void printReport() {
System.out.println("Average Values Report");
System.out.println("---------------------");
// local variables to store the sums and averages
double sum_temp, avg_temp;
double sum_humid, avg_humid;
// loop over all the floors
for(int i=0; i<rooms.length; i++){
System.out.print("Floor " + (i+1) + " -- ");
// Initialize the sums and averages every time we get to a new floor
sum_temp = 0;
sum_humid = 0;
for(Room the_room : rooms[i]) {
sum_temp = sum_temp + the_room.getTemp();
sum_humid = sum_humid + the_room.getHumidity();
}
avg_temp = sum_temp / rooms[i].length;
avg_humid = sum_humid / rooms[i].length;
System.out.printf("Average Temperature: %6.2f; ", avg_temp);
System.out.printf("Average Humidity: %2.0f%%.%n", avg_humid);
}
}
}
Room.java
public class Room {
// declare and initialize the temp and humidity values to ridiculous values
// to make it easier to notice they have not been set to sensor readings or to test data
double temp = 800;
double humidity = 900;
public Room() {
}
/**
* @return the temp
*/
public double getTemp() {
return temp;
}
/**
* @param temp the temp to set
*/
public void setTemp(double temp) {
this.temp = temp;
}
/**
* @return the humidity
*/
public double getHumidity() {
return humidity;
}
/**
* @param humidity the humidity to set
*/
public void setHumidity(double humidity) {
this.humidity = humidity;
}
}
You will modify the program so that it also stores the light level for each room (in lux).
Also, we don’t have access to the sensors in the rooms yet, so you’ll add a method that can put test values in all the Room objects in the building, so that we get a report that shows average temperature, humidity, and light levels for each floor. Name the method fillWithTestData(). Add it to the Building class. Call that method from the Building class’ main method, just before the report is printed.
Generate the random values using the Random class (as described in section 4.13 of your book). For the ranges of each type of value, refer to the problem statement. Each room should have its own randomly generated values.
• Make a new project for the Building and Room classes • Modify the Room object so that it now can store light level information • Add a fillWithTestData() method to Building – this is where you generate your test data and add it to the Rooms. • Call the new method from your main, just before the spot in the program that generates the report.
Building.java
public class Building {
// Declare the two-dimensional room array
// a static variable belongs to the class
private static Room rooms[][];
// This is the main method, where the program runs
public static void main(String[] args) {
initializeRooms();
printReport();
}
/**
* A method for initializing the room array and creating each individual room
*/
private static void initializeRooms() {
// Initialize the room array so that the building has 5 floors
// and 25 rooms in each floor
rooms = new Room[5][25];
// Initialize all the rooms in all the floors.
for (int i= 0; i<rooms.length; i++)
for(int j=0; j<rooms[i].length; j++) {
rooms[i][j] = new Room();
}
}
/**
* This method prints a report of the average values to the console.
*/
private static void printReport() {
System.out.println("Average Values Report");
System.out.println("---------------------");
// local variables to store the sums and averages
double sum_temp, avg_temp;
double sum_humid, avg_humid;
// loop over all the floors
for(int i=0; i<rooms.length; i++){
System.out.print("Floor " + (i+1) + " -- ");
// Initialize the sums and averages every time we get to a new floor
sum_temp = 0;
sum_humid = 0;
for(Room the_room : rooms[i]) {
sum_temp = sum_temp + the_room.getTemp();
sum_humid = sum_humid + the_room.getHumidity();
}
avg_temp = sum_temp / rooms[i].length;
avg_humid = sum_humid / rooms[i].length;
System.out.printf("Average Temperature: %6.2f; ", avg_temp);
System.out.printf("Average Humidity: %2.0f%%.%n", avg_humid);
}
}
}
Explanation / Answer
Building.java
package org.students;
import java.util.Random;
public class Building {
// Declare the two-dimensional room array
// a static variable belongs to the class
private static Room rooms[][];
static int tmin=66;
static int tmax=82;
static int hmin=35;
static int hmax=50;
static int lmin=400;
static int lmax=600;
// This is the main method, where the program runs
public static void main(String[] args) {
initializeRooms();
fillWithTestData();
printReport();
}
/**
* A method for filling the test data for individual room
*/
private static void fillWithTestData()
{
for(int i=0;i<3;i++)
{
for(int j=0;j<25;j++)
{
Random rand = new Random();
int randomNum1 = tmin + rand.nextInt((tmax - tmin) + 1);
int randomNum2 = hmin + rand.nextInt((hmax - hmin) + 1);
int randomNum3 = lmin + rand.nextInt((lmax - lmin) + 1);
rooms[i][j].temp=randomNum1;
rooms[i][j].humidity=randomNum2;
rooms[i][j].light=randomNum3;
}
}
}
/**
* A method for initializing the room array and creating each individual room
*/
private static void initializeRooms() {
// Initialize the room array so that the building has 5 floors
// and 25 rooms in each floor
rooms = new Room[3][25];
// Initialize all the rooms in all the floors.
for (int i= 0; i<rooms.length; i++)
for(int j=0; j<rooms[i].length; j++) {
rooms[i][j] = new Room();
}
}
/**
* This method prints a report of the average values to the console.
*/
private static void printReport() {
System.out.println("Average Values Report");
System.out.println("---------------------");
// local variables to store the sums and averages
double sum_temp, avg_temp;
double sum_humid, avg_humid;
double sum_light, avg_light;
// loop over all the floors
for(int i=0; i<rooms.length; i++){
System.out.print("Floor " + (i+1) + " -- ");
// Initialize the sums and averages every time we get to a new floor
sum_temp = 0;
sum_humid = 0;
sum_light=0;
for(Room the_room : rooms[i]) {
sum_temp = sum_temp + the_room.getTemp();
sum_humid = sum_humid + the_room.getHumidity();
sum_light=sum_light+the_room.getLight();
}
avg_temp = sum_temp / rooms[i].length;
avg_humid = sum_humid / rooms[i].length;
avg_light = sum_light / rooms[i].length;
System.out.printf("Average Temperature: %6.2f; ", avg_temp);
System.out.printf("Average Humidity: %2.0f%%.%n", avg_humid);
System.out.printf("Average Light:%6.2fn lux",avg_light);
System.out.println(" ");
}
}
}
---------------------------------------------------------------------------------------------------------------------------------------------------------------------
Room.java
package org.students;
public class Room {
// declare and initialize the temp and humidity values to ridiculous values
// to make it easier to notice they have not been set to sensor readings or to test data
double temp = 0;
double humidity =0;
double light=0;
public double getLight() {
return light;
}
public void setLight(double light) {
this.light = light;
}
public Room(double temp, double humidity, double light) {
super();
this.temp = temp;
this.humidity = humidity;
this.light = light;
}
public Room() {
}
/**
* @return the temp
*/
public double getTemp() {
return temp;
}
/**
* @param temp the temp to set
*/
public void setTemp(double temp) {
this.temp = temp;
}
/**
* @return the humidity
*/
public double getHumidity() {
return humidity;
}
/**
* @param humidity the humidity to set
*/
public void setHumidity(double humidity) {
this.humidity = humidity;
}
}
_____________________________________________________________________________________________
output:
Average Values Report
---------------------
Floor 1 -- Average Temperature: 74.36; Average Humidity: 43%.
Average Light:472.60n lux
Floor 2 -- Average Temperature: 73.44; Average Humidity: 43%.
Average Light:506.44n lux
Floor 3 -- Average Temperature: 73.52; Average Humidity: 42%.
Average Light:497.48n lux
______________________________________________________________________________________________
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.