In this lesson on Modelling in Design, you'll dive into key concepts that help simplify and solve real-world problems using programming. By the end, you'll have hands-on experience building Python programs that apply these ideas.
Here's what you'll be learning and doing:
Abstraction is a key concept in computer science and design. It means simplifying complex systems by focusing on the essential features while ignoring unnecessary details. This helps in problem-solving by making problems more manageable.
For example, when modelling a car's movement, you might abstract away details like the colour or brand and focus on speed, distance, and time. This creates an abstract model that represents the real-world system in a simplified way.
Abstraction is used in software design to create models that can be simulated and tested. It allows you to solve problems by breaking them down into core components.
Abstraction in programming means simplifying complex things by hiding unnecessary details and focusing on what's important. For example, you don't need to know how a car's engine works to drive itβyou just use the steering wheel and pedals.
Your Task: Create a simple Python program that abstracts a "Pet" object. We'll use a class to hide the details of how the pet's hunger or happiness changes, and just provide easy methods to interact with it.
Create a new folder in VS Code called ModellingLesson
and a file named pet_abstraction.py
inside it.
Add the following complete code to pet_abstraction.py
:
class Pet:
def __init__(self, name):
self.name = name
self.hunger = 50 # Hidden detail: starts at 50/100
self.happiness = 50 # Hidden detail: starts at 50/100
def feed(self):
self.hunger -= 10 # Abstracted: just feed, don't worry about how
if self.hunger < 0:
self.hunger = 0
print(f"{self.name} has been fed! Hunger is now {self.hunger}.")
def play(self):
self.happiness += 10 # Abstracted: just play, happiness increases
if self.happiness > 100:
self.happiness = 100
print(f"{self.name} is playing! Happiness is now {self.happiness}.")
def status(self):
print(f"{self.name}'s status: Hunger = {self.hunger}, Happiness = {self.happiness}")
Now, add code at the bottom to create a pet and test it:
my_pet = Pet("Buddy")
my_pet.feed()
my_pet.play()
my_pet.status()
Run the program (press F5 or use the terminal with python pet_abstraction.py
).
hunger
or happiness
βthe methods handle it for you. Try modifying the class to add another abstracted method, like sleep()
that reduces hunger but increases happiness.Modelling is the process of creating a simplified representation of a real-world system or problem to understand, analyse, or predict its behaviour. It builds directly on abstraction by taking those essential features and using them to build models that can be tested and refined.
In the context of design and software engineering, modelling allows us to break down complex problems into manageable parts. Models can be mathematical equations, diagrams, or computer simulations that mimic how a system works. This helps in problem-solving by enabling us to experiment with different scenarios without dealing with the full complexity of the real world.
For example, if you're designing a new app for traffic management, you might create a model that simulates car movements based on speed, road capacity, and traffic lights, ignoring minor details like weather unless they're crucial.
Modelling is essential in the software development process because it helps identify potential issues early, evaluate solutions, and optimise designs before implementation.
Modelling means creating a digital representation of something real-world, like a map models a city or a budget models your spending.
Your Task: Model a simple "Weather Forecast" system. We'll use variables and a function to represent daily weather data, like temperature and rain chance.
Create a new file named weather_model.py
and add the following complete code to it:
def weather_model(day, temperature, rain_chance):
# This function models the weather for a given day
if rain_chance > 50:
forecast = "Rainy"
elif temperature > 25:
forecast = "Sunny and warm"
else:
forecast = "Cloudy and cool"
print(f"Day: {day}")
print(f"Temperature: {temperature}Β°C")
print(f"Rain Chance: {rain_chance}%")
print(f"Forecast: {forecast}")
print("-------------------")
# Example data to model a week's weather
weather_model("Monday", 22, 30)
weather_model("Tuesday", 28, 60)
weather_model("Wednesday", 15, 80)
Run the program and you should see that it outputs the weather details and forecasts for Monday as 'Cloudy and cool', Tuesday as 'Rainy', and Wednesday as 'Rainy'.
Snowy
" if temperature < 0. What happens if you input extreme values like temperature = 50
or rain_chance = 100
?