DAAD project “Joint Course on OOP using Java” Humboldt University Berlin, University of Novi Sad, ‘Polytehnica’ University of Timisoara, University of.

Slides:



Advertisements
Similar presentations
1 CS101 Introduction to Computing Lecture 17 Algorithms II.
Advertisements

JAVA Coursework (the same for 2A and 2B). Fundamental Information The coursework is 30 marks in your O’Level = 15% of the exam Must be word processed.
COMPSCI 105 S Principles of Computer Science 12 Abstract Data Type.
Introduction To System Analysis and Design
Algorithms and Problem Solving-1 Algorithms and Problem Solving.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Software Development Software Life Cycle UML Diagrams.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. slide 1 CS 125 Introduction to Computers and Object- Oriented Programming.
Algorithms and Problem Solving. Learn about problem solving skills Explore the algorithmic approach for problem solving Learn about algorithm development.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. COMPSCI 125 Introduction to Computer Science I.
Introduction to Software Design Chapter 1. Chapter 1: Introduction to Software Design2 Chapter Objectives To become familiar with the software challenge.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. slide 1 CS 125 Introduction to Computers and Object- Oriented Programming.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 1 Introduction to Object-Oriented Programming and Software Development.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. COMPSCI 125 Introduction to Computer Science I.
Chapter Chapter 1 Introduction to Object-Oriented Programming and Software Development.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 1 Introduction to Object-Oriented Programming and Software Development.
© Copyright Eliyahu Brutman Programming Techniques Course.
Chapter 1 Program Design
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 1 Introduction to Object-Oriented Programming and Software Development.
Introduction to Software Design Chapter 1. Chapter 1: Introduction to Software Design2 Chapter Objectives To become familiar with the software challenge.
1 OO Java, Baile Herculane, Romania, 2005 OO Java Requirements Specification - Produce highly adaptable teaching materials - 1 st step: collect all useful.
Basic Concepts The Unified Modeling Language (UML) SYSC System Analysis and Design.
SuperCorners. Problem The Corners sample robot has a simple strategy: first, move into a corner of the arena, and second sweep the gun back and forth.
Software Engineering 2003 Jyrki Nummenmaa 1 USE CASES In this lecture: Use cases - What are use cases? - Why to use use cases? - How to write.
Introduction To System Analysis and design
1 6th Workshop "SEERE", Ravda, September 2006 Evolution of the MouseMaze case study and its assessment M. Ivanovic, K. Bothe.
Abstract Data Types (ADTs) and data structures: terminology and definitions A type is a collection of values. For example, the boolean type consists of.
Object-Oriented Software Engineering Practical Software Development using UML and Java Chapter 8: Modelling Interactions and Behaviour.
Introduction to Object-oriented programming and software development Lecture 1.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 1 Introduction to Object-Oriented Programming and.
BCS 2143 Introduction to Object Oriented and Software Development.
Object Oriented Analysis and Design Introduction.
Chapter 3 Introduction to Collections – Stacks Modified
Understand Application Lifecycle Management
© The McGraw-Hill Companies, 2006 Chapter 4 Implementing methods.
Chapter 1 What is Programming? Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition) by S.N. Kamin, D. Mickunas, E.
Putting together a complete system Chapter 10. Overview  Design a modest but complete system  A collection of objects work together to solve a problem.
Introduction To System Analysis and Design
Object-Oriented Analysis and Design An Introduction.
By the end of this session you should be able to...
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 5: Software Design & Testing; Revision Session.
Object-Oriented Analysis and Design Fall 2009.
Data Structures Using C++1 Chapter 1 -Software Engineering Principles -ADT and Classes.
DAAD project “Joint Course on OOP using Java” Humboldt University Berlin, University of Novi Sad, ‘Polytehnica’ University of Timisoara, University of.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
1 FUNCTIONS - I Chapter 5 Functions help us write more complex programs.
1 CSCD 326 Data Structures I Software Design. 2 The Software Life Cycle 1. Specification 2. Design 3. Risk Analysis 4. Verification 5. Coding 6. Testing.
1 6th Workshop "SEERE", Ravda, September 2006 OOJava course materials – what happened during last year? M. Ivanovic, K. Bothe.
Data Structures Using C++ 2E
Chapter 5 Classes and Methods II Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition) by S.N. Kamin, D. Mickunas, E.
Algorithms Java Methods A & AB Object-Oriented Programming and Data Structures Maria Litvin ● Gary Litvin Copyright © 2006 by Maria Litvin, Gary Litvin,
Chapter 5 Classes and Methods II Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition) by S.N. Kamin, D. Mickunas, E.
Objective You will be able to define the basic concepts of object-oriented programming with emphasis on objects and classes by taking notes, seeing examples,
Chapter 3 AS3 Programming. Introduction Algorithms + data structure =programs Why this formula relevant to application programs created in flash? The.
Chapter 7 Classes and Methods III: Static Methods and Variables Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition)
CSCI 383 Object-Oriented Programming & Design Lecture 7 Martin van Bommel.
Session 7 More Implications of Inheritance & Chapter 5: Ball World Example.
DAAD project “Joint Course on OOP using Java” Humboldt University Berlin, University of Novi Sad, ‘Polytehnica’ University of Timisoara, University of.
Algorithms and Problem Solving. Learn about problem solving skills Explore the algorithmic approach for problem solving Learn about algorithm development.
Object Oriented Analysis & Design By Rashid Mahmood.
Algorithms and Problem Solving
Chapter 1 OBJECT-ORIENTED ANALYSIS AND DESIGN
About the Presentations
Lecture 2 Introduction to Programming
Programming Fundamentals (750113) Ch1. Problem Solving
The non-trivial Java example ‘Mouse in a Maze’
Algorithms and Problem Solving
Programming Fundamentals (750113) Ch1. Problem Solving
Programming Fundamentals (750113) Ch1. Problem Solving
A type is a collection of values
Presentation transcript:

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-trivial Java example ‘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 Based on an idea from S.N. Kamin, M.D. Mickunas, E.M. Reingold: „An introduction to computer science – Using Java“, McGraw-Hill, 1998

DAAD project „Joint Course on OOP using Java“ © 3 Points of this Java sample program Introduce a non- trivial Java program Illustrate a methodology how to present larger Java programs in lectures Demonstrate the importance of software engineering principles for ordinary Java program development Interactive style of the lecture improves learning effects Using Java API For the lecturer only

DAAD project „Joint Course on OOP using Java“ © 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) For the lecturer only ClassesLOC Mouse61 Maze55 MouseMaze64 Easel23 SoftFrame135 MazeTest40

DAAD project „Joint Course on OOP using Java“ © 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. For the lecturer only

DAAD project „Joint Course on OOP using Java“ © 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 For the lecturer only

DAAD project „Joint Course on OOP using Java“ © 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. Next steps? What to do?

DAAD project „Joint Course on OOP using Java“ © 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 %

DAAD project „Joint Course on OOP using Java“ © 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 project’s failure.

DAAD project „Joint Course on OOP using Java“ © 10 Development process of ‚Mouse in a Maze‘ Requirements analysis Design Implementation and test

DAAD project „Joint Course on OOP using Java“ © 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. Open questions ?

DAAD project „Joint Course on OOP using Java“ © 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?

DAAD project „Joint Course on OOP using Java“ © 13 How does a maze look like?

DAAD project „Joint Course on OOP using Java“ © 14 Examples of our kind of mazes

DAAD project „Joint Course on OOP using Java“ © 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:

DAAD project „Joint Course on OOP using Java“ © 16 How does a mouse look like?

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

DAAD project „Joint Course on OOP using Java“ © 18 Mouse confronted with different mazes entryexit entry exit This will become hard  No problem

DAAD project „Joint Course on OOP using Java“ © 19 Which kinds of movements is our mouse able to do?  step forward  turn left  turn right

DAAD project „Joint Course on OOP using Java“ © 20 Possible movements of the mouse entry exit May turn left or right by 90 degrees May move one step forward into the adjacent room in the line of vision

DAAD project „Joint Course on OOP using Java“ © 21 entry exit Two implemented solutions 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 Requirements specification is incomplete 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

DAAD project „Joint Course on OOP using Java“ © 23 Perceptions of the mouse entry exit Line of vision only straight 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 Field of view of a mouse

DAAD project „Joint Course on OOP using Java“ © 25 The problem may be huge The problem may be huge

DAAD project „Joint Course on OOP using Java“ © 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.

DAAD project „Joint Course on OOP using Java“ © 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

DAAD project „Joint Course on OOP using Java“ © 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: Requirements Specification (3) entryexit

DAAD project „Joint Course on OOP using Java“ © 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 (4) entryexit

DAAD project „Joint Course on OOP using Java“ © 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: Requirements Specification (5) entry

DAAD project „Joint Course on OOP using Java“ © 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.

DAAD project „Joint Course on OOP using Java“ © 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?

DAAD project „Joint Course on OOP using Java“ © 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.

DAAD project „Joint Course on OOP using Java“ © 34 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:

DAAD project „Joint Course on OOP using Java“ © 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 Impossible:

DAAD project „Joint Course on OOP using Java“ © 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.

DAAD project „Joint Course on OOP using Java“ © 37 Mouse movements according to the algorithm: first example entryexit

DAAD project „Joint Course on OOP using Java“ © 38 Mouse movements according to the algorithm in detail: first example entryexit

DAAD project „Joint Course on OOP using Java“ © 39 Mouse movements according to the algorithm: second example entry exit

DAAD project „Joint Course on OOP using Java“ © 40 Mouse movements according to the algorithm in detail: second example entry exit

DAAD project „Joint Course on OOP using Java“ © 41 Mouse movements: graphical and textual output entryexit step forward turn right step forward turn right turn left step forward turn right turn left step forward turn right step forward

DAAD project „Joint Course on OOP using Java“ © 42 Textual output of the solution xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx x x x x x xxxxxxxxxxxxxx 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 step forward …

DAAD project „Joint Course on OOP using Java“ © 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

DAAD project „Joint Course on OOP using Java“ © 44 Development process of ‚Mouse in a Maze‘ Requirements analysis Design Implementation and test

DAAD project „Joint Course on OOP using Java“ © 45 Task 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 architecture  SW architecture description languages: UML  Our task: define an object-oriented architecture  Main problem: How to find classes?

DAAD project „Joint Course on OOP using Java“ © 47 Software development: Phases and results  Analysis & Definition  Requirements specification  Design  Software architecture  Implementation  Program  Test  Test cases

DAAD project „Joint Course on OOP using Java“ © 48 As a matter of fact … Finding a proper class structure may be more challenging then implementing the Java program code.

DAAD project „Joint Course on OOP using Java“ © 49 Software 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 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.

DAAD project „Joint Course on OOP using Java“ © 51 UML 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 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?

DAAD project „Joint Course on OOP using Java“ © 53 The usable interface of a class: only 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: Associations, inheritance, … Class 1Class 2 Class 1Class 2 Association Directed association: objects of class one know objects of class two – and not vice versa

DAAD project „Joint Course on OOP using Java“ © 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 Principles ?

DAAD project „Joint Course on OOP using Java“ © 56 Case study: ‘Mouse 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

DAAD project „Joint Course on OOP using Java“ © 57 Which relations exist between the components? Mouse Maze 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? MouseMaze Algorithm for mouse movement ? ? ? e.g. test if in front of a wall no Alg. has not to know the maze User interface /output

DAAD project „Joint Course on OOP using Java“ © 59 Class 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: Which data, which operations? (first approach) Mouse -Location : Point -Direction : int stepForward ( ) turnLeft ( ) turnRight ( ) facingWall ( ) : boolean outsideMaze ( ) : boolean State: place / direction in a maze Behaviour: abilities of the mouse (movements & perceptions)

DAAD project „Joint Course on OOP using Java“ © 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 Current position of the mouse Visible attribute

DAAD project „Joint Course on OOP using Java“ © 62 Interface 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”)?

DAAD project „Joint Course on OOP using Java“ © 63 Interface of the maze (first approach) Maze outside (pos : Point) : boolean checkWall (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 ? Maze outside (pos : Point) : boolean checkWall (direction : int, pos : Point) : boolean getStartLocation ( ) : ? The representation of the maze is too complex to be included as attributes (array)

DAAD project „Joint Course on OOP using Java“ © 65 Detailed interface of the maze 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 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: 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 Algorithm for mouse movement main ( ) Textual output of the maze printMaze ( )

DAAD project „Joint Course on OOP using Java“ © 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.

DAAD project „Joint Course on OOP using Java“ © 68 Development process of ‚Mouse in a Maze‘ Requirements analysis Design Implementation and test

DAAD project „Joint Course on OOP using Java“ © 69 Java sources ClassesLOC Mouse61 Maze55 MouseMaze64 Easel23 SoftFrame135 MazeTest40 Sum:378

DAAD project „Joint Course on OOP using Java“ © 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.

DAAD project „Joint Course on OOP using Java“ © 71 Planning of implementation steps  UML class diagram: Dependencies  order of implementation MouseMaze Algorithm for mouse movement User interface: Textual output of the maze 1 2 3a 3b In which order should the classes be implemented?

DAAD project „Joint Course on OOP using Java“ © 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 Algorithm for mouse movement main ( ) Textual output of the maze printMaze ( ) 1 2 3a 3b In the lecture: 1, 2, 3b (main ideas) Self-study: 3a; 1, 2, 3b (details)

DAAD project „Joint Course on OOP using Java“ © 73 Implementation of the maze 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  Starting from the class diagram

DAAD project „Joint Course on OOP using Java“ © 74 Representation of the maze as a data structure How to represent the maze by Java data structures?

DAAD project „Joint Course on OOP using Java“ © 75 (0,0)(1,0)(2,0)(3,0)(4,0)(5,0) (0,1)(1,1)(2,1)(3,1)(4,1)(5,1) (0,2)(1,2)(2,2)(3,2)(4,2)(5,2) (0,3)(1,3)(2,3)(3,3)(4,3)(5,3) (0,4)(1,4)(2,4)(3,4)(4,4)(5,4) 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 mouse Path: (0,2), (1,2), (1,3), (1,2), (1,1), …, (4,0) (0,0)(1,0)(2,0)(3,0)(4,0)(5,0) (0,1)(1,1)(2,1)(3,1)(4,1)(5,1) (0,2)(1,2)(2,2)(3,2)(4,2)(5,2) (0,3)(1,3)(2,3)(3,3)(4,3)(5,3) (0,4)(1,4)(2,4)(3,4)(4,4)(5,4)

DAAD project „Joint Course on OOP using Java“ © 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 … (0,0)(1,0)(2,0)(3,0)(4,0)(5,0) (0,1)(1,1)(2,1)(3,1)(4,1)(5,1) (0,2)(1,2)(2,2)(3,2)(4,2)(5,2) (0,3)(1,3)(2,3)(3,3)(4,3)(5,3) (0,4)(1,4)(2,4)(3,4)(4,4)(5,4)

DAAD project „Joint Course on OOP using Java“ © 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

DAAD project „Joint Course on OOP using Java“ © 79 Representation of the maze: store the walls (0,0)(1,0)(2,0)(3,0)(4,0)(5,0) (0,1)(1,1)(2,1)(3,1)(4,1)(5,1) (0,2)(1,2)(2,2)(3,2)(4,2)(5,2) (0,3)(1,3)(2,3)(3,3)(4,3)(5,3) (0,4)(1,4)(2,4)(3,4)(4,4)(5,4) 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: example (0,0)(1,0)(2,0)(3,0)(4,0)(5,0) (0,1)(1,1)(2,1)(3,1)(4,1)(5,1) (0,2)(1,2)(2,2)(3,2)(4,2)(5,2) (0,3)(1,3)(2,3)(3,3)(4,3)(5,3) (0,4)(1,4)(2,4)(3,4)(4,4)(5,4)

DAAD project „Joint Course on OOP using Java“ © 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}} true false 1 true 2false 3true

DAAD project „Joint Course on OOP using Java“ © 82 Method ‘outside’ 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 }

DAAD project „Joint Course on OOP using Java“ © 83 Method ‘checkWall’: technical detail (0,0)(1,0)(2,0)(3,0)(4,0)(5,0) (0,1)(1,1)(2,1)(3,1)(4,1)(5,1) (0,2)(1,2)(2,2)(3,2)(4,2)(5,2) (0,3)(1,3)(2,3)(3,3)(4,3)(5,3) (0,4)(1,4)(2,4)(3,4)(4,4)(5,4) 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: 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.

DAAD project „Joint Course on OOP using Java“ © 85 Component test of the maze Component to be tested Testframe for class Maze class Maze {… } class MazeTest {… }

DAAD project „Joint Course on OOP using Java“ © 86 Test frame: basic principles for test output % java MazeTest Start location is (0,2): [x=0,y=2] Start direction is EAST = 1: 1 Outside true : true Not outside -> false : false Wall -> true : true No wall -> true : true Wall -> true : true Expected value Actual value Semantic interpretation

DAAD project „Joint Course on OOP using Java“ © 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

DAAD project „Joint Course on OOP using Java“ © 88 Communication between the objects: send 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?’

DAAD project „Joint Course on OOP using Java“ © 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; } Think about these formulas.

DAAD project „Joint Course on OOP using Java“ © 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”); }

DAAD project „Joint Course on OOP using Java“ © 91 Search algorithm: class ‘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

DAAD project „Joint Course on OOP using Java“ © 92 Search algorithm: class ‘MouseMaze’ (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

DAAD project „Joint Course on OOP using Java“ © 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)