Lab 3

In this lab we re-created Lab 2 but using inheritance.

The first class created was named it Sensor.

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.

 

Then we created another class 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. These methods must be implemented by calling the super class methods appropriately.

Then we created 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.

 

Finally we ceated 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.

 

Once the use selects the season and the type of humidity your program should ask the user how many simulations to generate. Each simulation will call the corresponding method from the TemperatureSensor and HumiditySensor. Each iteration should display the readings being generated for each temperature and humidity. At the end your program should display a summary of the simulation as follows:

<pre>

public class Sensor {
private int reading;

public int getReading() {
return this.reading;
}

public void setReading() {
this.reading = (int) (100 * Math.random());
}

public void setReading(int u) {
this.reading = (int) (u * Math.random());
}

public void setReading(int l, int u) {
this.reading = (int) (u * Math.random() + l);
}
}

 

</pre>

<pre>

public class HumiditySensor extends Sensor {
public int getHumidity() {
return super.getReading();
}

public void setHumidity() {
super.setReading();
}

public int getLowHumidity() {
return super.getReading();
}

public void setLowHumidity(int u) {
super.setReading(u);
}

public int getHighHumidity() {
return super.getReading();
}

public void setHighHumidity(int l, int u) {
super.setReading(l, u);
}
}

</pre>

 

<pre>

//Main sensor application that generates random Temperature and Humidity and outputs Statistics.
import java.io.IOException;
import java.util.InputMismatchException;
import java.util.Scanner;

public class SensorStartsApp
{
public static void main(String args[])
{
String season = “0”;
String humidity = “0”;
// initialized variables used for Temperature are all integers
int seasonMenu = 0;
int firstTemp = 0;
int lastTemp = 0;
int lowTemp = 0;
int highTemp = 0;
int sumTemp = 0;
int avgTemp = 0;
// initialized variables used for Humidity are all integers
int humidityMenu = 0;
int firstHum = 0;
int lastHum = 0;
int lowHum = 0;
int highHum = 0;
int sumHum = 0;
int avgHum = 0;
// initialized variables used for Simulation and checking whether the correct type is being used.
int iteration = 0;
Scanner input = new Scanner(System.in);
boolean tempDone = false;
boolean humDone = false;
boolean simDone = false;
boolean runOnce = true;
boolean runSum = true;
int variable = 0;
// Main while loops that runs the application unless 6 from the season
// menu is pressed and 5 from the humidity menu
while (seasonMenu != 6 && humidityMenu != 5)
{
// If conditions that check if the errors in each menu (humidity and
// temperature) is done
if (humDone != false || runOnce == true || simDone == false || seasonMenu == 0)
{
if (simDone != false || runOnce == true || seasonMenu == 0)
{
if (simDone == false || seasonMenu == 0)
{
tempDone = false;
// try and catch used to make sure user inputs a integer
// from 1 to 6 for season menu
try {
variable = 1;
System.out.print(“What season would you like to simulate:”);
System.out.print(” \n (1) Winter \n (2) Spring \n (3) Summer \n (4) Fall \n (5) random \n (6) exit “);
seasonMenu = input.nextInt();
if (seasonMenu > 6 || seasonMenu < 1)
{
seasonMenu=0;
variable = 0;
throw new IOException(“ERROR!!!\nThe number you have chosen is either too high or too low.\nYour integer options are 1, 2, 3, 4, 5, and 6. “);
}
tempDone = true;
}
catch (InputMismatchException e)
{
seasonMenu=0;
variable = 0;
System.out.println(“ERROR!!!\nThe number you have chosen is not an integer.\nYour integer options are 1, 2, 3, 4, 5, and 6.”);
input.next();
}
catch (IOException exc)
{
variable = 0;
System.out.println(exc.getMessage());
}
}
}
// Exit the menu if choice is 6
if (seasonMenu == 6)
break;
if (seasonMenu != 6 && tempDone == true && simDone == false || (variable == 1 && variable != 0))
{
if (iteration != -20)
{
// reset sum and average temperature
sumTemp = 0;
avgTemp = 0;
simDone = false;
try
{
System.out.print(“How many tempature simulation would you like to do? “);
iteration = input.nextInt();
simDone = true;
}
catch (InputMismatchException e)
{
System.out.println(“ERROR!!!\nThe number you have chosen is not an integer.”);
input.next();
}

if (simDone != false)
{
for (int i = 1; i <= iteration; i++)
{
TemperatureSensor temp = new TemperatureSensor();
// if season menu = 5 then sends a random
// options from 1 to 4
if (seasonMenu == 5)
{
season=”Random”;
seasonMenu = (int) (4 * Math.random() + 1);
}
// if season menu = 1 then simulate winter
if (seasonMenu == 1)
{
temp.setWintertemp(20, 20);
System.out.println(“Simulated Temperature#”+ i + “: “+ (temp.getWintertemp()));
if (i == 1)
{
if (season.equals(“Random”))
season=”Random:Winter”;
else
season=”Winter”;
highTemp = temp.getWintertemp();
lowTemp = temp.getWintertemp();
firstTemp = temp.getWintertemp();
}
if (i == iteration)
lastTemp = temp.getWintertemp();
if (temp.getWintertemp() > highTemp)
highTemp = temp.getWintertemp();
if (temp.getWintertemp() < lowTemp)
lowTemp = temp.getWintertemp();
sumTemp = sumTemp + temp.getWintertemp();
}
// if season menu = 2 then simulate spring
if (seasonMenu == 2)
{
temp.setSpringtemp(40, 30);
System.out.println(“Simulated Temperature#”+ i + “: ” + temp.getSpringtemp());
if (i == 1)
{
if (season.equals(“Random”))
season=”Random:Spring”;
else
season=”Spring”;
highTemp = temp.getSpringtemp();
lowTemp = temp.getSpringtemp();
firstTemp = temp.getSpringtemp();
}
if (i == iteration)
lastTemp = temp.getSpringtemp();
if (temp.getSpringtemp() > highTemp)
highTemp = temp.getSpringtemp();
if (temp.getSpringtemp() < lowTemp)
lowTemp = temp.getSpringtemp();
sumTemp = sumTemp + temp.getSpringtemp();
}
// if season menu = 3 then simulate Summer
if (seasonMenu == 3)
{
temp.setSummertemp(70, 20);
System.out.println(“Simulated Temperature#”+ i + “: “+ (temp.getSummertemp()));
if (i == 1)
{
if (season.equals(“Random”))
season=”Random:Summer”;
else
season=”Summer”;
highTemp = temp.getSummertemp();
lowTemp = temp.getSummertemp();
firstTemp = temp.getSummertemp();
}
if (i == iteration)
lastTemp = temp.getSummertemp();
if (temp.getSummertemp() > highTemp)
highTemp = temp.getSummertemp();
if (temp.getSummertemp() < lowTemp)
lowTemp = temp.getSummertemp();
sumTemp = sumTemp + temp.getSummertemp();
}
// if season menu = 4 then simulate fall
if (seasonMenu == 4)
{
temp.setFalltemp(40, 20);
System.out.println(“Simulated Temperature#”
+ i + “: ” + temp.getFalltemp());
if (i == 1)
{
if (season.equals(“Random”))
season=”Random:Fall”;
else
season=”Fall”;
highTemp = temp.getFalltemp();
lowTemp = temp.getFalltemp();
firstTemp = temp.getFalltemp();
}
if (i == iteration)
lastTemp = temp.getFalltemp();
if (temp.getFalltemp() > highTemp)
highTemp = temp.getFalltemp();
if (temp.getFalltemp() < lowTemp)
lowTemp = temp.getFalltemp();
sumTemp = sumTemp + temp.getFalltemp();
}
}
// Output results from the temperature menu
avgTemp = sumTemp / iteration;
System.out.println(“\nSeason: ” + season);
System.out.println(“First tempature generated: “+ firstTemp);
System.out.println(“Last tempature generated: “+ lastTemp);
System.out.println(“Lowest tempature generated: “+ lowTemp);
System.out.println(“Highest tempature generated: “+ highTemp);
System.out.println(“Total sum of all temperatures generated: “+ sumTemp);
System.out.println(“Average for the temperatures: “+ avgTemp);
}
}
}
}
if (tempDone != false && simDone != false)
{
if (simDone == false || runSum == true || iteration > -10)
{
humDone = false;
runSum = false;
try
{
System.out.print(“What humidity would you like to simulate \n (1) Full Range \n (2) low \n (3) high \n(4) random \n(5) exit: “);
humidityMenu = input.nextInt();
if (humidityMenu > 5 || humidityMenu < 1)
throw new IOException(“ERROR!!!\nThe number you have chosen is either too high or too low.\nYour integer options are 1, 2, 3, 4, and 5. “);
humDone = true;
}
catch (InputMismatchException e)
{
System.out.println(“ERROR!!!\nThe number you have chosen is not an integer.\nYour integer options are 1, 2, 3, 4, and 5.”);
input.next();
}
catch (IOException exc)
{
System.out.println(exc.getMessage());
}
}
if (humidityMenu != 5 && humDone == true)
{
sumHum = 0;
avgHum = 0;
try
{
System.out.print(“How many humidity simulation would you like to do? “);
iteration = input.nextInt();
simDone = true;
}
catch (InputMismatchException e)
{
iteration = -20;
System.out.println(“ERROR!!!\nThe number you have chosen is not an integer.”);
input.next();
}
if (simDone == false || iteration > -10)
{
for (int i = 1; i <= iteration; i++)
{
HumiditySensor hum = new HumiditySensor();
if (humidityMenu == 4)
{
humidity = “Random”;
humidityMenu = (int) (3 * Math.random() + 1);
}
if (humidityMenu == 1)
{
hum.setHumidity();
System.out.println(“Simulated Humidity#” + i+ “: ” + (hum.getHumidity()));
if (i == 1)
{
if (humidity.equals(“Random”))
humidity=”Random:Full”;
else
humidity=”Full”;
highHum = hum.getHumidity();
lowHum = hum.getHumidity();
firstHum = hum.getHumidity();
}
if (i == iteration)
lastHum = hum.getHumidity();
if (hum.getHumidity() > highHum)
highHum = hum.getHumidity();
if (hum.getHumidity() < lowHum)
lowHum = hum.getHumidity();
sumHum = sumHum + hum.getHumidity();
}
if (humidityMenu == 2)
{
hum.setLowHumidity(50);
System.out.println(“Simulated Humidity#” + i+ “: ” + hum.getLowHumidity());
if (i == 1)
{
if (humidity.equals(“Random”))
humidity=”Random:High”;
else
humidity=”Low”;
highHum = hum.getLowHumidity();
lowHum = hum.getLowHumidity();
firstHum = hum.getLowHumidity();
}
if (i == iteration)
lastHum = hum.getLowHumidity();
if (hum.getLowHumidity() > highHum)
highHum = hum.getLowHumidity();
if (hum.getLowHumidity() < lowHum)
lowHum = hum.getLowHumidity();
sumHum = sumHum + hum.getLowHumidity();
}
if (humidityMenu == 3)
{
hum.setHighHumidity(50, 50);
System.out.println(“Simulated Humidity#” + i+ “: ” + (hum.getHighHumidity()));
if (i == 1)
{
if (humidity.equals(“Random”))
humidity=”Random:High”;
else
humidity=”High”;
highHum = hum.getHighHumidity();
lowHum = hum.getHighHumidity();
firstHum = hum.getHighHumidity();
}
if (i == iteration)
lastHum = hum.getHighHumidity();
if (hum.getHighHumidity() > highHum)
highHum = hum.getHighHumidity();
if (hum.getHighHumidity() < lowHum)
lowHum = hum.getHighHumidity();
sumHum = sumHum + hum.getHighHumidity();
}
}
avgHum = sumHum / iteration;
System.out.println(“\nHumidity Type: ” + humidity);
System.out.println(“First humidity generated: “+ firstHum);
System.out.println(“Last humidity generated: “+ lastHum);
System.out.println(“Lowest humidity generated: “+ lowHum);
System.out.println(“Highest humidity generated: “+ highHum);
System.out.println(“Total sum of all humidities generated: “+ sumHum);
System.out.println(“Average for the humidities: “+ avgHum);
seasonMenu = 0;
}
}
}
runOnce = false;
}
}
}

</pre>

<pre>

public class TemperatureSensor extends Sensor {
public int getWintertemp() {
return super.getReading();
}

public void setWintertemp(int l, int u) {
super.setReading(l, u);
}

public int getSummertemp() {
return super.getReading();
}

public void setSummertemp(int l, int u) {
super.setReading(l, u);
}

public int getSpringtemp() {
return super.getReading();
}

public void setSpringtemp(int l, int u) {
super.setReading(l, u);
}

public int getFalltemp() {
return super.getReading();
}

public void setFalltemp(int l, int u) {
super.setReading(l, u);
}
}

</pre>

Leave a Reply

Your email address will not be published. Required fields are marked *