Lab 3 – Inheritance

advertisement
NEW YORK CITY COLLEGE OF TECHNOLOGY
THE CITY UNIVERSITY OF NEW YORK
Department of Computer Engineering Technology
300 Jay Street, Brooklyn, NY 11201-1909
CET3640 – Software for Computer Control
Lab #3
Instructions:
In this lab you will re-create Lab #2 but using inheritance.
First, create a class and name 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.
Create 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.
Create 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.
Create 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:
Season: _________ (if random, then display Random:Summer for example)
123456-
First temperature generated
Last temperature generated
Lowest temperature generated
Highest temperature generated
Total sum of all temperatures generated
Average for the season
Humidity Type: _________ (if random, then display Random:Full for example)
1- First humidity reading generated
2- Last humidity reading generated
3- Lowest humidity reading generated
4- Highest humidity reading generated
5- Total sum of all humidity readings generated
6- Average humidity reading
When asking input from the user you must validate it by checking that only valid options from
the menu are selected (i.e. choosing option 9 is an error as it doesn’t exists) and also that the
number of simulation is an integer (i.e. users should not enter letters or doubles). To accomplish
that research the try/catch statement and include it in our code.
Once the user enter valid input and your program executes, display the results of the statistics
computed back to the user.
Show the menu again until the user decides to exit.
Create a Lab Report according to the instructions and format.
Deadline:
D476 – 10 Mar
D477 – 14 Mar
Download