The nontrivial Java example Mouse in a Maze - PowerPoint PPT Presentation

1 / 93
About This Presentation
Title:

The nontrivial Java example Mouse in a Maze

Description:

A simple software development process from specification, to ... It can decide wether it faces a wall or not. DAAD project 'Joint Course on OOP using Java' ... – PowerPoint PPT presentation

Number of Views:175
Avg rating:3.0/5.0
Slides: 94
Provided by: bot52
Category:

less

Transcript and Presenter's Notes

Title: The nontrivial Java example Mouse in a Maze


1
The non-trivial Java example Mouse in a Maze
  • A simple software development process from
    specification, to design, implementation, and
    test

2
Source
  • Based on an idea from S.N. Kamin, M.D. Mickunas,
    E.M. Reingold An introduction to computer
    science Using Java, McGraw-Hill, 1998

3
Points of this Java sample program
  • Introduce a
  • non- trivial
  • Java program

Demonstrate the importance of software
engineering principles for ordinary Java program
development
Illustrate a methodology how to present larger
Java programs in lectures
Interactive style of the lecture improves
learning effects
Using Java API
4
Course materials
  • Lecture slides
  • about 90 slides
  • takes 3 x 2 lecture hours at HU (interactive
    style)
  • Java sources
  • Sum 378
  • Assignments
  • modify, extend the program
  • implement as an applet (grafical interface)

5
Didactical mistake in presenting complex Java
programs in lectures
  • The lecturer tries to explain each technical
    detail of program code
  • non-understandable in a lecture
  • boring
  • The lecturer starts with a Java program too early
  • details of requirements specification still open
  • design / class structure not well discussed
  • Finding a proper class structure may be more
    challenging then implementing the Java program
    code.

6
Didactical principles in presenting complex Java
programs in lectures
  • Follow the software engineering process
  • requirements analysis, design, implementation
    test
  • Involve students into this process
    (interactivity)
  • Only outline the main ideas of the implementation
  • Assignments support detailed understanding the
    code

7
Requirements analysis Mouse in a Maze
  • Task
  • Develop a program, that simulates the movement
    of a mouse in a maze (labyrinth) from an entry to
    an exit.

8
Facts concerning software development phases
  • Most errors of software systems result from
    misunderstanding the problem to be solved.
  • Software development consists only of a small
    part of programming
  • Requirements analysis 20
  • Design 15
  • Implementation 20
  • Test 45

9
Learning goals
  • This example should illustrate the importance of
    a complete and correct requirements specification
    for the project success.
  • Before implementing, the next step is the design
    of the application Which components belong to
    the program architecture?
  • Starting with the implementation too early may
    lead to projects failure.

10
Development process of Mouse in a Maze
  • Requirements analysis
  • Design
  • Implementation and test

11
Requirements analysis Mouse in a Maze
  • Task
  • Develop a program, that simulates the movement
    of a mouse in a maze (labyrinth) from an entry to
    an exit.

12
Open questions
  • How does a maze look like?
  • What is a mouse able to do (e.g. which kinds of
    movements)?
  • What is the initial state of the mouse?
  • What, if there is no way from entry to exit?
  • In which way should the solution be presented?

13
How does a maze look like?
14
Examples of our kind of mazes
15
Requirements specification (1)
  • Develop a program, that simulates the movement
    of a mouse in a maze (labyrinth) from an entry to
    an exit.
  • 1. The Maze
  • A maze is a collection of quadratic rooms
    arranged in a rectangle. Two adjacent rooms are
    separated by a wall or opening (gap). The maze is
    surrounded by a wall which is open at two
    locations (entry and exit). The size of the maze
    (length and height) is variable.
  • Examples

16
How does a mouse look like?
17
Our mouse
18
Mouse confronted with different mazes
No problem ?
This will become hard ?
19
Which kinds of movements is our mouse able to do?
  • step forward
  • turn left
  • turn right

20
Possible movements of the mouse
May move one step forward into the adjacent room
in the line of vision
May turn left or right by 90 degrees
21
Two implemented solutions
  • Proper solution for the wrong problem.
  • In each case the shortest path has been found.
  • ? Requirements specification is incomplete

22
Requirements specification is incomplete
  • In each case the shortest path has been found
  • Only possible if the mouse knows the whole maze
    (view from above)
  • This is not the characteristic of maze problems.

23
Perceptions of the mouse
May see only inside of one room (not into the
adjacent room)
Line of vision only straight forward
May decide Do I face a wall or an opening (gap)?

May decide Am I inside or outside the maze?
24
Field of view of a mouse
25
The problem may be huge ?
26
Requirements Specification (2)
  • 2. The Mouse
  • The mouse has no general overview of the maze.
  • a) The mouse may move in the following
    way turn left, turn right (by 90 degrees), move
    forward into the adjacent room.
  • b) The mouse is located either in a fixed room
    inside the maze or just next to the entry or
    exit. In addition, it has a fixed line of
    vision.
  • c) The mouse can see only in the line of vision.
    It can decide wether it faces a wall or not.

27
Requirements specification open problems?
  • Description of the initial state of the mouse
  • Description of the final state of the mouse
  • Description of the task
  • Special cases
  • User interface

28
Requirements Specification (3)
  • 3. Desription of the initial state of the mouse
  • The mouse is initially placed in front of the
    entrance to the maze (the next step forward takes
    her to the maze).
  • Example

29
Requirements Specification (4)
  • 4. Desription of the final state of the mouse
    and of the task
  • We a looking for a sequence of movements (turn
    left, turn right, step forward) leading the mouse
    from the entry to the exit.
  • Example

30
Requirements Specification (5)
  • 5. Special case
  • If there is no exit or if there is no way from
    the entry to the exit, then the mouse should
    leave the maze through the entry.
  • Example

entry
31
Requirements Specification (6)
  • 6. User interface
  • a) Textual output of the solution
  • Example
  • step forward, turn right,
  • b) Graphical user interface
  • The sequence of movements will be displayed
    with the help of a graphical applet, i.e. the
    steps of the mouse are visualized. The user can
    interactively trigger the next step.

32
Solvability of the problem
  • Is there an algorithm at all that solves the
    problem for arbitrary mazes taking into account
    the characteristic properties of the mouse?

33
Algorithm basic principle
  • Principle
  • Have the mouse walk with its right side on the
    wall of the maze.
  • Still not an algorithm
  • An algorithm has to determine the sequence of
    mouse movements.

34
Algorithm as a pseudo code program
only one step
  • if mouse is still inside the maze
  • turn right
  • if (facing a wall?)
  • then
  • turn left and test if (facing a wall?)
  • if (facing a wall?) then turn left and test ...
  • else
  • step forward

35
Strong pseudo code one step
IF (NOT outside the maze?) BEGIN /do next
step/ turn right WHILE (facing a wall?)
DO turn left ENDWHILE step forward END
36
Strong pseudo code whole algorithm
step forward / enter the maze / WHILE(NOT
outside the maze?) BEGIN /do next step/ turn
right WHILE (facing a wall?) DO turn
left ENDWHILE step forward END ENDWHILE
Think about The algorithm fulfills the principle.
37
Mouse movements according to the algorithm first
example
38
Mouse movements according to the algorithm in
detail first example
39
Mouse movements according to the algorithm
second example
exit
entry
40
Mouse movements according to the algorithm in
detail second example
exit
entry
41
Mouse movements graphical and textual output
step forward turn right step forward turn
right turn left turn left step forward turn
right turn left turn left step forward turn
right step forward
42
Textual output of the solution
  • xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
    x
  • x
    x
  • x
    x
  • x
    x
  • x
    x
  • x
    x
  • x
    x
  • x
    x
  • x
    x
  • x
    x
  • x
    x
  • x
    x
  • x x x
    xxxxxxxxxxxxxx
  • x x
    x
  • x x
    x
  • x x
    x
  • x x
    x
  • x x
    x

step forward turn to the right step forward turn
to the right turn to the left turn to the
left turn to the left step forward
43
Mouse in a maze only a nice game?
  • Sample of finding algorithms for robots dealing
    with different tasks
  • Playing soccer
  • Robot moving on the moon
  • Rescue robots

44
Development process of Mouse in a Maze
  • Requirements analysis
  • Design
  • Implementation and test

45
Task
  • Requirements specification

Next step ?
  • Start with programming?
  • If so,
  • with which part/component?
  • ???

46
Design develop the software architecture
  • SW architecture description languages UML
  • Our task define an object-oriented architecture
  • Main problem How to find classes?

47
Software development Phases and results
  • Analysis Definition
  • Requirements specification
  • Design
  • Software architecture
  • Implementation
  • ? Program
  • Test
  • Test cases

48
As a matter of fact
  • Finding a proper class structure may be more
    challenging then implementing the Java program
    code.

49
Software architecture
  • Structure of the software
  • Which components exist?
  • Which relations are between them?

Stack Push(e) Pop() Top()
50
Architecture specification languages
  • Graphical languages for the specification of
    software architectures
  • Industry standard UML
  • Unified Modelling Language
  • Now examples of one graphical element of UML
  • class diagram
  • What programming languages mean to the
    implementation phase, are architecture
    specification languages to the design phase.

51
UML classes the structure
Name
Attributes state
  • Time
  • hour int
  • minute int
  • Time (hour int, minute int)
  • addMinutes (Min int)
  • printTime ( )

hidden
Operations behaviour
52
UML classes as interfaces
  • Time
  • hour int
  • minute int
  • Time (hour int, minute int)
  • addMinutes (Min int)
  • printTime ( )

53
The usable interface of a class only visible
features
  • Time
  • - hour int
  • minute int
  • Time (hour int, minute int)
  • addMinutes (Min int)
  • printTime ( )

The only way to interact with objects of this
class is to call its methods
54
Relations between classes Associations,
inheritance,
Class 1
Class 2
Association
Class 1
Class 2
Directed association objects of class one know
objects of class two and not vice versa
55
How to find classes?
  • By separation of the problem to sub-problems
  • Sub-problems may become classes
  • From the objects of the problem area
  • Investigate the requirements specification to
    find objects of the problem area

56
Case studyMouse in a Maze
  • Which objects of the problem area should be
    implemented as component / class of the system?

Mouse
Maze
Algorithm for mouse movement
User interface /output
57
Which relations exist between the components?
Mouse
Maze
Algorithm for mouse movement
User interface /output
58
Which relation Who needs whom?
no
Mouse
Maze
e.g. test if in front of a wall
?
Alg. has not to know the maze
User interface /output
Algorithm for mouse movement
?
?
59
Class diagram for a mouse
  • Mouse
  • ?
  • ?

60
Interface of the mouse Which data, which
operations? (first approach)
State place / direction in a maze
  • Mouse
  • Location Point
  • Direction int
  • stepForward ( )
  • turnLeft ( )
  • turnRight ( )
  • facingWall ( ) boolean
  • outsideMaze ( ) boolean

Behaviour abilities of the mouse (movements
perceptions)
61
Detailed interface of the mouse
  • Mouse
  • - Location Point
  • Direction int
  • started boolean
  • - theMaze Maze
  • Mouse (Maze m)
  • getLocation ( ) Point
  • stepForward ( )
  • turnLeft ( )
  • turnRight ( )
  • facingWall ( ) boolean
  • outsideMaze ( ) boolean

Create a mouse in relation to a particular maze
Visible attribute
Current position of the mouse
62
Interface of the maze
  • Maze
  • ?
  • ?

63
Interface of the maze(first approach)
Is the current position outside of the maze?
  • Maze
  • outside (pos Point) boolean
  • checkWall (direction int, pos Point)
    boolean
  • getStartLocation ( ) Point

Is there a wall at position pos in this direction?
Where to place the mouse initially? (position
direction)
64
Interface of the maze attributes ?
  • Maze
  • outside (pos Point) boolean
  • checkWall (direction int, pos Point)
    boolean
  • getStartLocation ( ) ?

65
Detailed interface of the maze
Detailed maze data are too complex to be included
in a class diagram.
  • Maze
  • height int
  • width int
  • getStartLocation ( ) Point
  • getStartDirection ( ) int
  • getSize ( ) Point
  • checkWall (direction int, pos Point)
    boolean
  • checkWall (direction int, col int, row int)
    boolean
  • outside (pos Point) boolean

The height and width of the maze are useful to
draw the maze
Height and width as a point.
To find class diagrams is an iterative process
start with a simple solution and extend and
modify if necessary.
66
Software Architecture the whole view (textual
output)
  • Mouse
  • - Location Point
  • - Direction int
  • started boolean
  • - theMaze Maze
  • Mouse (Maze m)
  • getLocation ( ) Point
  • stepForward ( )
  • turnLeft ( )
  • turnRight ( )
  • facingWall ( ) boolean
  • outsideMaze ( ) boolean
  • Maze
  • height int
  • width int
  • getStartLocation ( ) Point
  • getStartDirection ( ) int
  • getSize ( ) Point
  • checkWall (direction int, pos Point)
    boolean
  • checkWall (direction int, col int, row int)
    boolean
  • outside (pos Point) boolean

Textual output of the maze printMaze ( )
Algorithm for mouse movement main ( )
67
Problems of the development of a software
architecture
  • Software architecture
  • Not unique (many good and many bad solutions)
  • Design of a software architecture does not
    succeed for the first time
  • Long process
  • Software architecture develops stepwise
  • Principle
  • Start with one preliminary architecture
  • The usability of the methods finally turns out
    only during the implementation.

68
Development process of Mouse in a Maze
  • Requirements analysis
  • Design
  • Implementation and test

69
Java sources
Sum 378
70
Information to the audience
  • This presentation gives only an outline of the
    principle points of the implementation.
  • Several crucial technical details will be
    explained.
  • However, not each implementation detail will be
    presented. This would be boring in a lecture.
  • During the self-study and with assignments,
    everybody should explain oneself open questions.

71
Planning of implementation steps
Mouse
Maze
User interface Textual output of the maze
Algorithm for mouse movement
  • UML class diagram
  • Dependencies ? order of implementation

72
Software Architecture the whole view
  • Mouse
  • - Location Point
  • - Direction int
  • started boolean
  • - theMaze Maze
  • Mouse (Maze m)
  • getLocation ( ) Point
  • stepForward ( )
  • turnLeft ( )
  • turnRight ( )
  • facingWall ( ) boolean
  • outsideMaze ( ) boolean
  • Maze
  • height int
  • width int
  • getStartLocation ( ) Point
  • getStartDirection ( ) int
  • getSize ( ) Point
  • checkWall (direction int, pos Point)
    boolean
  • checkWall (direction int, col int, row int)
    boolean
  • outside (pos Point) boolean

Textual output of the maze printMaze ( )
Algorithm for mouse movement main ( )
In the lecture 1, 2, 3b (main ideas)
Self-study 3a 1, 2, 3b (details)
73
Implementation of the maze
  • Starting from the class diagram
  • Maze
  • height int
  • width int
  • getStartLocation ( ) Point
  • getStartDirection ( ) int
  • getSize ( ) Point
  • checkWall (direction int, pos Point)
    boolean
  • checkWall (direction int, col int, row int)
    boolean
  • outside (pos Point) boolean

74
Representation of the maze as a data structure
75
Position in a maze
  • Position coordinate (x,y)
  • (number of column, number of line)

76
Movement path of the mouse
  • Path (0,2), (1,2), (1,3), (1,2), (1,1), , (4,0)

77
Movement path of the mouse Long form
  • Detailed description
  • (0,2), step forward to (1,2), turn to right, step
    forward to (1,3), turn to right, turn to left,
    turn to left, turn to left, step forward

78
Representation of the maze store the walls
  • For each relevant position
  • Is there a wall to the south
  • ? boolean sWall
  • Is there a wall to the east
  • ? boolean eWall

79
Representation of the maze store the walls
boolean sWall
boolean eWall
80
South walls of the maze example
  • boolean sWall
  • true, true, true, false,
  • false, false, false, true,
  • false, false, false, false,
  • true, true, true, true

81
South walls of the maze the array
  • boolean sWall
  • true, true, true, false,
  • false, false, false, true,
  • false, false, false, false,
  • true, true, true, true

82
Method outside
public boolean outside (Point pos) return
((pos.x lt 1) //left ... (pos.x gt
width) //right ... (pos.y lt 1) //over
... (pos.y gt height) //under
... ) // the maze
  • API
  • Point pos (x , y)

83
Method checkWall technical detail
Boolean checkWall (int dir, int col, int row)
switch (dir) case NORTH return
sWallrow-1col-1 case SOUTH return
sWallrowcol-1 ...
84
Components separate implementation separate
test
  • Component test
  • Test of the component independent of the rest of
    the system.
  • Integration test
  • Later on, the collaboration with other components
    will be tested.

85
Component test of the maze
  • Component to be tested
  • Testframe for class Maze

class Maze
class MazeTest
86
Test frame basic principles for test output
java MazeTest Start location is (0,2)
x0,y2 Start direction is EAST 1 1 Outside
true true Not outside -gt false false Not
outside -gt false false Wall -gt true true No
wall -gt true true Wall -gt true true
Actual value
Expected value
Semantic interpretation
87
Implementation of the mouse
  • Starting from the class diagram
  • Mouse
  • - Location Point
  • Direction int
  • started boolean
  • - theMaze Maze
  • Mouse (Maze m)
  • getLocation ( ) Point
  • stepForward ( )
  • turnLeft ( )
  • turnRight ( )
  • facingWall ( ) boolean
  • outsideMaze ( ) boolean

88
Communication between the objects send a message
Operation of the mouse
public boolean facingWall() return theMaze.ch
eckWall (direction, location)
Operation of the maze
89
Methods turnLeft and turnRight
public void turnLeft() printoutMove(turn to
the left) direction (direction 3) 4
public void turnRight() printoutMove(turn to
the right) direction (direction 1) 4
90
Method stepForward
public void stepForward() switch (direction)
case NORTH location.y-- break case
EAST location.x break case SOUTH
location.y break case WEST
location.x-- break printoutMove(step
forward)
91
Search algorithmclass MouseMaze (1)
public static void main ( ... ) Maze theMaze
new Maze() Mouse littleMouse new
Mouse(theMaze) printMaze(theMaze) //move the
mouse step by step do makeStep(littleMouse)
while (!littleMouse.outsideMaze())
Moves the mouse one step forward, if necessary
with turns
92
Search algorithmclass MouseMaze (2)
private static void makestep(Mouse m) if
(m.started) if (!m.outsideMaze()) m.turnRig
ht() while (m.facingWall()) m.turnLeft()
m.stepForward() else
m.stepForward() m.started true
Visible attribute
Moves the mouse one step forward, if necessary
with turns
93
Critics of the implementation
  • Constant NORTH 0, repeatedly defined in three
    classes
  • ? source of error
  • ? better solution define them in an interface
    only once
  • Variable started visible outside
  • ? instead of this
  • - private additional access operation
  • - modify the algorithm
  • Data representation of the maze is error-prone
  • true/false sequencies correct ?
  • Variables height, width, size may be in
    contradiction to eWall, sWall
  • ? better let them compute
  • Strategy of the mouse may be a part of the mouse
  • Other mice may have other strategies move at the
    left-hand wall
  • (mice plural of mouse)
Write a Comment
User Comments (0)
About PowerShow.com