Description:
First a Sensor class is created. This is going to be the super class from which we will inherit methods and variables.
The Sensor class will contain at least an int field reading and the following methods: setReading, getReading. Method setReading() will generate a reading value between 0 and 100. Overloaded method setReading( int u) will generate a reading value between 0 and u with 0 <= u <= 100. Overloaded method setReading( int l, int u) will generate a reading value between l and u with 0 <= l <= u <= 100. Method getReading() will return the last reading generated.
Next a TemperatureSensor class will be created. TemperatureSensor that will inherit class Sensor and will implement the following methods: getWinterTemp, getSpringTemp, getSummerTemp, getFallTemp. Method getWinterTemp should return a number between 20-40, getSpringTemp between 40-70, getSummerTemp between 70-90, and getFallTemp between 40-60.
Another class HumiditySensor that will inherit class Sensor and will implement the following methods: getHumidity, getLowHumidity, getHighHumidity. Method getHumidity should return a number between 0-100, getLowHumidity between 0-50, getHighHumidity between 50-100. These methods must be implemented by calling the super class methods appropriately.
Another class SensorsStatsApp that will implement the application. In the application you will display a menu that will first ask the user what season to simulate (1) winter (2) spring (3) summer (4) fall (5) random (6) exit. Random will choose the season for the user. After that, another menu will ask the user what type of humidity to simulate (1) full range (2) low (3) high (4) random (5) exit. Random will choose the type of humidity for the user.
Code:
Sensor.java
//Ryan Lawrence //CET 3640 //Super class package Lab3; import java.util.Random; public class Sensor { private int reading; Random r = new Random(); //get method for private variable public int getReading(){ return reading; } //Set methods for private variable public void setReading(){ reading = 0 + r.nextInt(100); } public void setReading(int u){ reading = 0 + r.nextInt(u); } public void setReading(int l, int u){ reading = l + r.nextInt(u); } }
TemperatureSensor.java
//Ryan Lawrence //CET 3640 //Sub class package Lab3; public class TemperatureSensor extends Sensor { //get methods public int getWinterTemp(){ super.setReading(20, 20); int reading = super.getReading(); return reading; } public int getSpringTemp(){ super.setReading(40, 30); int reading = super.getReading(); return reading; } public int getSummerTemp(){ super.setReading(70, 20); int reading = super.getReading(); return reading; } public int getFallTemp(){ super.setReading(40, 20); int reading = super.getReading(); return reading; } }
HumiditySensor.java
//Ryan Lawrence //CET 3640 //Sub class package Lab3; public class HumiditySensor extends Sensor { //Full Range Method public int getHumidity(){ super.setReading(); int humidity = super.getReading(); return humidity; } //Low Range Method public int getLowHumidity(){ super.setReading(50); int humidity = super.getReading(); return humidity; } //High Range Method public int getHighHumidity(){ super.setReading(50, 50); int humidity = super.getReading(); return humidity; } }
SensorStatsApp.java
package Lab3; import java.util.InputMismatchException; import java.util.Scanner; import java.util.Random; public class SensorStatsApp { public static void main(String[] args) { //Create Temperature Sensor Object TemperatureSensor tempSensor = new TemperatureSensor(); //Create Humidity Sensor Object HumiditySensor humSensor = new HumiditySensor(); //Scanner objects Scanner input = new Scanner(System.in); Scanner loopInput = new Scanner(System.in); //Variables int firstTemp = 0, lastTemp = 0, lowTemp=999999, highTemp=0, sumTemp =0, TempValue,avgTemp=0, numOfSim; int firstHum = 0, lastHum = 0, lowHum=999999, highHum=0, sumHum =0, humValue,avgHum=0; String season = ""; String humidityRange=""; String loopControl = "yes"; Boolean sRandom = false; Boolean hRandom = false; int sChoice,hChoice; //Random Object Random r = new Random(); while(loopControl.equals("yes")){ //Try/Catch blocks used to prevent input mismatch try{ System.out.println("Select Season"); System.out.println("1 - Winter\n2 - Spring\n3 - Summer\n4 - Fall\n5 - Random\n6 - Exit"); System.out.println("Choice: "); sChoice = input.nextInt(); } //If the user enters anything that is not an integer an error is displayed and the program restarts catch (InputMismatchException inputMismatchException){ System.out.printf("\nException: %s\n", inputMismatchException); input.nextLine(); System.out.println("Must enter an Integer. Restarting Program\n"); continue; } //If statement is used to ensure the user enters a value from the choices given //If they do not the program restarts if(sChoice==0 || sChoice > 6){ System.out.println("Invalid Choice. Restarting Program\n"); continue; } if(sChoice == 5){ sRandom = true; sChoice = 1 + r.nextInt(4); } if(sChoice == 6){ System.out.println("Exiting"); break; } try{ System.out.println("Select Humidity"); System.out.println("1 - Full Range\n2 - Low\n3 - High\n4 - Random\n5 - Exit"); System.out.println("Choice: "); hChoice = input.nextInt(); } catch (InputMismatchException inputMismatchException){ System.out.printf("\nException: %s\n", inputMismatchException); input.nextLine(); System.out.println("Must enter an Integer. Restarting Program\n"); continue; } if(hChoice == 5){ System.out.println("Exiting"); break; } //CHecks inputs are within an allowed range if(hChoice==0 || hChoice > 5){ System.out.println("Invalid Choice. Restarting Program\n"); continue; } if(hChoice == 4){ hRandom = true; hChoice= 1+r.nextInt(3); } try{ System.out.println("Enter Number of Simulations to Run: "); numOfSim = input.nextInt(); } //If the user enters anything that is not an integer an error is displayed and the program restarts catch(InputMismatchException inputMismatchException2){ System.out.printf("\nException: %s\n", inputMismatchException2); input.nextLine(); System.out.println("Must enter an Integer. Restarting Program\n"); continue; } //Season Switch cases used to calculate the temperatures needed switch(sChoice){ case 1: season = "Winter"; for(int count = 0; count <= numOfSim; count++){ TempValue = tempSensor.getWinterTemp(); if (count == 0){ firstTemp=TempValue; } //checks to see if the current random temperature is lower than the stored lowest. if (TempValue <= lowTemp){ lowTemp = TempValue; } //checks to see if the current random temperature is higher than the stored highest. if (TempValue >= highTemp){ highTemp = TempValue; } //Stores the last value in the iteration if (count == (numOfSim - 1)){ lastTemp = TempValue; } //Adds all the Generated Temperatures sumTemp += TempValue; } //Calculate the average of all the generated temperatures. avgTemp = sumTemp/numOfSim; break; case 2: season = "Springr"; for(int count = 0; count <= numOfSim; count++){ TempValue = tempSensor.getSpringTemp(); if (count == 0){ firstTemp=TempValue; } //checks to see if the current random temperature is lower than the stored lowest. if (TempValue <= lowTemp){ lowTemp = TempValue; } //checks to see if the current random temperature is higher than the stored highest. if (TempValue >= highTemp){ highTemp = TempValue; } //Stores the last value in the iteration if (count == (numOfSim - 1)){ lastTemp = TempValue; } //Adds all the Generated Temperatures sumTemp += TempValue; } //Calculate the average of all the generated temperatures. avgTemp = sumTemp/numOfSim; break; case 3: season = "Summer"; for(int count = 0; count <= numOfSim; count++){ TempValue = tempSensor.getSummerTemp(); if (count == 0){ firstTemp=TempValue; } //checks to see if the current random temperature is lower than the stored lowest. if (TempValue <= lowTemp){ lowTemp = TempValue; } //checks to see if the current random temperature is higher than the stored highest. if (TempValue >= highTemp){ highTemp = TempValue; } //Stores the last value in the iteration if (count == (numOfSim - 1)){ lastTemp = TempValue; } //Adds all the Generated Temperatures sumTemp += TempValue; } //Calculate the average of all the generated temperatures. avgTemp = sumTemp/numOfSim; break; case 4: season = "Fall"; for(int count = 0; count <= numOfSim; count++){ TempValue = tempSensor.getFallTemp(); if (count == 0){ firstTemp=TempValue; } //checks to see if the current random temperature is lower than the stored lowest. if (TempValue <= lowTemp){ lowTemp = TempValue; } //checks to see if the current random temperature is higher than the stored highest. if (TempValue >= highTemp){ highTemp = TempValue; } //Stores the last value in the iteration if (count == (numOfSim - 1)){ lastTemp = TempValue; } //Adds all the Generated Temperatures sumTemp += TempValue; } //Calculate the average of all the generated temperatures. avgTemp = sumTemp/numOfSim; break; }//End of Season Switch //Switch cases used to determine Humidity values switch(hChoice){ case 1: humidityRange = "Full"; for(int hCount = 0; hCount <=numOfSim; hCount++){ humValue = humSensor.getHumidity(); if (hCount == 0){ firstHum=humValue; } //checks to see if the current random temperature is lower than the stored lowest. if (humValue <= lowHum){ lowHum = humValue; } //checks to see if the current random temperature is higher than the stored highest. if (humValue >= highHum){ highHum = humValue; } //Stores the last value in the iteration if (hCount == (numOfSim - 1)){ lastHum = humValue; } //Adds all the Generated Temperatures sumHum += humValue; } //Calculate the average of all the generated temperatures. avgHum = sumHum/numOfSim; break; case 2: humidityRange = "Low"; for(int hCount = 0; hCount <=numOfSim; hCount++){ humValue = humSensor.getLowHumidity(); if (hCount == 0){ firstHum=humValue; } //checks to see if the current random temperature is lower than the stored lowest. if (humValue <= lowHum){ lowHum = humValue; } //checks to see if the current random temperature is higher than the stored highest. if (humValue >= highHum){ highHum = humValue; } //Stores the last value in the iteration if (hCount == (numOfSim - 1)){ lastHum = humValue; } //Adds all the Generated Temperatures sumHum += humValue; } //Calculate the average of all the generated temperatures. avgHum = sumHum/numOfSim; break; case 3: humidityRange = "High"; for(int hCount = 0; hCount <=numOfSim; hCount++){ humValue = humSensor.getHighHumidity(); if (hCount == 0){ firstHum=humValue; } //checks to see if the current random temperature is lower than the stored lowest. if (humValue <= lowHum){ lowHum = humValue; } //checks to see if the current random temperature is higher than the stored highest. if (humValue >= highHum){ highHum = humValue; } //Stores the last value in the iteration if (hCount == (numOfSim - 1)){ lastHum = humValue; } //Adds all the Generated Temperatures sumHum += humValue; } //Calculate the average of all the generated temperatures. avgHum = sumHum/numOfSim; break; }//End of Humidity Switch //Print outs if(sRandom==true){ System.out.println("Season: Random:"+season); } else{ System.out.println("Season: "+season); } System.out.printf("First Temperature: %d\n", firstTemp); System.out.printf("Last Temperature: %d\n", lastTemp); System.out.printf("Lowest Temperature: %d\n", lowTemp); System.out.printf("Highest Temperature: %d\n", highTemp); System.out.printf("Sum of Temperature: %d\n", sumTemp); System.out.printf("Average Temperature: %d\n\n", avgTemp); if (hRandom==true){ System.out.println("Humidity: Random:"+humidityRange); } else{ System.out.println("Humidity: "+humidityRange); } System.out.printf("First Humidity Reading: %d\n", firstTemp); System.out.printf("Last Humidity Reading: %d\n", lastTemp); System.out.printf("Lowest Humidity Reading: %d\n", lowTemp); System.out.printf("Highest Humidity Reading: %d\n", highTemp); System.out.printf("Sum of Humidity Readings: %d\n", sumTemp); System.out.printf("Average Humidity Reading: %d\n\n", avgTemp); //reinitialize variables firstTemp = 0; lastTemp = 0; lowTemp=999999; highTemp=0; sumTemp =0; avgTemp=0; firstHum = 0; lastHum = 0; lowHum=999999; highHum=0; sumHum =0; avgHum=0; sRandom = false; hRandom = false; //Control Loop System.out.println("Would you like to run again?[yes/no]: "); loopControl = loopInput.nextLine(); } } }
Screenshot: