共计 16737 个字符,预计需要花费 42 分钟才能阅读完成。
CSSE1001 / 7030 Semester 1, 2019
Assignment 2 (15 Marks)
Due: Friday 3 May 2019 at 8:30pm
Introduction
The second assignment is a simple weather prediction and event planning system. You will provide
some information about how the weather could affect an event. The system will predict the weather
and indicate if conducting the event is advisable.
The purpose of this assignment is to help you to understand how to make use of classes to define
an object’s structure. And, to help you understand how to implement program functionality by
having several objects interacting with each other, by sending messages to each other.
Overview
You are provided with three files for this assignment. They are weather_data.py,
prediction.py and event_decision.py. Each file defines a set of classes, which are described
below. You will need to implement parts, or all, of the classes in the prediction.py and
event_decision.py files.
The file, weather_data.py, contains the WeatherData and WeatherDataItem classes.
WeatherData represents all the weather data that is loaded from the data file. WeatherDataItem
represents weather data for a single day. It provides methods to access this data.
The prediction.py file defines the super class WeatherPrediction. It defines a set of methods
that you need to override in subclasses that you implement for the assignment. An example of doing
this is provided by the YesterdaysWeather subclass. You need to implement the SimplePrediction
and SophisticatedPredication subclasses.
The file, event_decision.py, provides a description of the EventDecision class. It uses data
about the event to determine if aspects of the weather would impact on the event. You will need
to implement the init and advisability methods. The advisability method determines how
advisable it is to continue with the event, based on the weather prediction. Two other classes
defined in this file are Event and UserInteraction. The Event class represents the data about an
event and defines a set of methods that you will need to implement. The UserInteraction class
provides methods that allow the program to interact with the user. It defines a set of methods that
you will need to implement. Two of these methods, get_prediction_model and output_advisability,
are partially implemented with some example ideas that you may use in your solution.
CSSE1001 / 7030 Semester 1, 2019
Design
The following diagram provides an overview of the classes in this design and their relationships.
The WeatherDataItem class defines a type of object that holds weather data for one day. It stores
the amount of rainfall, the maximum and minimum temperatures, the number of hours of sunshine,
average relative humidity, average and maximum wind speeds, predominant direction of the wind,
extent of cloud cover and the mean sea level air pressure. This is the data that will be used to predict
the next day’s weather. The WeatherDataItem class has methods that provide access to the data.
An object of the WeatherData class loads the weather data from a file and creates a list of
WeatherDataItem objects to hold the data. The load method takes a string as a parameter that is
the name of the file from which the file is to be loaded. The get_data method takes an integer
parameter that is the number of days of data to return. The data is returned as a list of
WeatherDataItem objects. The size method returns the number of WeatherDataItems being stored
in the WeatherData object.
The WeatherData and WeatherDataItem classes are implemented for you.
The WeatherPrediction class defines the abstract interface for any weather prediction model. Each
different prediction model will be a subclass of WeatherPrediction. The WeatherPrediction class
has a reference to a WeatherData object, which is used to access the data to make a prediction.
Each of the methods in WeatherPrediction, aside from __init__, raise the NotImplementedError.
This is because these methods need to be implemented in the subclasses. An example of doing this
is provided by the YesterdaysWeather class. An object of this class makes a prediction by accessing
yesterday’s WeatherDataItem object and using its data to indicate that tomorrow’s weather will be
pretty much the same as yesterday’s. You need to implement subclasses for two other prediction
models. These are called SimplePrediction and SophisticatedPrediction.
CSSE1001 / 7030 Semester 1, 2019
An object of the EventDecision class uses the predictions made by one of the subclasses of
WeatherPrediction and the details from an object of the Event class to indicate how advisable it is
to continue with an event.
An object of the UserInteraction class provides the user interface that allows a user to interact with
the program. It gets the details of an event from the user and creates an Event object to hold this
data. It also allows a user to select which weather prediction model to use and creates the
appropriate object of a subclass of WeatherPrediction. When it does this it passes the WeatherData
object to the WeatherPrediction object. After the EventDecision object has determined the
advisability of continuing with the event, the UserInteraction object is given the advisability rating
and outputs these details to the user. The UserInteraction object also provides a method to check
to see if the user wants to try checking the advisability using a different prediction model.
The main function starts the execution of the program. It creates a WeatherData object and gets it
to load the data from a file. It creates a UserInteraction object to provide the user interface. The
main function gets the user interface to ask the user for event details and to choose the prediction
model. Once this is done, the main function then gets the EventDecision object to determine the
advisability of continuing with the event and then has the user interface output the result.
Example Usage
Let’s determine how suitable your event is for the predicted weather.
What is the name of the event? My Event
Is the event outdoors? Y
Is there covered shelter? Y
What time is the event? 18
Select the weather prediction model you wish to use:
1) Yesterday’s weather.
2) Simple prediction.
3) Sophisticated prediction.
1
Based on the YesterdaysWeather model, the advisability of holding My Event is
2.8600000000000003.
Would you like to check again? Y
Select the weather prediction model you wish to use:
1) Yesterday’s weather.
2) Simple prediction.
3) Sophisticated prediction.
2
Enter how many days of data you wish to use for making the prediction: 4
Based on the SimplePrediction model, the advisability of holding My Event is 5.
Would you like to check again? Y
Select the weather prediction model you wish to use:
1) Yesterday’s weather.
2) Simple prediction.
3) Sophisticated prediction.
3
Enter how many days of data you wish to use for making the prediction: 12
Based on the SophisticatedPrediction model, the advisability of holding My Event
is 5.
Would you like to check again? N
CSSE1001 / 7030 Semester 1, 2019
Tasks
You need to implement the Event, UserInteraction, EventDecision, SimplePrediction and
SophisticatedPrediction classes. Method definitions are provided for some classes in the
prediction.py and event_decision.py files. You may not change the interfaces (names,
parameters or return type) of any of these methods. You may add other private instance variables
and methods to these classes to implement your logic and to structure your code.
Event defines an object that holds data about a single event and provides access to that data. The
Event constructor will take as parameters:
name: a string representing the name of the event,
outdoors: a Boolean value representing whether the event is outdoors,
cover_available: a Boolean value representing whether there is cover available,
time: an integer from 0 up to, but not including, 24, indicating the closest hour to the starting
time of the event.
You need to implement the following methods:
init – Stores local references to the given parameters
get_name – Returns the Event name
get_time – Returns the integer time value
get_outdoors – Returns the Boolean outdoors value
get_cover_available – Returns the Boolean cover_available value
str – Returns a string representation of the Event in the following format:
‘Event(name @ time, outdoors, cover_available)’
One example might look like:‘Event(Party @ 12, True, False)’
UserInteraction defines an object that is the program’s user interface. Unlike the first assignment,
you have some flexibility in how you display your prompts and results to the user. You need to
implement the following methods:
get_event_details – Prompt the user to enter the data required to create an Event object,
and return that object. The prompts need to be in the same order as shown in the example
usage (event name, outdoors, shelter, time). The event name can be any string. If the event
is outdoors of if there is shelter should be indicated by the user entering either‘Y’or‘Yes’,
in any combination of lower or upper case characters. The UserInteraction object should
store a reference to the Event object, to be able to refer to it in other methods (e.g.
output_advisability).
get_prediction_model – An initial version of this method is provided. You need to extend it
when you implement each new subclass of WeatherPrediction. The user needs to enter the
digit corresponding to their choice of prediction model. If the prediction model is the simple
or sophisticated prediction model, the user needs to then enter how many days of data
should be used by the model. The UserInteraction object should store a reference to the
selected WeatherPrediction object, to be able to refer to it in other methods (e.g.
output_advisability).
output_advisability – Display the result of determining how advisable it is to continue with
the event.
another_check – Ask the user if they would like to try using a different weather prediction
model. The user needs to enter‘Y’or‘Yes’to indicate they want to perform another check.
The user enters‘N’or‘No’to indicate they do not want to perform any more checks. This
method only asks if they want to try again and returns a bool value indicating their response.
It does not call other methods to restart the prediction.
CSSE1001 / 7030 Semester 1, 2019
EventDecision defines an object that uses the predicted weather to determine the impact it will
have on an event. You need to implement the following methods:
init – Stores local references to the Event and WeatherPrediction objects passed as
parameters.
_temperature_factor – Returns the temperature factor which is determined by the following
steps:
- The predicted temperatures may be adjusted based on the humidity. If the humidity is
greater than 70% a humidity factor is calculated by dividing the humidity value by 20.
The humidity factor should be added to the high and low temperatures, if they are
positive; or subtracted from any temperature if it is negative. - An initial temperature factor is calculated based on the following rules:
a) If the time is between 6 and 19 inclusive and the event is taking place outdoors, and
the adjusted high temperature is greater than or equal to 30, then the temperature
factor is the adjusted high temperature divided by -5 and then 6 is added to the
result.
b) If the adjusted high temperature is greater than or equal to 45, regardless of time
or if the event is indoors or outdoors, the temperature factor is calculated using the
same formula (high temp ÷ -5 + 6).
c) If the time is between 0 and 5 inclusive or 20 and 23 inclusive and the adjusted low
temperature is less than 5, and the adjusted high temperature is less than 45, then
the temperature factor is the adjusted low temperature divided by 5 and then 1.1
is subtracted from the result (low temp ÷ 5 – 1.1).
d) If the adjusted low temperature is greater than 15 and the adjusted high
temperature is less than 30, the temperature factor is the low temperature
subtracted from the high temperature and the result divided by 5 ((high – low) ÷ 5).
e) In all other cases, the initial temperature factor is 0. - After calculating the initial temperature factor, if it is negative because of high
temperature (rules a or b) add 1 to the temperature factor for each of the following
conditions:
There is cover available for the event.
The wind speed is greater than 3 and less than 10.
The cloud cover is greater than 4.
_rain_factor – Returns the rain factor which is determined by the following steps: - An initial rain factor is calculated based on the following rules.
a) If the chance of rain is less than 20%, then the rain factor is the chance of rain
divided by -5 and then 4 is added to the result (chance of rain ÷ -5 + 4).
b) If the chance of rain is greater than 50%, then the rain factor is the chance of rain
divided by -20 and then 1 is added to the result (chance of rain ÷ -20 + 1).
c) In all other cases, the initial rain factor is 0. - After calculating the initial rain factor:
If the event is outdoors and cover is available, and the wind speed is less than 5, add - to the rain factor.
If the rain factor is less than 2 and the wind speed is greater than 15, then divide
the wind speed by -15 and add this result to the rain factor (rain factor + (wind speed
÷ -15)). If the rain factor is less than -9 then set it to be -9.
advisability – Returns the advisability ranking, which is calculated by the following: - Add the final temperature factor and rain factor together. If the result is less than -5, set
it to be -5. If the result is greater than 5, set it to be 5. Return this result as the advisability
score.
CSSE1001 / 7030 Semester 1, 2019
SimplePrediction defines an object that predicts the weather based on the average of the past n
days’worth of weather data, where n is a parameter given to the class at construction. You need to
override the methods defined in the WeatherPrediction class.
init – Retrieves and stores references to the past n days’weather data. If n is greater
than the number of days of weather data that is available, all of the available data is stored
and used, rather than n days.
get_number_days – Returns the number of days of data being used
chance_of_rain – Calculate the average rainfall for the past n days. Multiply this average by - If the resulting value is greater than 100, set it to be 100. Return this result.
high_temperature, low_temperature – Each should return the highest/lowest temperature
recorded in the past n days
humidity and cloud_cover – Each should calculate the average of their data from the past n
days and return this result.
wind_speed – Use only the average wind speed for the past n days, calculate the average
value and return this result.
SophisticatedPrediction defines an object that predicts the weather based on the past n days’worth
of weather data, where n is a parameter given to the class at construction. You need to override the
methods defined in the WeatherPrediction class.
init – Retrieves and stores references to the past n days’weather data. If n s greater
than the number of days of weather data that is available, all of the available data is stored
and used, rather than n days.
get_number_days – Returns n, the number of days of data being used
chance_of_rain – Calculate the average rainfall for the past n days. If yesterday’s air pressure
was lower than the average air pressure for the past n days, multiply the average rainfall by - If yesterday’s air pressure was higher than the average air pressure for the past n days,
multiply the average rainfall by 7. After taking into account the air pressure, if yesterday’s
wind direction was from any easterly point (NNE, NE, ENE, E, ESE, SE or SSE) multiply the
average rainfall by 1.2. If the resulting value is greater than 100, set it to be 100. Return this
result.
high_temperature – Calculate the average high temperature for the past n days. If
yesterday’s air pressure was higher than the average air pressure for the past n days, add 2
to the calculated average high temperature. Return this result.
low_temperature – Calculate the average low temperature for the past n days. If yesterday’s
air pressure was lower than the average air pressure for the past n days, subtract 2 from the
calculated average low temperature. Return this result.
humidity – Calculate the average humidity for the past n days. If yesterday’s air pressure was
lower than the average air pressure for the past n days, add 15 to the calculated average
humidity. If yesterday’s air pressure was higher than the average air pressure for the past n
days, subtract 15 from the calculated average humidity. If the resulting value is less than 0
or greater than 100, set it to be 0 or 100. Return this result.
cloud_cover – Calculate the average cloud cover for the past n days. If yesterday’s air
pressure was lower than the average air pressure for the past n days, add 2 to the calculated
average cloud cover. If the resulting value is greater than 9, set it to be 9. Return this result.
wind_speed – Calculate the average of the average wind speed for the past n days. If
yesterday’s maximum wind speed was greater than four times the calculated average wind
speed, multiply the calculated average wind speed by 1.2. Return this result.
When an object of any of the WeatherPrediction subclasses is created, it will store the weather data
that it will use to make its predictions. Loading new data into the WeatherData object will not
update the data used by the WeatherPrediction object.
CSSE1001 / 7030 Semester 1, 2019
Hints
The focus of this assignment is on demonstrating your ability to implement a simple object-oriented
program using objects, classes and inheritance. It is possible to pass the assignment if you only
implement some of the specified functionality. You should design and implement your program in
stages, so that after the first stage you will always have a working previous partial implementation.
It is recommended that you start by implementing the Event class and then the EventDecision class.
You can then implement the UserInteraction class. This will allow you to start using the program.
The provided YesterdaysWeather class will give you an initial weather prediction model. You could
implement the advisability method in EventDecision to initially return a random value without
implement the _temperature_factor and _rain_factor methods. This allows you to run the program
and create an Event object, even though the logic does not do anything useful. After initially testing
Event, you should then implement advisability so that it uses _temperature_factor and _rain_factor.
You can then implement the SimplePrediction class as your own weather prediction model. The last
stage of development should be to implement the SophisticatedPrediction class.
Submission
You must submit your assignment electronically through Blackboard. You need to submit both your
prediction.py and event_decision.py files (use these names – all lower case). You may
submit your assignment multiple times before the deadline – only the last submission before the
deadline will be marked.
Late submissions of the assignment will not be accepted. Do not wait until the last minute to submit
your assignment, as the time to upload it may make it late. In the event of exceptional personal or
medical circumstances that prevent you from handing in the assignment on time, you may submit
a request for an extension. See the course profile for details of how to apply for an extension:
https://www.courses.uq.edu.au…
Requests for extensions must be made no later than 48 hours prior to the submission deadline. The
expectation is that with less than 48 hours before an assignment is due it should be substantially
completed and submittable. Applications for extension, and any supporting documentation (e.g.
medical certificate), must be submitted via my.UQ. You must retain the original documentation for
a minimum period of six months to provide as verification should you be requested to do so.