Tải bản đầy đủ (.pdf) (44 trang)

btec level 5 hnd diploma in computing unit 20 advanced programming

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (5.23 MB, 44 trang )

<span class="text_page_counter">Trang 1</span><div class="page_container" data-page="1">

<b> ASSIGNMENT 2 FRONT SHEET</b>

<b>QualificationBTEC Level 5 HND Diploma in ComputingUnit number and title</b> Unit 20: Advanced Programming

</div><span class="text_page_counter">Trang 3</span><div class="page_container" data-page="3">

<b>❒</b>

<b>Summative Feedback: </b>

<b>❒</b>

<b>Resubmission Feedback:</b>

<b>Lecturer Signature:</b>

</div><span class="text_page_counter">Trang 7</span><div class="page_container" data-page="7">

List of Figure

Figure 1 - Class Diagram...7

Figure 2 - ICarState...9

Figure 3 - IncreaseAcceleration in EcoState...10

Figure 4 - Acceleration in Car class...11

Figure 5 - Button Acceleration in form...12

Figure 6 - Update UI in form...12

Figure 7 - Fuelconsumption in Ecoclass...13

Figure 8 - Fuel calculation...14

Figure 9 - Update UI for fuel...15

Figure 10 - Main Program...15

Figure 11 - After Click Normal button...16

Figure 12 - Click Speed button...17

Figure 13 - Warning Max Speed...18

Figure 14 - Warning fuel...19

Figure 15- Fuel refill warning and Low fuel warning...20

</div><span class="text_page_counter">Trang 9</span><div class="page_container" data-page="9">

1. Introduction

In Part 2, our team progressed from the initial exploration of UML diagrams and introduced different Design Patterns (DPs) in Oriented Analysis and Design (OOAD). We have now divided our team into separate tasks, each focusing on different aspects of the project.

Object-For this report, I will choose a real-life scenario from the Design Patterns phase that my team presented earlier, and I will demonstrate how to apply it using the corresponding class diagram developed by your team . Our project is about OOPing the Car object when applying DP as State when implementing it in code

2. Scenario (Duc)

Nowadays, means of transportation are becoming more and more modern to meet everyone's personal travel needs. As an Automobile manufacturing corporation, we want to have software that can provide all customers with information about the car including its characteristics, status and actions in that state.

In the car's eco mode, the car's fuel consumption will decrease compared to other modes. Besides, the acceleration ability also decreases, the car's engine sound will also change less than in other states.

Besides, in Sport mode, fuel consumption will be higher than normal functions but at the same time will provide faster acceleration compared to other vehicle states. In addition, the car's engine sound will be louder when entering sport mode.

</div><span class="text_page_counter">Trang 10</span><div class="page_container" data-page="10">

The maximum speed that the car can reach in Normal state is 120 km/h. when overtaking accelerate and there is a risk of overspeeding. The program will issue a warning to the user

</div><span class="text_page_counter">Trang 13</span><div class="page_container" data-page="13">

Figure 1 - Class Diagram

</div><span class="text_page_counter">Trang 14</span><div class="page_container" data-page="14">

Car Class:

</div><span class="text_page_counter">Trang 15</span><div class="page_container" data-page="15">

The Car class represents a virtual car and has various properties and methods to manage its state and behavior. The class includes:

The constructor takes an initial state as a parameter and initializes the fuel level.Methods:

SetState(ICarState newState): Changes the current state of the car.Brake(): Decreases the car's speed when the brake is applied.

CheckMaxSpeed(): Checks if the car has reached its maximum speed based on the current state.CalculateFuelConsumption(): Calculates the fuel consumption based on the current state.CalculateEngineNoise(): Calculates the engine noise based on the current state.GetCurrentStateName(): Gets the name of the current state.

WarnMaxSpeed(): Checks if the car is at its maximum speed and provides a warning to reduce speed if necessary.CheckFuel(): Monitors the fuel level and triggers warnings and state changes if fuel is low or empty.

NormalState, EcoState, SportState Classes:

These classes represent different states that a car can be in, and they implement the ICarState interface.

Each class defines specific behavior for increasing acceleration, calculating fuel consumption, calculating engine noise, gettingthe maximum speed, and getting the state's name. For example, the NormalState allows acceleration up to a maximum speed of120, has a fuel consumption rate of 0.4, and calculates engine noise based on the car's speed.

</div><span class="text_page_counter">Trang 17</span><div class="page_container" data-page="17">

3. Implementation (Duc)

3.1 Code

</div><span class="text_page_counter">Trang 19</span><div class="page_container" data-page="19">

3.1.1. IncreaseAcceleration

Figure 3 - IncreaseAcceleration in EcoState

This is the EcoState class which is 1 of 3 subclasses of the ICarState interface. This means that the EcoState class must provideconcrete implementations of all methods and properties defined in the ICarState interface.

Public void IncreaseAcceleration(Car): This is an implementation of the IncreaseAcceleration method required by the ICarState interface. In this implementation, it will increase the car's speed by 5 units if the current speed is less than the maximum speed (GetMaxSpeed()).

It also checks and limits the speed to the maximum speed if that limit is exceeded. With each different state there will be a different speed increase

</div><span class="text_page_counter">Trang 21</span><div class="page_container" data-page="21">

Figure 4 - Acceleration in Car class

This car class will have all the properties of a normal car, including normal functions and functions that change depending on the state it carries.

public void Accelerate(): This method represents the vehicle's acceleration action.

Here there will be a combination of several results of different functions, specifically the result of void FuelConsumption(). it will return different results based on the current state of the car

There will be several conditions under which the results of void FuelConsumption() can be divided. Under a certain condition, the vehicle will accelerate according to the preset acceleration.

CheckMaxSpeed();: This method checks and has the ability to limit the car's speed to the maximum allowable speed.If there is no fuel (other block), the car's speed is reduced by 3 units. This simulates deceleration due to running out of fuel.WarnMaxSpeed();: This method seems to be responsible for giving a warning or taking action when the vehicle reaches maximum speed. Every time you press acceleration, it will check whether the maximum speed has been reached or not. If so, a

</div><span class="text_page_counter">Trang 22</span><div class="page_container" data-page="22">

notification will be displayed on the screen

</div><span class="text_page_counter">Trang 23</span><div class="page_container" data-page="23">

Figure 5 - Button Acceleration in form

This is the event handler when the btnSpeed_Click_1 button is clicked. The creation of this button is intended for users to be able to accelerate the Car object according to the acceleration function

In this button there are called functions such as

car.CheckFuel(); : this is calling the car object and using the fuel check function to see if there is no fuel. If the fuel has not yet run out, no event will occur. Otherwise, a notification will be sent to the user via the screen and give options to the next user.

car.Accelerate(); : This will be the main purpose when creating a button to accelerate the Car object according to what has been programmed and also depends on the current state of the object.

Figure 6 - Update UI in form

UpdateUI(); : this will be the function created in the form. cells that display data each time any button is pressed. The purpose is to update the data in the interface

</div><span class="text_page_counter">Trang 25</span><div class="page_container" data-page="25">

3.1.2. Fuelconsumption

Figure 7 - Fuelconsumption in Ecoclass

Public double FuelConsumption(Car car): This method is an implementation of the FuelConsumption method defined in the ICarState interface. It takes a Car object as a parameter, representing the car whose fuel consumption is being calculated.

Return 0.3;: In this implementation, the FuelConsumption method returns the constant value of 0.3. This means that, while the car is in this state, the speed or any other factor of the car, fuel consumption is always set at 0.3.

</div><span class="text_page_counter">Trang 27</span><div class="page_container" data-page="27">

Figure 8 - Fuel calculation

double fuelConsumption = currentState.FuelConsumption(this);: This line calculates the fuel consumption by calling the FuelConsumption method of the current state (stored in the currentState variable). The this keyword is used to refer to the current Car object.

if (Fuel > 0) { ... }: This section of code checks if there is any fuel left in the car's fuel tank. If there is fuel, the car can accelerate.

Fuel -= fuelConsumption;: This line subtracts the calculated fuelConsumption from the car's fuel tank, representing the fuel consumption during acceleration. If the calculated consumption depletes the fuel completely (fuel becomes negative), it sets the fuel level to 0 to ensure it doesn't go negative.

else { ... }: If there's no fuel left (i.e., Fuel is 0 or negative), the car's speed is reduced by 3 units.

Speed = Math.Max(0, Speed - 3);: This line sets the car's speed to the greater of 0 and the current speed minus 3. This ensures that the speed doesn't go below 0.

</div><span class="text_page_counter">Trang 29</span><div class="page_container" data-page="29">

Figure 9 - Update UI for fuel

After being called in and processing the information carefully, like displaying the speed on the display bar. The remaining fuel in the car will be updated when pressing any button that affects the car's current fuel.

3.2 Program screenshots

Figure 10 - Main Program

</div><span class="text_page_counter">Trang 30</span><div class="page_container" data-page="30">

The following is a screenshot of our running program. We have 4 labels to represent the textbox bars with their data such as: Speed, Noise, State and Fuel

</div><span class="text_page_counter">Trang 31</span><div class="page_container" data-page="31">

There are 3 more text boxes behind to show that fuel will be calculated in L, speed will be calculated in Km/h and vehicle noise will be calculated in dB.

Figure 11 - After Click Normal button

There are 5 buttons that display changes in the vehicle's status. When selecting Normal mode.

The vehicle will display Normal data including current speed, vehicle's remaining fuel, and vehicle's current engine noise. With the same function State buttons also display the same vehicle data.

</div><span class="text_page_counter">Trang 33</span><div class="page_container" data-page="33">

Figure 12 - Click Speed button

Each time you click on speed, the speed will increase by 7km/h and after each click on Break. The vehicle speed will decrease to 7km/h if you press the brake button on the interface. Noise and fuel will vary according to the vehicle's current speed.

</div><span class="text_page_counter">Trang 35</span><div class="page_container" data-page="35">

Figure 13 - Warning Max Speed

In Normal state, the maximum speed that the car can reach is 120km/h. If the user still presses the acceleration button, the program will display a warning "You are running at maximum speed. Do you want to slow down?

Similar to the normal state, other states such as eco and sport also have a maximum speed mark to issue warnings to the user.

</div><span class="text_page_counter">Trang 37</span><div class="page_container" data-page="37">

Figure 14 - Warning fuel

If the vehicle's fuel is less than 10L, a message will display "Out of gas! Switch to Eco mode?”, “Fuel warning” and there will be a screen to switch fuel economy state to Eco or not.

If you select yes, it will be displayed. to Eco state, if not selected, you are still in active state.If you are already in the car's eco mode, the warning will not be sent to the user

</div><span class="text_page_counter">Trang 39</span><div class="page_container" data-page="39">

Figure 15- Fuel refill warning and Low fuel warning

After the fuel level drops below 5L, there will be another warning sent to the user: "Out of gas! Please refuel soon", "Fuel warning"

When it reaches 0L, the message "Out of fuel! Vehicle has stopped" will appear. is displayed. "Fuel warning" when the user clicks on the speed, the speed decreases by 3km/h each time and if the brake is pressed, the speed still decreases by 7km/h.

4. Discussion (Nam) 4.1Range of similar patterns

A quick overview of a similar trend. Explain why the pattern is the best fit for your situation.

<b>similar pattern:</b>

Abstract Factory

Make things with specified attributes: The "Abstract Factory" template allows you to make objects with specific properties of automobiles in each state. For example, you might design an Abstract Factory for the "Eco" state and another for the "Sport" state, with each Factory producing things (e.g., fuel consumption objects) appropriate for that condition.

</div><span class="text_page_counter">Trang 40</span><div class="page_container" data-page="40">

Separation of interface and implementation: The "Abstract Factory" design divides an abstract factory's interface from the actual implementation of its concrete factories. This allows you to easily alter or extend the way objects are formed without having to update the main source code.

</div><span class="text_page_counter">Trang 41</span><div class="page_container" data-page="41">

Manage different car states and properties: With different car states and properties (Eco mode and Sport mode), the "Abstract Factory" template allows you to manage all states and properties efficiently and simply in the future.

Simple product expansion: If you wish to add additional states or attributes to your product in the future, the "Abstract Factory" template allows you to do so without modifying the entire system.

Optimize performance and resources: Using this pattern, you may optimize the production of state-specific objects and information, allowing you to conserve resources and enhance speed.

<b>The reason why we choose state pattern.</b>

Managing vehicle states: Your project necessitates the management of several vehicle states, including "Eco," "Normal," and "Sport." The State Design Model enables you to simply monitor and alter the state of the vehicle without the need for elaborateif-else control statements.

Use the State Design approach to construct different state classes for each vehicle state, making project code easier to manage. This makes the code easier to maintain since you can focus on each state independently rather than having to alter the whole code every time a modification or expansion of a specific state's capability happens.

Ease of extension: If you wish to add more states to your vehicle in the future, the State Design paradigm allows you to do so without altering existing code. This allows the project to easily expand and react to changes in product needs..

Separating state logic: This model helps you separate the processing logic for each state from each other. This makes the source code easier to read and understand and reduces the risk of bugs due to the interference of logic between states.

4.2Usage of pattern

In my program there are some advantages and disadvantages as follows:

</div><span class="text_page_counter">Trang 42</span><div class="page_container" data-page="42">

separate classes, which might lead to issues.

</div><span class="text_page_counter">Trang 43</span><div class="page_container" data-page="43">

Not suitable for all types of applications: Pattern State should be used when an object has numerous states and behavior that is dependent on the state. In certain basic scenarios, employing Pattern State might add unnecessary complexity to the code.

</div>

×