930 likes | 943 Views
The non-trivial Java example ‘Mouse in a Maze’. A simple software development process from specification, to design, implementation, and test. 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. For
E N D
The non-trivial Java example ‘Mouse in a Maze’ A simple software development process from specification, to design, implementation, and test
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
For the lecturer only 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
For the lecturer only 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)
For the lecturer only 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.
For the lecturer only 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
Next steps? What to do? 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.
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 %
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 project’s failure.
Development process of ‚Mouse in a Maze‘ Requirements analysis Design Implementation and test
Open questions ? 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.
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?
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:
exit entry entry exit Mouse confronted with different mazes No problem This will become hard
Which kinds of movements is our mouse able to do? • step forward • turn left • turn right
exit entry 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
What may be suspicious with these solutions? exit entry entry exit Two implemented solutions • Proper solution for the wrong problem. • In each case: the shortest path has been found. Requirements specification is incomplete
exit entry entry exit 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.
exit entry 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?
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.
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
entry exit 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:
entry exit 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:
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
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.
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?
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.
Algorithm: as a pseudo code program 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 only one step:
Impossible: 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
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.
entry exit Mouse movements according to the algorithm: first example
entry exit Mouse movements according to the algorithm in detail: first example
Mouse movements according to the algorithm: second example exit entry
Mouse movements according to the algorithm in detail: second example exit entry
entry exit 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
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 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 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 x x x x x x x x x x x x x x x x x x xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx 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 …
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
Development process of ‚Mouse in a Maze‘ Requirements analysis Design Implementation and test
Task Requirements specification Next step ? Start with programming? If so, • with which part/component? • ???
Design: develop the software architecture • SW architecture description languages: UML • Our task: define an object-oriented architecture • Main problem: How to find classes?
Software development: Phases and results • Analysis & Definition • Requirements specification • Design • Software architecture • Implementation Program • Test • Test cases
As a matter of fact … Finding a proper class structure may be more challenging then implementing the Java program code.
Software architecture Structure of the software: • Which components exist? • Which relations are between them? Stack: Push(e) Pop() Top()
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.