📚 Year 9 Cambridge Computer Science: Case Study Practical Exercises | Year 9 剑桥计算机:案例分析实战演练
Case studies are a vital part of mastering computer science. They allow you to apply theoretical knowledge to real-world scenarios, developing problem-solving and computational thinking skills. In this article, we will work through a comprehensive case study of an automated greenhouse system, tackling input/output, algorithm design, data representation, logic gates, networking, and cybersecurity. Each section includes practical exercises to reinforce your learning.
案例分析是掌握计算机科学的重要途径。它们让你将理论知识应用到现实场景中,培养解决问题和计算思维的能力。本文将深入一个自动化温室系统的综合案例,处理输入/输出、算法设计、数据表示、逻辑门、联网和网络安全的实际问题。每个部分都包含实战练习,以巩固学习。
1. Case Study Scenario: Automated Greenhouse System | 案例场景:自动化温室系统
Imagine a smart greenhouse that automatically controls its internal environment to grow plants efficiently. The system uses temperature, humidity, and soil moisture sensors to monitor conditions. Actuators such as a heater, a ventilation window motor, a water pump, and an alarm buzzer can be switched on or off by the control unit. The main goal is to maintain the temperature within 22°C to 28°C, keep soil moisture above 40%, and trigger an alarm if humidity exceeds 90%.
设想一个智能温室,能自动控制内部环境以高效种植植物。系统使用温度、湿度和土壤湿度传感器来监测状况。控制单元可以开关加热器、通风窗电机、水泵和警报蜂鸣器等执行器。主要目标是保持温度在22°C到28°C之间,土壤湿度高于40%,当湿度超过90%时触发警报。
The control program runs on a microcontroller connected to all sensors and actuators. It reads digital values representing physical measurements, makes decisions based on thresholds, and sends signals to devices. You will design the logic, represent data, and consider how the system connects to a network for remote monitoring.
控制程序在一个连接所有传感器和执行器的微控制器上运行。它读取表示物理测量值的数字量,根据阈值做出决策,并向设备发送信号。你将设计逻辑、表示数据,并考虑系统如何连接网络以实现远程监控。
2. Identifying Inputs, Processes, and Outputs | 确定输入、处理和输出
In any computer system, it is essential to clearly identify what goes in, what processing happens, and what comes out. For our greenhouse, the inputs are the readings from three sensors: a temperature sensor (range 0-255 corresponding to 0-50°C), a humidity sensor (0-255 for 0-100% relative humidity), and a soil moisture sensor (0-255 for 0-100% moisture). An additional digital input is a manual override switch that can force the alarm on.
在任何计算机系统中,清晰确定输入、处理和输出至关重要。就我们的温室而言,输入是三个传感器的读数:温度传感器(范围0-255对应0-50°C)、湿度传感器(0-255对应0-100%相对湿度)和土壤湿度传感器(0-255对应0-100%湿度)。另一个数字输入是一个手动超控开关,可强制开启报警。
The processing compares each sensor value against predefined thresholds using conditional statements. For example, if the temperature reading is below 22°C, the heater is activated; if above 28°C, the window is opened. The humidity threshold check combines with temperature for the alarm logic. The system also calculates and stores the average temperature over the last 5 minutes.
处理过程使用条件语句将每个传感器值与预定义的阈值进行比较。例如,如果温度读数低于22°C,则启动加热器;如果高于28°C,则打开窗户。湿度阈值检查与温度结合用于报警逻辑。系统还计算并存储过去5分钟内的平均温度。
The outputs are the control signals to the heater, window motor, water pump, alarm buzzer, and an LCD display showing current temperature and humidity. Each actuator is represented by a binary output: 1 for ON, 0 for OFF.
输出是发往加热器、窗电机、水泵、警报蜂鸣器的控制信号,以及显示当前温度和湿度的LCD屏幕。每个执行器由一个二进制输出表示:1表示开,0表示关。
3. Flowchart Design for Climate Control | 气候控制流程图设计
A flowchart uses standard symbols to represent a solution visually. Ovals mark start and end points. Parallelograms indicate inputs or outputs. Rectangles show processes, and diamonds represent decisions with 'yes' or 'no' branches. Let's outline the automatic temperature regulation process.
流程图使用标准符号可视化地表示解决方案。椭圆形标记起点和终点。平行四边形表示输入或输出。矩形表示处理过程,菱形表示带有“是”或“否”分支的判断。让我们勾勒出自动温度调节过程。
The algorithm begins at the Start oval. The first step is reading the temperature sensor value (input parallelogram). Then a decision diamond checks: 'Is temperature < 22°C?' If yes, the process rectangle 'Turn heater ON' and 'Close window' are executed. If no, another diamond asks 'Is temperature > 28°C?' A yes leads to 'Open window and turn heater OFF', while a no (temperature between 22 and 28) results in 'Turn heater OFF and close window'. The flow then returns to read the sensor, creating a continuous loop.
算法始于“开始”椭圆形。第一步是读取温度传感器值(输入平行四边形)。然后一个判断菱形检查:“温度是否低于22°C?”如果是,执行处理矩形“打开加热器”和“关闭窗户”。如果否,另一个菱形询问“温度是否高于28°C?”是则执行“打开窗户并关闭加热器”,否(温度在22至28之间)则执行“关闭加热器和窗户”。流程随后返回读取传感器,形成一个连续循环。
You should also include checks for humidity and soil moisture in a full flowchart. Drawing this diagram before writing code helps to spot logical errors early and is a key skill for examinations.
你还应该在完整的流程图中加入对湿度和土壤湿度的检查。在编写代码之前绘制此图有助于尽早发现逻辑错误,这也是考试中的一项关键技能。
4. Pseudocode: Temperature Regulation Algorithm | 伪代码:温度调节算法
Pseudocode is a way of describing an algorithm using structured English that resembles programming code but is not bound to specific syntax. Here is the temperature control logic for our greenhouse.
伪代码是用结构化英语描述算法的一种方式,它类似于编程代码,但不拘泥于特定语法。以下是我们温室的温度控制逻辑。
BEGIN
REPEAT
READ temperature
IF temperature < 22 THEN
TurnHeater(ON)
OpenWindow(OFF)
ELSE IF temperature > 28 THEN
TurnHeater(OFF)
OpenWindow(ON)
ELSE
TurnHeater(OFF)
OpenWindow(OFF)
ENDIF
WAIT 1 second
UNTIL system shutdown
END
这段伪代码以BEGIN开始,无限循环读取温度。如果温度低于22,则打开加热器并关闭窗户;如果温度高于28,则关闭加热器并开启窗户;否则,两者都关闭。每次循环后等待1秒。在测试和调试时,你可以“走查”该代码,用不同的温度输入值(如20、25、30)来验证逻辑。
This pseudocode translates easily into Python or any other language you are learning. Notice how the IF-ELSE IF-ELSE structure ensures only one branch runs at a time. For the full greenhouse system, you would add similar blocks for humidity, soil moisture, and the alarm condition.
这段伪代码可以轻松转换成Python或你正在学习的任何其他语言。请注意IF-ELSE IF-ELSE结构如何确保每次只执行一个分支。对于完整的温室系统,你会为湿度、土壤湿度和报警条件添加类似的代码块。
5. Data Representation: Storing Sensor Values | 数据表示:存储传感器数值
e reading of 25°C is represented by the scaled value 128, because the full scale 255 corresponds to 50°C, so value = (25/50) × 255 = 127.5 rounded to 128. We need to store this value in memory and possibly convert to binary.
微控制器将所有传感器数据作为二进制数处理。每个传感器输出一个8位无符号整数(0到255)。例如,25°C的温度读数由标度值128表示,因为满量程255对应50°C,所以值 = (25/50) × 255 = 127.5四舍五入为128。我们需要将这个值存储在内存中,并可能需要将其转换为二进制形式。
Let' ctly one 2⁷, and all lower bits are 0. Therefore, 128 in binary is 10000000. The corresponding hexadecimal representation is 0x80, because 8 × 16¹ + 0 × 16⁰ = 128.
我们将值128转换为8位二进制数。使用标准位权值:2⁷=128, 2⁶=64, 2⁵=32, 2⁴=16, 2³=8, 2²=4, 2¹=2, 2⁰=1。128恰好多出一个2⁷,所有低位均为0。因此,128的二进制为10000000。对应的十六进制表示为0x80,因为8 × 16¹ + 0 × 16⁰ = 128。
Now consider a humidity reading of 56% ¹ + 2⁰, so 10001111. The hex is 0x8F. Representing data in multiple bases is crucial for understanding low-level operations.
现在考虑一个56%的湿度读数。原始值为0.56 × 255 ≈ 143。二进制中,143 = 128 + 8 + 4 + 2 + 1 = 2⁷ + 2³ + 2² + 2¹ + 2⁰,即10001111。十六进制为0x8F。以多种进制表示数据对于理解底层操作至关重要。
6. Binary Arithmetic: Calculating Average Temperature | 二进制算术:计算平均温度
The greenhouse controller needs to maintain a rolling average of the temperature to smooth out rapid fluctuations. Suppose the last two temperature readings were 120 (23.5°C) and 100 (19.6°C). We must add these binary values and then divide by 2 using a logical right shift.
温室控制器需要保持温度的滚动平均值,以平滑快速波动。假设最后两个温度读数为120(23.5°C)和100(19.6°C)。我们必须将这些二进制值相加,然后使用逻辑右移除以2。
ries: 01111000 + 01100100 = 11011100. The result is 220 in decimal. Now to average, we shift all bits one place to the right, inserting a 0 at the most significant bit and discarding the least significant bit. 11011100 shifted right becomes 01101110, which is 110 in decimal. This perfectly equals (120+100)/2.
将120写为8位二进制:01111000(0×128 + 1×64 + 1×32 + 1×16 + 1×8 + 0×4 + 0×2 + 0×1)。100是01100100。二进制加法按列进行,有进位:01111000 + 01100100 = 11011100。结果为十进制220。现在要求平均值,我们将所有位向右移动一位,在最高有效位插入0,并舍弃最低有效位。11011100右移后变为01101110,即十进制110。这完全等于(120+100)/2。
Logical shifts are very efficient for multiplication and division by powers of 2. Be careful: right shifting an unsigned integer is equivalent to floor division. Practice with other pairs of values to master binary arithmetic.
逻辑移位对于乘以或除以2的幂非常高效。注意:无符号整数的右移相当于向下取整的除法。练习使用其他数值对来掌握二进制算术。
7. Logic Gates in Alert Systems | 警报系统中的逻辑门
The greenhouse alarm activates if either the manual override switch is pressed, or both the temperature and humidity exceed dangerous levels simultaneously. This requires a combination of AND and OR logic gates.
如果按下手动超控开关,或者温度和湿度同时超过危险水平,温室警报就会激活。这需要AND和OR逻辑门的组合。
Define three inputs: H (humidity > 90% sed). The alarm output A is true when M=1 OR (H=1 AND T=1). In Boolean algebra: A = M + (H ⋅ T). Here '+' means OR, '⋅' means AND.
定义三个输入:H(湿度>90%,高电平有效),T(温度>28°C,高电平有效),M(手动超控,按下时高电平有效)。当M=1或(H=1且T=1)时,报警输出A为真。用布尔代数表示:A = M + (H ⋅ T)。这里“+”表示OR,“⋅”表示AND。
| M | H | T | H AND T | A (M OR (H AND T)) |
|---|---|---|---|---|
| 0 | 0 | 0 | 0 | 0 |
| 0 | 0 | 1 | 0 | 0 |
| 0 | 1 | 0 | 0 | 0 |
| 0 | 1 | 1 | 1 | 1 |
| 1 | 0 | 0 | 0 | 1 |
| 1 | 0 | 1 | 0 | 1 |
| 1 | 1 | 0 | 0 | 1 |
| 1 | 1 | 1 | 1 | 1 |
s if both H and T are 1. This can be built with a single 74-series logic IC or coded in a microcontroller.
真值表确认,当M=1时无论H和T如何警报都会响起。当M=0时,只有H和T同时为1警报才响。这可以用单个74系列逻辑IC实现,或在微控制器中用代码实现。
8. Networking IoT Sensors | 物联网传感器联网
To enable remote monitoring, the greenhouse controller connects to a local Wi-Fi network. Each sensor node and the main controller have a unique MAC address and are assigned an IP address by the router, such as 192.168.1.101 for the controller and 192.168.1.102 for a separate sensor module.
为了实现远程监控,温室控制器连接到本地Wi-Fi网络。每个传感器节点和主控制器都有唯一的MAC地址,并由路由器分配IP地址,例如控制器为192.168.1.101,独立的传感器模块为192.168.1.102。
Data is sent using the lightweight MQTT protocol. The controller publishes sensor readings to topics like 'greenhouse/temperature', and a remote dashboard subscribes to these topics to display live charts. The network relies on the TCP/IP stack, where IP handles addressing and routing, and TCP ensures reliable data delivery.
数据使用轻量级MQTT协议发送。控制器将传感器读数发布到诸如“greenhouse/temperature”的主题,远程仪表板订阅这些主题以显示实时图表。网络依赖TCP/IP协议栈,其中IP负责寻址和路由,TCP确保可靠的数据传输。
When designing such a system, you need to think about IP addressing schemes, the difference between IPv4 and IPv6, and the role of a gateway in connecting to the internet. This is a perfect scenario to practice identifying network components.
设计这样的系统时,你需要考虑IP地址分配方案、IPv4与IPv6的区别,以及网关在连接互联网中的作用。这是练习识别网络组件的绝佳场景。
9. Cybersecurity for Smart Greenhouses | 智能温室的网络安全
Connecting a greenhouse to a network introduces security risks. If an attacker gains control, they could destroy crops by altering temperature setpoints or disabling the irrigation. Strong cybersecurity practices must be built into the system from the start.
将温室连接到网络会带来安全风险。如果攻击者获得控制权,他们可能通过改变温度设定值或禁用灌溉来摧毁作物。必须从一开始就将强大的网络安全实践构建到系统中。
Start by changing default usernames and passwords on all IoT devices. Use WPA2 or WPA3 encryption on the Wi-Fi network. Data transmitted should be encrypted using TLS to prevent eavesdropping. The controller firmware should be regularly updated to patch vulnerabilities, and only necessary network ports should be open.
首先更改所有物联网设备的默认用户名和密码。在Wi-Fi网络上使用WPA2或WPA3加密。传输的数据应使用TLS加密以防止窃听。控制器固件应定期更新以修补漏洞,并且只开放必要的网络端口。
Additionally, implement input validation to prevent buffer overflow attacks, and never trust user-supplied data. A penetration test mindset helps: ask yourself how someone might break the system, and then defend against that. In your exam case studies, always mention at least three security measures.
此外,实施输入验证以防止缓冲区溢出攻击,绝不要信任用户提供的数据。采用渗透测试思维:问问自己有人可能会如何破坏系统,然后针对这种情况进行防御。在你的考试案例分析中,始终提及至少三项安全措施。
10. Testing with Normal, Boundary, and Erroneous Data | 使用正常、边界和错误数据进行测试
Th
Published by TutorHao | Year 9 Computer Science Revision Series | aleveler.com
更多咨询请联系16621398022(同微信)

或截图后用「扫一扫 → 相册」选择本图