the non-trivial java example ‘mouse in a maze’ · humboldt university berlin, university of...

47
1 DAAD project “Joint Course on OOP using Java” Humboldt University Berlin, University of Novi Sad, ‘Polytehnica’ University of Timisoara, University of Plovdiv, University of Belgrade Author: Klaus Bothe Version: Sep. 29, 2004 The non The non- trivial Java example trivial Java example Mouse in a Maze Mouse in a Maze’ A simple software development process from specification, to design, implementation, and test DAAD project „Joint Course on OOP using Java“ © 2 Source 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

Upload: others

Post on 10-Aug-2020

1 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: The non-trivial Java example ‘Mouse in a Maze’ · Humboldt University Berlin, University of Novi Sad, ‘Polytehnica’ University of Timisoara, University of Plovdiv, University

1

DAAD project “Joint Course on OOP using Java”

Humboldt University Berlin, University of Novi Sad, ‘Polytehnica’ University of Timisoara,University of Plovdiv, University of Belgrade

Author: Klaus Bothe Version: Sep. 29, 2004

The nonThe non--trivial Java example trivial Java example ‘‘Mouse in a MazeMouse in a Maze’’

A simple software development process from specification, to design,

implementation, and test

DAAD project „Joint Course on OOP using Java“ © 2

SourceSource

Based on an idea from S.N. Kamin, M.D. Mickunas, E.M. Reingold: „An introduction to computer science – Using Java“, McGraw-Hill, 1998

Page 2: The non-trivial Java example ‘Mouse in a Maze’ · Humboldt University Berlin, University of Novi Sad, ‘Polytehnica’ University of Timisoara, University of Plovdiv, University

2

DAAD project „Joint Course on OOP using Java“ © 3

Points of this Java sample Points of this Java sample programprogram

Introduce a non- trivial Java program

Illustrate a methodology howto present larger Java programs in lectures

Demonstrate theimportance of softwareengineeringprinciples forordinary Java programdevelopment

Interactive style of the lectureimproves learningeffects

Using Java API

For the lecturer

only

DAAD project „Joint Course on OOP using Java“ © 4

CourseCourse materialsmaterials

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

135SoftFrame

61Mouse

40MazeTest

23Easel

64MouseMaze

55Maze

LOCClasses

Page 3: The non-trivial Java example ‘Mouse in a Maze’ · Humboldt University Berlin, University of Novi Sad, ‘Polytehnica’ University of Timisoara, University of Plovdiv, University

3

DAAD project „Joint Course on OOP using Java“ © 5

DidacticalDidactical mistakemistake in in presentingpresenting complexcomplexJava Java programsprograms in in lectureslectures

The lecturer tries to explain each technical detail of program code

non-understandable in a lectureboring

The lecturer starts with a Java program too early

details of requirements specification still opendesign / class structure not well discussed

Finding a proper class structure may be more challengingthen implementing the Java program code.

For the lecturer

only

DAAD project „Joint Course on OOP using Java“ © 6

DidacticalDidactical principlesprinciples in in presentingpresentingcomplexcomplex Java Java programsprograms in in lectureslectures

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 thecode

For the lecturer

only

Page 4: The non-trivial Java example ‘Mouse in a Maze’ · Humboldt University Berlin, University of Novi Sad, ‘Polytehnica’ University of Timisoara, University of Plovdiv, University

4

DAAD project „Joint Course on OOP using Java“ © 7

Requirements analysis: Requirements analysis: ‚‚Mouse in a MazeMouse in a Maze‘‘

Task:Develop a program, that simulates the movement of a mouse in a maze (labyrinth) from an entry to an exit.

Next steps?What to do?

DAAD project „Joint Course on OOP using Java“ © 8

Facts concerning software Facts concerning software development phasesdevelopment 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 %

Page 5: The non-trivial Java example ‘Mouse in a Maze’ · Humboldt University Berlin, University of Novi Sad, ‘Polytehnica’ University of Timisoara, University of Plovdiv, University

5

DAAD project „Joint Course on OOP using Java“ © 9

Learning goalsLearning 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.

DAAD project „Joint Course on OOP using Java“ © 10

DevelopmentDevelopment processprocess of of ‚‚Mouse in a Mouse in a MazeMaze‘‘

Requirements analysis

Design

Implementation and test

Page 6: The non-trivial Java example ‘Mouse in a Maze’ · Humboldt University Berlin, University of Novi Sad, ‘Polytehnica’ University of Timisoara, University of Plovdiv, University

6

DAAD project „Joint Course on OOP using Java“ © 11

Requirements analysis: Requirements analysis: ‚‚Mouse in a MazeMouse 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 ?

DAAD project „Joint Course on OOP using Java“ © 12

Open Open questionsquestions

How does a maze look like?What is a mouse able to do (e.g. whichkinds 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 bepresented?

Page 7: The non-trivial Java example ‘Mouse in a Maze’ · Humboldt University Berlin, University of Novi Sad, ‘Polytehnica’ University of Timisoara, University of Plovdiv, University

7

DAAD project „Joint Course on OOP using Java“ © 13

HowHow doesdoes a a mazemaze look look likelike??

DAAD project „Joint Course on OOP using Java“ © 14

ExamplesExamples of of ourour kindkind of of mazesmazes

Page 8: The non-trivial Java example ‘Mouse in a Maze’ · Humboldt University Berlin, University of Novi Sad, ‘Polytehnica’ University of Timisoara, University of Plovdiv, University

8

DAAD project „Joint Course on OOP using Java“ © 15

RequirementsRequirements specificationspecification (1)(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:

DAAD project „Joint Course on OOP using Java“ © 16

HowHow doesdoes a a mousemouse look look likelike??

Page 9: The non-trivial Java example ‘Mouse in a Maze’ · Humboldt University Berlin, University of Novi Sad, ‘Polytehnica’ University of Timisoara, University of Plovdiv, University

9

DAAD project „Joint Course on OOP using Java“ © 17

OurOur mousemouse

DAAD project „Joint Course on OOP using Java“ © 18

Mouse Mouse confrontedconfronted withwith different different mazesmazes

entry

exit

entry

exit

This will become hard

No problem ☺

Page 10: The non-trivial Java example ‘Mouse in a Maze’ · Humboldt University Berlin, University of Novi Sad, ‘Polytehnica’ University of Timisoara, University of Plovdiv, University

10

DAAD project „Joint Course on OOP using Java“ © 19

WhichWhich kindskinds of of movementsmovements isis ourourmousemouse ableable to do?to do?

step forwardturn leftturn right

DAAD project „Joint Course on OOP using Java“ © 20

PossiblePossible movementsmovements of of thethe mousemouse

entry

exit

May turn left or right by 90 degrees

May move one stepforward into the

adjacent room in theline of vision

Page 11: The non-trivial Java example ‘Mouse in a Maze’ · Humboldt University Berlin, University of Novi Sad, ‘Polytehnica’ University of Timisoara, University of Plovdiv, University

11

DAAD project „Joint Course on OOP using Java“ © 21

entry

exit

TwoTwo implementedimplemented solutionssolutions

entry

exit

Proper solution for the wrong problem.In each case: the shortest path has been found.

Requirements specification is incomplete

What may be suspicious with these

solutions?

DAAD project „Joint Course on OOP using Java“ © 22

RequirementsRequirements specificationspecification isisincompleteincomplete

entry

exit

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.

entry

exit

Page 12: The non-trivial Java example ‘Mouse in a Maze’ · Humboldt University Berlin, University of Novi Sad, ‘Polytehnica’ University of Timisoara, University of Plovdiv, University

12

DAAD project „Joint Course on OOP using Java“ © 23

PerceptionsPerceptions of of thethe mousemouse

entry

exit

Line of vision onlystraight forward

May see only inside of one room (not into the

adjacent room)

May decide: Do I face a wall or an opening (gap)? May decide: Am I inside

or outside the maze?

DAAD project „Joint Course on OOP using Java“ © 24

FieldField of of viewview of a of a mousemouse

Page 13: The non-trivial Java example ‘Mouse in a Maze’ · Humboldt University Berlin, University of Novi Sad, ‘Polytehnica’ University of Timisoara, University of Plovdiv, University

13

DAAD project „Joint Course on OOP using Java“ © 25

TheThe problemproblem maymay bebe hugehuge ☺☺

DAAD project „Joint Course on OOP using Java“ © 26

RequirementsRequirements SpecificationSpecification (2)(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 forwardinto the adjacent room.

b) The mouse is located either in a fixed room inside themaze or just next to the entry or exit. In addition, ithas a fixed line of vision.

c) The mouse can see only in the line of vision. It candecide wether it faces a wall or not.

Page 14: The non-trivial Java example ‘Mouse in a Maze’ · Humboldt University Berlin, University of Novi Sad, ‘Polytehnica’ University of Timisoara, University of Plovdiv, University

14

DAAD project „Joint Course on OOP using Java“ © 27

RequirementsRequirements specificationspecification: : openopen problemsproblems??

Description of the initial state of the mouseDescription of the final state of the mouseDescription of the taskSpecial casesUser interface

DAAD project „Joint Course on OOP using Java“ © 28

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:

RequirementsRequirements SpecificationSpecification (3)(3)

entry

exit

Page 15: The non-trivial Java example ‘Mouse in a Maze’ · Humboldt University Berlin, University of Novi Sad, ‘Polytehnica’ University of Timisoara, University of Plovdiv, University

15

DAAD project „Joint Course on OOP using Java“ © 29

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 theentry to the exit.

Example:

RequirementsRequirements SpecificationSpecification (4)(4)

entry

exit

DAAD project „Joint Course on OOP using Java“ © 30

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 mazethrough the entry.

Example:

RequirementsRequirements SpecificationSpecification (5)(5)

entry

Page 16: The non-trivial Java example ‘Mouse in a Maze’ · Humboldt University Berlin, University of Novi Sad, ‘Polytehnica’ University of Timisoara, University of Plovdiv, University

16

DAAD project „Joint Course on OOP using Java“ © 31

RequirementsRequirements SpecificationSpecification (6)(6)

6. User interfacea) Textual output of the solution:

Example: step forward, turn right, …

b) Graphical user interface:The sequence of movements will be displayed withthe help of a graphical applet, i.e. the steps of themouse are visualized. The user can interactivelytrigger the next step.

DAAD project „Joint Course on OOP using Java“ © 32

SolvabilitySolvability of of thethe problemproblem

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

Page 17: The non-trivial Java example ‘Mouse in a Maze’ · Humboldt University Berlin, University of Novi Sad, ‘Polytehnica’ University of Timisoara, University of Plovdiv, University

17

DAAD project „Joint Course on OOP using Java“ © 33

AlgorithmAlgorithm: : basicbasic principleprinciple

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.

DAAD project „Joint Course on OOP using Java“ © 34

AlgorithmAlgorithm: : as a as a pseudopseudo codecode programprogram

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:

Page 18: The non-trivial Java example ‘Mouse in a Maze’ · Humboldt University Berlin, University of Novi Sad, ‘Polytehnica’ University of Timisoara, University of Plovdiv, University

18

DAAD project „Joint Course on OOP using Java“ © 35

Strong pseudo code: Strong pseudo code: one stepone step

IF (NOT outside the maze?)BEGIN /*do next step*/

turn right;WHILE (facing a wall?) DO

turn left;ENDWHILEstep forward;

END

Impossible:

DAAD project „Joint Course on OOP using Java“ © 36

StrongStrong pseudopseudo codecode: : wholewhole algorithmalgorithm

step forward; /* enter the maze */WHILE(NOT outside the maze?)

BEGIN /*do next step*/turn right;WHILE (facing a wall?) DO

turn left;ENDWHILEstep forward;

ENDENDWHILE

Think about: The algorithm fulfills the principle.

Page 19: The non-trivial Java example ‘Mouse in a Maze’ · Humboldt University Berlin, University of Novi Sad, ‘Polytehnica’ University of Timisoara, University of Plovdiv, University

19

DAAD project „Joint Course on OOP using Java“ © 37

Mouse Mouse movementsmovements accordingaccording to to thethealgorithmalgorithm: : firstfirst exampleexample

entry

exit

DAAD project „Joint Course on OOP using Java“ © 38

Mouse Mouse movementsmovements accordingaccording to to thethealgorithmalgorithm in detail: in detail: firstfirst exampleexample

entry

exit

Page 20: The non-trivial Java example ‘Mouse in a Maze’ · Humboldt University Berlin, University of Novi Sad, ‘Polytehnica’ University of Timisoara, University of Plovdiv, University

20

DAAD project „Joint Course on OOP using Java“ © 39

Mouse Mouse movementsmovements accordingaccording to to thethealgorithmalgorithm: second : second exampleexample

entry

exit

DAAD project „Joint Course on OOP using Java“ © 40

Mouse Mouse movementsmovements accordingaccording to to thethealgorithmalgorithm in detail: second in detail: second exampleexample

entry

exit

Page 21: The non-trivial Java example ‘Mouse in a Maze’ · Humboldt University Berlin, University of Novi Sad, ‘Polytehnica’ University of Timisoara, University of Plovdiv, University

21

DAAD project „Joint Course on OOP using Java“ © 41

Mouse Mouse movementsmovements: : graphicalgraphical and and textualtextual outputoutput

entry

exit

step forwardturn rightstep forwardturn rightturn leftturn leftstep forwardturn rightturn leftturn leftstep forwardturn rightstep forward

DAAD project „Joint Course on OOP using Java“ © 42

Textual output of the solutionTextual output of the solution

xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx xx xx xx xx xx xx xx xx xx xx xx xx x x xxxxxxxxxxxxxx

x x xx x xx x xx x xx x xx x xx x xx x xx x xx x xx x xx x x

x x x x x x x x xx x x xx x x xx x x xx x x xx x x xx x x xx x x xx x x xx x x xx x x xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

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

Page 22: The non-trivial Java example ‘Mouse in a Maze’ · Humboldt University Berlin, University of Novi Sad, ‘Polytehnica’ University of Timisoara, University of Plovdiv, University

22

DAAD project „Joint Course on OOP using Java“ © 43

Mouse in a maze Mouse in a maze –– only a nice game?only a nice game?

Sample of finding algorithms for robots dealing with different tasks:

• Playing soccer• Robot moving on the moon• Rescue robots

DAAD project „Joint Course on OOP using Java“ © 44

DevelopmentDevelopment processprocess of of ‚‚Mouse in a Mouse in a MazeMaze‘‘

Requirements analysis

Design

Implementation and test

Page 23: The non-trivial Java example ‘Mouse in a Maze’ · Humboldt University Berlin, University of Novi Sad, ‘Polytehnica’ University of Timisoara, University of Plovdiv, University

23

DAAD project „Joint Course on OOP using Java“ © 45

TaskTask

Requirements specification

Next step ?

Start with programming?

If so, • with which part/component?• ???

DAAD project „Joint Course on OOP using Java“ © 46

Design: develop the software architectureDesign: develop the software architecture

SW architecture description languages: UML

Our task: define an object-oriented architecture

Main problem: How to find classes?

Page 24: The non-trivial Java example ‘Mouse in a Maze’ · Humboldt University Berlin, University of Novi Sad, ‘Polytehnica’ University of Timisoara, University of Plovdiv, University

24

DAAD project „Joint Course on OOP using Java“ © 47

Software development: Software development: Phases and resultsPhases and results

Analysis & DefinitionRequirements specification

DesignSoftware architecture

ImplementationProgram

TestTest cases

DAAD project „Joint Course on OOP using Java“ © 48

As a matter of fact As a matter of fact ……

Finding a proper class structure maybe more challenging thenimplementing the Java program code.

Page 25: The non-trivial Java example ‘Mouse in a Maze’ · Humboldt University Berlin, University of Novi Sad, ‘Polytehnica’ University of Timisoara, University of Plovdiv, University

25

DAAD project „Joint Course on OOP using Java“ © 49

Software architectureSoftware architecture

Structure of the software:Which components exist?Which relations are between them?

Stack:

Push(e)Pop()Top()

DAAD project „Joint Course on OOP using Java“ © 50

Architecture specification languagesArchitecture specification languages

Graphical languages for the specification of software architectures

Industry standard: UMLUnified 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.

Page 26: The non-trivial Java example ‘Mouse in a Maze’ · Humboldt University Berlin, University of Novi Sad, ‘Polytehnica’ University of Timisoara, University of Plovdiv, University

26

DAAD project „Joint Course on OOP using Java“ © 51

UML classes: the structureUML classes: the structure

Time

- hour : int- minute : int

Time (hour: int, minute : int)addMinutes (Min : int)printTime ( )

Name

Attributes: state

Operations: behaviour

hidden

DAAD project „Joint Course on OOP using Java“ © 52

UML classes as interfacesUML classes as interfaces

Time

- hour : int- minute : int

Time (hour: int, minute : int)addMinutes (Min : int)printTime ( )

UML classes provide an interface: Which information is visible from outside ?

Why are hidden data – which are not visible from outside – part of UML class?

Page 27: The non-trivial Java example ‘Mouse in a Maze’ · Humboldt University Berlin, University of Novi Sad, ‘Polytehnica’ University of Timisoara, University of Plovdiv, University

27

DAAD project „Joint Course on OOP using Java“ © 53

The usable interface of a class: The usable interface of a class: only visible featuresonly visible features

Time

- hour : int- minute : int

Time (hour: int, minute : int)addMinutes (Min : int)printTime ( )

Hidden data support the understanding of the interface. They are part of the class model.

The only way to interact with objects

of this class is to call its methods

DAAD project „Joint Course on OOP using Java“ © 54

Relations between classes: Relations between classes: Associations, inheritance, Associations, inheritance, ……

Class 1 Class 2

Class 1 Class 2

Association

Directed association: objects of class one know objects of class

two – and not vice versa

Page 28: The non-trivial Java example ‘Mouse in a Maze’ · Humboldt University Berlin, University of Novi Sad, ‘Polytehnica’ University of Timisoara, University of Plovdiv, University

28

DAAD project „Joint Course on OOP using Java“ © 55

How to find classes?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

Principles ?

DAAD project „Joint Course on OOP using Java“ © 56

Case study:Case study:‘‘Mouse in a MazeMouse in a Maze’’

Which objects of the problem area should be implemented as component / class of the system?

MouseMaze

Algorithm for mouse movement

User interface /output

Page 29: The non-trivial Java example ‘Mouse in a Maze’ · Humboldt University Berlin, University of Novi Sad, ‘Polytehnica’ University of Timisoara, University of Plovdiv, University

29

DAAD project „Joint Course on OOP using Java“ © 57

Which relations exist between the Which relations exist between the components?components?

MouseMaze

Algorithm for mouse movement

User interface /output

Which relations ?

Who needs whom ?

DAAD project „Joint Course on OOP using Java“ © 58

Which relation: Who needs whom?Which relation: Who needs whom?

Mouse Maze

Algorithm for mouse movement

?

?

?

e.g. test if in front of a wall

no

Alg. has not to know the maze

User interface /output

Page 30: The non-trivial Java example ‘Mouse in a Maze’ · Humboldt University Berlin, University of Novi Sad, ‘Polytehnica’ University of Timisoara, University of Plovdiv, University

30

DAAD project „Joint Course on OOP using Java“ © 59

Class diagram for a mouseClass diagram for a mouse

Mouse

?

?

Which data and which operations characterize the mouse?

DAAD project „Joint Course on OOP using Java“ © 60

Interface of the mouse: Interface of the mouse: Which data, which operations? Which data, which operations?

(first approach)(first approach)

Mouse

- Location : Point- Direction : int

stepForward ( )turnLeft ( )turnRight ( )facingWall ( ) : booleanoutsideMaze ( ) : boolean

State: place / direction in a

maze

Behaviour: abilities of the

mouse (movements & perceptions)

Page 31: The non-trivial Java example ‘Mouse in a Maze’ · Humboldt University Berlin, University of Novi Sad, ‘Polytehnica’ University of Timisoara, University of Plovdiv, University

31

DAAD project „Joint Course on OOP using Java“ © 61

Detailed interface of the mouseDetailed interface of the mouse

Mouse

- Location : Point- Direction : int+ started : boolean- theMaze : Maze

Mouse (Maze m)getLocation ( ) : PointstepForward ( )turnLeft ( )turnRight ( )facingWall ( ) : booleanoutsideMaze ( ) : boolean

Create a mouse in relation to a

particular maze

Current position of the mouse

Visible attribute

DAAD project „Joint Course on OOP using Java“ © 62

Interface of the mazeInterface of the maze

Maze

?

?

Class diagram of the maze: data and operations?

Interface of the maze: Which information is necessary for a user of this class (user = objects of class “Mouse”)?

Page 32: The non-trivial Java example ‘Mouse in a Maze’ · Humboldt University Berlin, University of Novi Sad, ‘Polytehnica’ University of Timisoara, University of Plovdiv, University

32

DAAD project „Joint Course on OOP using Java“ © 63

Interface of the mazeInterface of the maze(first approach)(first approach)

Maze

outside (pos : Point) : booleancheckWall (direction : int, pos : Point) : boolean

getStartLocation ( ) : Point

Where to place the mouse initially? (position & direction)

Is there a wall at position pos in this direction?

Is the current position outside of

the maze?

DAAD project „Joint Course on OOP using Java“ © 64

Interface of the maze: attributes ?Interface of the maze: attributes ?

Maze

outside (pos : Point) : booleancheckWall (direction : int, pos : Point) : boolean

getStartLocation ( ) : ?

The representation of the maze is too complex to be included as attributes (array)

Page 33: The non-trivial Java example ‘Mouse in a Maze’ · Humboldt University Berlin, University of Novi Sad, ‘Polytehnica’ University of Timisoara, University of Plovdiv, University

33

DAAD project „Joint Course on OOP using Java“ © 65

Detailed interface of the mazeDetailed interface of the maze

Maze

- height : int- width : int

getStartLocation ( ) : PointgetStartDirection ( ) : intgetSize ( ) : PointcheckWall (direction : int, pos : Point) : booleancheckWall (direction : int, col : int, row : int) : booleanoutside (pos : Point) : boolean

To find class diagrams is an iterative process: start with a simple solution and extend and modify if necessary.

Detailed maze data are too complex to be included in a class diagram.

The height and width of the maze are useful to draw the maze

Height and width as a point.

DAAD project „Joint Course on OOP using Java“ © 66

Software Architecture: Software Architecture: the whole view (textual output)the whole view (textual output)

Mouse

- Location : Point- Direction : int+ started : boolean- theMaze : Maze

Mouse (Maze m)getLocation ( ) : PointstepForward ( )turnLeft ( )turnRight ( )facingWall ( ) : booleanoutsideMaze ( ) : boolean

Maze

- height : int- width : int

getStartLocation ( ) : PointgetStartDirection ( ) : intgetSize ( ) : PointcheckWall (direction : int, pos : Point) : booleancheckWall (direction : int, col : int, row : int) : booleanoutside (pos : Point) : boolean

Algorithm for mouse movement

main ( )

Textual output of the maze

printMaze ( )

Page 34: The non-trivial Java example ‘Mouse in a Maze’ · Humboldt University Berlin, University of Novi Sad, ‘Polytehnica’ University of Timisoara, University of Plovdiv, University

34

DAAD project „Joint Course on OOP using Java“ © 67

Problems of the development of a Problems of the development of a software architecturesoftware architecture

Software architecture:• Not unique (many good and many bad solutions)

Design of a software architecture does not succeed for the first timeLong process:• Software architecture develops stepwise

Principle: • Start with one preliminary architecture • The usability of the methods finally turns out only

during the implementation.

DAAD project „Joint Course on OOP using Java“ © 68

DevelopmentDevelopment processprocess of of ‚‚Mouse in a Mouse in a MazeMaze‘‘

Requirements analysis

Design

Implementation and test

Page 35: The non-trivial Java example ‘Mouse in a Maze’ · Humboldt University Berlin, University of Novi Sad, ‘Polytehnica’ University of Timisoara, University of Plovdiv, University

35

DAAD project „Joint Course on OOP using Java“ © 69

Java sourcesJava sources

135SoftFrame

61Mouse

40MazeTest

23Easel

64MouseMaze

55Maze

LOCClasses

Sum: 378

DAAD project „Joint Course on OOP using Java“ © 70

Information to Information to thethe audienceaudience

This presentation gives only an outline of the principle points of the implementation.Several crucial technical details will beexplained.However, not each implementation detail will be presented. This would be boring in a lecture.During the self-study and withassignments, everybody should explainoneself open questions.

Page 36: The non-trivial Java example ‘Mouse in a Maze’ · Humboldt University Berlin, University of Novi Sad, ‘Polytehnica’ University of Timisoara, University of Plovdiv, University

36

DAAD project „Joint Course on OOP using Java“ © 71

Planning of implementation steps Planning of implementation steps

UML class diagram:• Dependencies order of implementation

Mouse Maze

Algorithm for mouse movement

User interface:Textual output of the

maze

12

3a3b

In which order should the classes be implemented?

DAAD project „Joint Course on OOP using Java“ © 72

Software Architecture: Software Architecture: the whole viewthe whole view

Mouse

- Location : Point- Direction : int+ started : boolean- theMaze : Maze

Mouse (Maze m)getLocation ( ) : PointstepForward ( )turnLeft ( )turnRight ( )facingWall ( ) : booleanoutsideMaze ( ) : boolean

Maze

- height : int- width : int

getStartLocation ( ) : PointgetStartDirection ( ) : intgetSize ( ) : PointcheckWall (direction : int, pos : Point) : booleancheckWall (direction : int, col : int, row : int) : booleanoutside (pos : Point) : boolean

Algorithm for mouse movement

main ( )

Textual output of the maze

printMaze ( )

12

3a3b

In the lecture: 1, 2, 3b (main ideas) Self-study: 3a; 1, 2, 3b (details)

Page 37: The non-trivial Java example ‘Mouse in a Maze’ · Humboldt University Berlin, University of Novi Sad, ‘Polytehnica’ University of Timisoara, University of Plovdiv, University

37

DAAD project „Joint Course on OOP using Java“ © 73

Implementation of the mazeImplementation of the maze

Maze

- height : int- width : int

getStartLocation ( ) : PointgetStartDirection ( ) : intgetSize ( ) : PointcheckWall (direction : int, pos : Point) : booleancheckWall (direction : int, col : int, row : int) : booleanoutside (pos : Point) : boolean

Starting from the class diagram

DAAD project „Joint Course on OOP using Java“ © 74

Representation of the maze as Representation of the maze as a data structurea data structure

How to represent the mazeby Java data structures?

Page 38: The non-trivial Java example ‘Mouse in a Maze’ · Humboldt University Berlin, University of Novi Sad, ‘Polytehnica’ University of Timisoara, University of Plovdiv, University

38

DAAD project „Joint Course on OOP using Java“ © 75

(5,4)(4,4)(3,4)(2,4)(1,4)(0,4)

(5,3)(4,3)(3,3)(2,3)(1,3)(0,3)

(5,2)(4,2)(3,2)(2,2)(1,2)(0,2)

(5,1)(4,1)(3,1)(2,1)(1,1)(0,1)

(5,0)(4,0)(3,0)(2,0)(1,0)(0,0)

Position in a maze Position in a maze

Position = coordinate (x,y) = (number of column, number of line)

DAAD project „Joint Course on OOP using Java“ © 76

Movement path of the mouseMovement path of the mouse

Path: (0,2), (1,2), (1,3), (1,2), (1,1), …, (4,0)

(5,4)(4,4)(3,4)(2,4)(1,4)(0,4)

(5,3)(4,3)(3,3)(2,3)(1,3)(0,3)

(5,2)(4,2)(3,2)(2,2)(1,2)(0,2)

(5,1)(4,1)(3,1)(2,1)(1,1)(0,1)

(5,0)(4,0)(3,0)(2,0)(1,0)(0,0)

Page 39: The non-trivial Java example ‘Mouse in a Maze’ · Humboldt University Berlin, University of Novi Sad, ‘Polytehnica’ University of Timisoara, University of Plovdiv, University

39

DAAD project „Joint Course on OOP using Java“ © 77

Movement path of the mouse: Movement path of the mouse: Long formLong 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 …

(5,4)(4,4)(3,4)(2,4)(1,4)(0,4)

(5,3)(4,3)(3,3)(2,3)(1,3)(0,3)

(5,2)(4,2)(3,2)(2,2)(1,2)(0,2)

(5,1)(4,1)(3,1)(2,1)(1,1)(0,1)

(5,0)(4,0)(3,0)(2,0)(1,0)(0,0)

DAAD project „Joint Course on OOP using Java“ © 78

Representation of the maze: Representation of the maze: store the wallsstore the walls

For each relevant position:

• Is there a wall to the south boolean [][] sWall

• Is there a wall to the eastboolean [][] eWall

Page 40: The non-trivial Java example ‘Mouse in a Maze’ · Humboldt University Berlin, University of Novi Sad, ‘Polytehnica’ University of Timisoara, University of Plovdiv, University

40

DAAD project „Joint Course on OOP using Java“ © 79

Representation of the maze: Representation of the maze: store the wallsstore the walls

(5,4)(4,4)(3,4)(2,4)(1,4)(0,4)

(5,3)(4,3)(3,3)(2,3)(1,3)(0,3)

(5,2)(4,2)(3,2)(2,2)(1,2)(0,2)

(5,1)(4,1)(3,1)(2,1)(1,1)(0,1)

(5,0)(4,0)(3,0)(2,0)(1,0)(0,0)

boolean [][] sWall

boolean [][] eWall

DAAD project „Joint Course on OOP using Java“ © 80

boolean [][] sWall ={{true, true, true, false},{false, false, false, true},{false, false, false, false},{true, true, true, true}}

South walls of the maze: exampleSouth walls of the maze: example

(5,4)(4,4)(3,4)(2,4)(1,4)(0,4)

(5,3)(4,3)(3,3)(2,3)(1,3)(0,3)

(5,2)(4,2)(3,2)(2,2)(1,2)(0,2)

(5,1)(4,1)(3,1)(2,1)(1,1)(0,1)

(5,0)(4,0)(3,0)(2,0)(1,0)(0,0)

Page 41: The non-trivial Java example ‘Mouse in a Maze’ · Humboldt University Berlin, University of Novi Sad, ‘Polytehnica’ University of Timisoara, University of Plovdiv, University

41

DAAD project „Joint Course on OOP using Java“ © 81

South walls of the maze: the arraySouth walls of the maze: the array

boolean [][] sWall ={{true, true, true, false},{false, false, false, true},{false, false, false, false},{true, true, true, true}}

truetruetruetrue3

falsefalsefalsefalse2

truefalsefalsefalse1

falsetruetruetrue0

3210

DAAD project „Joint Course on OOP using Java“ © 82

Method Method ‘‘outsideoutside’’

API:Point pos : (x , y)

public boolean outside (Point pos){return ((pos.x < 1) //left ...|| (pos.x > width) //right ...|| (pos.y < 1) //over ...|| (pos.y > height) //under ...); // the maze

}

Page 42: The non-trivial Java example ‘Mouse in a Maze’ · Humboldt University Berlin, University of Novi Sad, ‘Polytehnica’ University of Timisoara, University of Plovdiv, University

42

DAAD project „Joint Course on OOP using Java“ © 83

Method Method ‘‘checkWallcheckWall’’: technical detail: technical detail

(5,4)(4,4)(3,4)(2,4)(1,4)(0,4)

(5,3)(4,3)(3,3)(2,3)(1,3)(0,3)

(5,2)(4,2)(3,2)(2,2)(1,2)(0,2)

(5,1)(4,1)(3,1)(2,1)(1,1)(0,1)

(5,0)(4,0)(3,0)(2,0)(1,0)(0,0)

Boolean checkWall(int dir, int col, int row)

{switch (dir){

case NORTH: return sWall[row-1][col-1];

case SOUTH: return sWall[row][col-1];

...

DAAD project „Joint Course on OOP using Java“ © 84

Components: Components: separate implementation & separate testseparate 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.

Page 43: The non-trivial Java example ‘Mouse in a Maze’ · Humboldt University Berlin, University of Novi Sad, ‘Polytehnica’ University of Timisoara, University of Plovdiv, University

43

DAAD project „Joint Course on OOP using Java“ © 85

ComponentComponent test of test of thethe mazemaze

Componentto be tested

Testframe forclass Maze

class Maze {…}

class MazeTest {…}

DAAD project „Joint Course on OOP using Java“ © 86

Test frame: Test frame: basic principles for test outputbasic principles for test output

% java MazeTestStart location is (0,2): [x=0,y=2]Start direction is EAST = 1: 1Outside true : trueNot outside -> false : falseNot outside -> false : falseWall -> true : trueNo wall -> true : trueWall -> true : true

Expected value

Actual value

Semantic interpretation

Page 44: The non-trivial Java example ‘Mouse in a Maze’ · Humboldt University Berlin, University of Novi Sad, ‘Polytehnica’ University of Timisoara, University of Plovdiv, University

44

DAAD project „Joint Course on OOP using Java“ © 87

Implementation of the mouseImplementation of the mouseStarting from the class diagram

Mouse

- Location : Point- Direction : int+ started : boolean- theMaze : Maze

Mouse (Maze m)getLocation ( ) : PointstepForward ( )turnLeft ( )turnRight ( )facingWall ( ) : booleanoutsideMaze ( ) : boolean

DAAD project „Joint Course on OOP using Java“ © 88

Communication between the objects: Communication between the objects: send a messagesend a message

public boolean facingWall() {return

theMaze.checkWall (direction, location);}

Operation of the mouse

Operation of the maze

The mouse sends a message to the maze: ‘If I have a certain location and direction in the maze, is then a wall in front of me?’

Page 45: The non-trivial Java example ‘Mouse in a Maze’ · Humboldt University Berlin, University of Novi Sad, ‘Polytehnica’ University of Timisoara, University of Plovdiv, University

45

DAAD project „Joint Course on OOP using Java“ © 89

Methods: Methods: ‘‘turnLeftturnLeft’’ and and ‘‘turnRightturnRight’’

public void turnLeft() {printoutMove(“turn to the left”);direction = (direction + 3) % 4;

}

public void turnRight() {printoutMove(“turn to the right”);direction = (direction + 1) % 4;

}

Think about these formulas.

DAAD project „Joint Course on OOP using Java“ © 90

Method: Method: ‘‘stepForwardstepForward’’

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”);

}

Page 46: The non-trivial Java example ‘Mouse in a Maze’ · Humboldt University Berlin, University of Novi Sad, ‘Polytehnica’ University of Timisoara, University of Plovdiv, University

46

DAAD project „Joint Course on OOP using Java“ © 91

Search algorithm:Search algorithm:class class ‘‘MouseMazeMouseMaze’’ (1)(1)

public static void main ( ... ) {Maze theMaze = new Maze(); Mouse littleMouse = new Mouse(theMaze);printMaze(theMaze);

//move the mouse step by stepdo{

makeStep(littleMouse);}while (!littleMouse.outsideMaze());

}

Moves the mouse one

step forward, if necessary with turns

DAAD project „Joint Course on OOP using Java“ © 92

Search algorithm:Search algorithm:class class ‘‘MouseMazeMouseMaze’’ (2)(2)

private static void makestep(Mouse m){if (m.started){

if (!m.outsideMaze()){m.turnRight();while (m.facingWall()){

m.turnLeft();}m.stepForward();

}} else {

m.stepForward();m.started = true;

}}

Moves the mouse one

step forward, if necessary with turns

Visible attribute

Page 47: The non-trivial Java example ‘Mouse in a Maze’ · Humboldt University Berlin, University of Novi Sad, ‘Polytehnica’ University of Timisoara, University of Plovdiv, University

47

DAAD project „Joint Course on OOP using Java“ © 93

Critics of the implementationCritics of the implementation

Constant NORTH = 0, … repeatedly defined in three classessource of errorbetter solution: define them in an interface only once

Variable ‘started’ visible outsideinstead 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)