Software Design Pattern - State Pattern

profilea-ai-a-17
hw3_state.pdf

CSE 4361: SOFTWARE DESIGN PATTERNS

Homework 3 20%

Due: 11:59PM 11/17/2020

1 Introduction

This individual homework requires the student to apply the state pattern to design a simplistic lawn mower. The mower can mow a rectangular lawn by cutting the grass one row after another. The mower stops when all of the rows are cut.

The mower consists of a motor, a grass cutter, a sensor (e.g., a photoelectric sensor), a steering hardware for making left and right turns as well as control software. The motor moves the mower forward at a constant speed (such as 2 feet per second) and drives the cutter to cut the grass underneath. The control software uses a timer to generate an action event every second — indicating that the mower has traveled a distance of 2 feet. When the control software receives the action event, it checks with the sensor to see if the right, left or bottom edge of the lawn is reached. If the right edge is reached, the software directs the steering hardware to make a right turn, advance 2 feet and make another right turn to cut the next row in the opposite direction. If the left edge is reached, the software directs the steering hardware to make a left turn, advance 2 feet and make another left turn. If the bottom edge is reached, the software directs the mower to turn off.

Conceptually, you can view the lawn as consisting of n by m squares or an n x m array, denoted by A. You can image that the size of each entry is 2 feet by 2 feet, approximately the size of a residential lawn mower. You can assume that the mower always starts cutting A[0,0] and moves from left to right (or west to east) on even rows 0, 2, 4, 6 and from right to left (or east to west) on odd rows 1, 3, 5, 7 and so on. Figure 1 illustrates the movements of the lawn mower.

Figure 1: Movements of the lawn mower

You can view the mower as moving toward three directions: left, right and down, or alternatively east, west and south. You can use integers i=0, 1, 2, ..., n and j=0, 1, 2, ...m to keep track of the location of the mower. For example, when the mower moves east on the first row, each action event will increment j and the software will check with the sensor to see if the right edge is reached. When the mower is moving west, then each action event will decrement j. When the mower makes a left or right turn, the software also checks if the bottom edge is reached before it attempts to advance to the next row (i.e., i=i+1). You can use a function, say edgeReached():boolean, to simulate checking with the sensor. The implementation of this simulated function should be very easy:

public boolean edgeReached() {

return j-1 < 0 || j+1 == m || i+1 == n || i-1 < 0;

}

1

You can think of that the three moving directions represent three different states of the lawn mower. Depending on the current state of the mower, an action event will cause different actions to take place, such as increment or decrement j, as well as making a state transition (possibly remain in the same state). Such behavior can be described by using a state transition machine/diagram. This homework requires the student to produce the state transition diagram that accurately and adequately describe the behavior of this simplistic lawn mower. In addition, this homework requires the student to apply the state pattern to produce a design for the state behavior.

2 What to Do and Submit

This homework requires the student to do the following (equal weights):

1. Produce a state machine to describe the state-dependent behavior of the lawn mower.

2. Based on the state machine your produced above, explain the behavior of the mower by tracing each of the transitions of your state machine, beginning in the initial state (that is, the EAST state).

3. Convert the state machine into a UML class diagram by applying the state pattern. The UML class diagram must show the classes, methods of the classes and relationships between the classes. The UML class diagram must also show how the methods of the classes will be implemented.

4. Describe the correspondences between the modeling constructs such as states, transitions, actions and guard conditions in the state machine and the classes, methods of the classes and relationships between the classes in the UML class diagram.

3 Bonus Points

This homework assignment does not require the students to implement the design. However, students are encouraged to implement the design to gain deeper understanding about the benefits and liabilities (if any) of the state pattern. In this regard, students who implement the design that satisfy ALL of the following criteria will receive a bonus of up to 10% extra points for this homework assignment — that is, two points or 2% for the course because this homework is 20% of the course:

1. The implementation must be in Java. You may use Swing or AWT, whichever you prefer. Provide comments in your code to show the patterns applied.

2. The implementation must include a graphical user interface (GUI) that shows the movements of the lawn mower at a reasonable speed (not too fast and not too slow).

3. The design must correctly apply the state pattern AND the implementation must also correctly implement the state pattern as applied. A partially correct implementation is considered incorrect.

Note: an implementation that runs and produces the correct result may not correctly implement the state pattern.

4. Compile and run your application. Produce and submit several consecutive screen shots to show the movements of the lawn mower.

4 How To Submit

To be announced by the TA before the deadline.

2

  • Introduction
  • What to Do and Submit
  • Bonus Points
  • How To Submit