p2: design by contractscg.unibe.ch/download/lectures/p2lab/21-03-12-week_03...2012/03/21  ·...

46
P2: Design By Contract Lorenzo Wipfli 12 March 2021

Upload: others

Post on 03-Sep-2021

1 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: P2: Design By Contractscg.unibe.ch/download/lectures/p2lab/21-03-12-week_03...2012/03/21  · •Scan the UML or take a picture and add them both to your repository as a .pngor .jpg

P2: Design By Contract

Lorenzo Wipfli12 March 2021

Page 2: P2: Design By Contractscg.unibe.ch/download/lectures/p2lab/21-03-12-week_03...2012/03/21  · •Scan the UML or take a picture and add them both to your repository as a .pngor .jpg

Contents

• Feedback Exercise 2• SwapSquare• WormHoleEntrance• SkipSquare• JavaDoc• Git

• Design by Contract• Assertions• Exceptions

• UML• Exercise 3

Page 3: P2: Design By Contractscg.unibe.ch/download/lectures/p2lab/21-03-12-week_03...2012/03/21  · •Scan the UML or take a picture and add them both to your repository as a .pngor .jpg

Exercise 2: SwapSquare

Exercise 2 – DBC, Assertions, Exceptions – UML – Exercise 3 4

Idea:• Ask yourself, does the player stay on this square or not? Where would you placethe logic?• Get the target (or next) Player.• Get the current position of target player.• Move the target player to the swapsquare.• Move the current player to the target player’s square.• Note: Watch out that there is no swapping loop!

Page 4: P2: Design By Contractscg.unibe.ch/download/lectures/p2lab/21-03-12-week_03...2012/03/21  · •Scan the UML or take a picture and add them both to your repository as a .pngor .jpg

Exercise 2: SwapSquare

Exercise 2 – DBC, Assertions, Exceptions – UML – Exercise 3 5

@Overridepublic ISquare landHereOrGoHome() {

if(this.isOccupied())return game.firstSquare();

//logic to prevent infinite swap loop...

//Get the next player to change withPlayer nextPlayer = game.currentPlayer();

//Get square on which that player isISquare changeSquare = nextPlayer.square();

//Tell the next player to move......

return changeSquare.landHereOrGoHome();}

}

Page 5: P2: Design By Contractscg.unibe.ch/download/lectures/p2lab/21-03-12-week_03...2012/03/21  · •Scan the UML or take a picture and add them both to your repository as a .pngor .jpg

Exercise 2: WormholeEntrance

Exercise 2 – DBC, Assertions, Exceptions – UML – Exercise 3 6

Idea:• Ask yourself, does the player stay on this square or not? Where would you place

the logic?• Get all available wormhole exits.• Choose one at random (for example with Random().nextInt(int scope) gives a

number from 0 to scope-1.)• Place the player at the exit.

Page 6: P2: Design By Contractscg.unibe.ch/download/lectures/p2lab/21-03-12-week_03...2012/03/21  · •Scan the UML or take a picture and add them both to your repository as a .pngor .jpg

Exercise 2: SkipSquare

Exercise 2 – DBC, Assertions, Exceptions – UML – Exercise 3 8

Idea:• Ask yourself, does the player stay on this square or not? Where would you place

the logic?• Tell the game to skip the next player.• Use a boolean attribute maybe?

Page 7: P2: Design By Contractscg.unibe.ch/download/lectures/p2lab/21-03-12-week_03...2012/03/21  · •Scan the UML or take a picture and add them both to your repository as a .pngor .jpg

/*** Square that skips the next player.*/public class SkipSquare extends Square implements ISquare {

// …}

JavaDoc: Examples

Exercise 2 – DBC, Assertions, Exceptions – UML – Exercise 3 9

Missing details

Page 8: P2: Design By Contractscg.unibe.ch/download/lectures/p2lab/21-03-12-week_03...2012/03/21  · •Scan the UML or take a picture and add them both to your repository as a .pngor .jpg

JavaDoc: Examples

Exercise 2 – DBC, Assertions, Exceptions – UML – Exercise 3 10

/*** The class SkipSquare contains functionality that* skip the next player.*/public class SkipSquare extends Square implements ISquare {

// …}

Filler words: The class SkipSquare

Page 9: P2: Design By Contractscg.unibe.ch/download/lectures/p2lab/21-03-12-week_03...2012/03/21  · •Scan the UML or take a picture and add them both to your repository as a .pngor .jpg

JavaDoc: Examples

Exercise 2 – DBC, Assertions, Exceptions – UML – Exercise 3 11

/**

* Skips the next player after the current one.

*

* Is created and called inside the {@link Game} class.

* Extends {@link Square}.

*

*/

public class SkipSquare extends Square implements ISquare {

// …

}

Page 10: P2: Design By Contractscg.unibe.ch/download/lectures/p2lab/21-03-12-week_03...2012/03/21  · •Scan the UML or take a picture and add them both to your repository as a .pngor .jpg

Git-messages

Exercise 2 – DBC, Assertions, Exceptions – UML – Exercise 3 12

https://xkcd.com/1296/

Page 11: P2: Design By Contractscg.unibe.ch/download/lectures/p2lab/21-03-12-week_03...2012/03/21  · •Scan the UML or take a picture and add them both to your repository as a .pngor .jpg

Git-messages

• No more errors!• I hate git• test• first try• solving exercise• Here have some code• Changes• Fix• .• Remove if• Do you see this?• I have seen it yes.• Its sunny outside.

Exercise 2 – DBC, Assertions, Exceptions – UML – Exercise 3 13

Page 12: P2: Design By Contractscg.unibe.ch/download/lectures/p2lab/21-03-12-week_03...2012/03/21  · •Scan the UML or take a picture and add them both to your repository as a .pngor .jpg

Git-messages

• Implemented SwapSquare

• Implemented SkipSquare which skips the next player of the current Game.

• Added Player.toString() method.

Exercise 2 – DBC, Assertions, Exceptions – UML – Exercise 3 14

Page 13: P2: Design By Contractscg.unibe.ch/download/lectures/p2lab/21-03-12-week_03...2012/03/21  · •Scan the UML or take a picture and add them both to your repository as a .pngor .jpg

DBC - Example

/**

* Sets the refresh rate for the current display.

* @param rate new refresh rate

*/

public void setRefreshRate(int rate) {

// what if rate < 0?

}

Exercise 2 – DBC, Assertions, Exceptions – UML – Exercise 3 16

Page 14: P2: Design By Contractscg.unibe.ch/download/lectures/p2lab/21-03-12-week_03...2012/03/21  · •Scan the UML or take a picture and add them both to your repository as a .pngor .jpg

DBC - Assertion Example

/**

* Sets the refresh rate for the current display.

* @param rate new refresh rate, must be >= 0

*/

public void setRefreshRate(int rate) {

assert rate >= 0;

}

Exercise 2 – DBC, Assertions, Exceptions – UML – Exercise 3 17

Page 15: P2: Design By Contractscg.unibe.ch/download/lectures/p2lab/21-03-12-week_03...2012/03/21  · •Scan the UML or take a picture and add them both to your repository as a .pngor .jpg

DBC – Exception Example

/**

* Sets the refresh rate for the current display.

*

* @param rate new refresh rate

* @throws IllegalArgumentException if rate is not valid

*/

public void setRefreshRate(int rate) throws IllegalArgumentException {

if (rate < 0) {

throw new IllegalArgumentException();

}

}

Exercise 2 – DBC, Assertions, Exceptions – UML – Exercise 3 18

Page 16: P2: Design By Contractscg.unibe.ch/download/lectures/p2lab/21-03-12-week_03...2012/03/21  · •Scan the UML or take a picture and add them both to your repository as a .pngor .jpg

DBC – When to use Assertions

• Use when you expect a property to hold• Calls inside the program• Use for contracts• Pre-/postconditions, invariants• Simplifies design

• Use inside complex code• For example to make sure an intermediate result holds

Exercise 2 – DBC, Assertions, Exceptions – UML – Exercise 3 19

Page 17: P2: Design By Contractscg.unibe.ch/download/lectures/p2lab/21-03-12-week_03...2012/03/21  · •Scan the UML or take a picture and add them both to your repository as a .pngor .jpg

Assertions – Pre-, and Postconditions

/*** Draw a vertical line, starting from position,* with a length of steps + 1.* * @param position start location of the line, must not be null* @param steps length of the line*/public void drawVertical(Point position, int steps) {

assert position != null; // This is a precondition// Implementation hereassert(invariant()); //This is a postcondition

}

Exercise 2 – DBC, Assertions, Exceptions – UML – Exercise 3 20

Page 18: P2: Design By Contractscg.unibe.ch/download/lectures/p2lab/21-03-12-week_03...2012/03/21  · •Scan the UML or take a picture and add them both to your repository as a .pngor .jpg

DBC – When to use Exceptions

• Favor exceptions for checking method parameters in public/external API• Can’t trust user to read JavaDoc

• Always use exceptions to check user input!

Exercise 2 – DBC, Assertions, Exceptions – UML – Exercise 3 21

Page 19: P2: Design By Contractscg.unibe.ch/download/lectures/p2lab/21-03-12-week_03...2012/03/21  · •Scan the UML or take a picture and add them both to your repository as a .pngor .jpg

Exceptions

• Error handling• Expected behavior• Deal with it in try-catch blocks, or• throw it up to the caller

Exercise 2 – DBC, Assertions, Exceptions – UML – Exercise 3 22

Page 20: P2: Design By Contractscg.unibe.ch/download/lectures/p2lab/21-03-12-week_03...2012/03/21  · •Scan the UML or take a picture and add them both to your repository as a .pngor .jpg

DBC – Checked Exceptions

• Declared Exception

• Wrapped inside a try-catch block

• Always use checked exceptions unless there is a very good reason not to!

Exercise 2 – DBC, Assertions, Exceptions – UML – Exercise 3 23

public void fooBar() {try {

// something that throws a TodoException} catch (TodoException e) {

// handle exception}

}

public void matches(String filename) throws NotImplementedException {}

Page 21: P2: Design By Contractscg.unibe.ch/download/lectures/p2lab/21-03-12-week_03...2012/03/21  · •Scan the UML or take a picture and add them both to your repository as a .pngor .jpg

NullPointerException

• Very common unchecked exception• Often hard to tell where it originated• Value may be passed around for a while before it is used

• Include null checks where appropriate

Exercise 2 – DBC, Assertions, Exceptions – UML – Exercise 3 25

Page 22: P2: Design By Contractscg.unibe.ch/download/lectures/p2lab/21-03-12-week_03...2012/03/21  · •Scan the UML or take a picture and add them both to your repository as a .pngor .jpg

NullPointerException

private void newGame() {

setPlayer(null);

execute();

}

private void setPlayer(Player player) {

this.player = player;

}

private void execute() {

this.player.move();

}Exercise 2 – DBC, Assertions, Exceptions – UML – Exercise 3 26

Exception in thread "main" java.lang.NullPointerExceptionat exercise_03.SomeClass.execute(SomeClass.java:79)at exercise_03.SomeClass.newGame(SomeClass.java:65)at exercise_03.SomeClass.main(SomeClass.java:7) ... Process finished with exit code 1

we do not know why player == null

Page 23: P2: Design By Contractscg.unibe.ch/download/lectures/p2lab/21-03-12-week_03...2012/03/21  · •Scan the UML or take a picture and add them both to your repository as a .pngor .jpg

Exceptions

private void newGame() {

setPlayer(null);

execute();

}

/** @param player must not be null */

private void setPlayer(Player player) {

assert player != null;

this.player = player;

}

private void execute() {

this.player.move();

}Exercise 2 – DBC, Assertions, Exceptions – UML – Exercise 3 27

Exception in thread "main" java.lang.AssertionErrorat exercise_03.SomeClass.setPlayer(SomeClass.java:74)at exercise_03.SomeClass.newGame(SomeClass.java:64)at exercise_03.SomeClass.main(SomeClass.java:7)Process finished with exit code

Stacktrace shows where Nullpointer occured

Page 24: P2: Design By Contractscg.unibe.ch/download/lectures/p2lab/21-03-12-week_03...2012/03/21  · •Scan the UML or take a picture and add them both to your repository as a .pngor .jpg

DBC - Example

Exercise 2 – DBC, Assertions, Exceptions – UML – Exercise 3 28

/*** Look up the object at the top of* this stack and return it.** @return the object at the top*/public E top() {

return top.item; }

Page 25: P2: Design By Contractscg.unibe.ch/download/lectures/p2lab/21-03-12-week_03...2012/03/21  · •Scan the UML or take a picture and add them both to your repository as a .pngor .jpg

DBC - Example

Exercise 2 – DBC, Assertions, Exceptions – UML – Exercise 3 29

/*** Look up the object at the top of* this stack and return it.* Returns null if called on an empty stack.** @return the object at the top*/public E top() {

if (this.isEmpty()) {return null;

}return top.item;

}

Page 26: P2: Design By Contractscg.unibe.ch/download/lectures/p2lab/21-03-12-week_03...2012/03/21  · •Scan the UML or take a picture and add them both to your repository as a .pngor .jpg

/*** Look up the object at the top of* this stack and return it.* @throws EmptyStackException if the stack is empty** @return the object at the top*/public E top() throws EmptyStackException {

if (this.isEmpty()) {throw new EmptyStackException();

}return top.item;

}

DBC - Example

Exercise 2 – DBC, Assertions, Exceptions – UML – Exercise 3 30

Page 27: P2: Design By Contractscg.unibe.ch/download/lectures/p2lab/21-03-12-week_03...2012/03/21  · •Scan the UML or take a picture and add them both to your repository as a .pngor .jpg

UML

• Documentation• Can be done automatically

• Can be an overkill (next slide)

• Drafts• Simplify reality• Understand an existing solution• Deciding how to build something from scratch• Capture requirements and discuss your idea with others• Reduce your effort to test different approaches

Exercise 2 – DBC, Assertions, Exceptions – UML – Exercise 3 31

Page 28: P2: Design By Contractscg.unibe.ch/download/lectures/p2lab/21-03-12-week_03...2012/03/21  · •Scan the UML or take a picture and add them both to your repository as a .pngor .jpg

UML - Documentation

Exercise 2 – DBC, Assertions, Exceptions – UML – Exercise 3 32

Page 29: P2: Design By Contractscg.unibe.ch/download/lectures/p2lab/21-03-12-week_03...2012/03/21  · •Scan the UML or take a picture and add them both to your repository as a .pngor .jpg

UML - Categories

Exercise 2 – DBC, Assertions, Exceptions – UML – Exercise 3 33

structureclass diagram

component diagram

composite structure diagram

object diagram

package diagram

profile diagram

behaviouractivity diagram

comunication diagram

interaction overview diagram

sequence diagram

state machine diagram

timing diagram

Page 30: P2: Design By Contractscg.unibe.ch/download/lectures/p2lab/21-03-12-week_03...2012/03/21  · •Scan the UML or take a picture and add them both to your repository as a .pngor .jpg

UML - Categories

Exercise 2 – DBC, Assertions, Exceptions – UML – Exercise 3 34

structureclass diagram

component diagram

composite structure diagram

object diagram

package diagram

profile diagram

behaviouractivity diagram

comunication diagram

interaction overview diagram

sequence diagram

state machine diagram

timing diagram

Page 31: P2: Design By Contractscg.unibe.ch/download/lectures/p2lab/21-03-12-week_03...2012/03/21  · •Scan the UML or take a picture and add them both to your repository as a .pngor .jpg

UML - Example

Exercise 2 – DBC, Assertions, Exceptions – UML – Exercise 3 35

Page 32: P2: Design By Contractscg.unibe.ch/download/lectures/p2lab/21-03-12-week_03...2012/03/21  · •Scan the UML or take a picture and add them both to your repository as a .pngor .jpg

UML

Exercise 2 – DBC, Assertions, Exceptions – UML – Exercise 3 36

Page 33: P2: Design By Contractscg.unibe.ch/download/lectures/p2lab/21-03-12-week_03...2012/03/21  · •Scan the UML or take a picture and add them both to your repository as a .pngor .jpg

UML – Class annotation

Exercise 2 – DBC, Assertions, Exceptions – UML – Exercise 3 37

Access modifiers:+ public, - private, # protected, static

Attributes:acessIdentifier: typeExample: - size: int

Methods:accessIdentifier(parameter: type): returnType

Page 34: P2: Design By Contractscg.unibe.ch/download/lectures/p2lab/21-03-12-week_03...2012/03/21  · •Scan the UML or take a picture and add them both to your repository as a .pngor .jpg

UML - Relationships

Exercise 2 – DBC, Assertions, Exceptions – UML – Exercise 3 38

Page 35: P2: Design By Contractscg.unibe.ch/download/lectures/p2lab/21-03-12-week_03...2012/03/21  · •Scan the UML or take a picture and add them both to your repository as a .pngor .jpg

UML - Relationships

Exercise 2 – DBC, Assertions, Exceptions – UML – Exercise 3 39

Page 36: P2: Design By Contractscg.unibe.ch/download/lectures/p2lab/21-03-12-week_03...2012/03/21  · •Scan the UML or take a picture and add them both to your repository as a .pngor .jpg

UML – Aggregation vs Composition

Exercise 2 – DBC, Assertions, Exceptions – UML – Exercise 3 40

Page 37: P2: Design By Contractscg.unibe.ch/download/lectures/p2lab/21-03-12-week_03...2012/03/21  · •Scan the UML or take a picture and add them both to your repository as a .pngor .jpg

UML – Sequence Diagramm

Exercise 2 – DBC, Assertions, Exceptions – UML – Exercise 3 41

Page 38: P2: Design By Contractscg.unibe.ch/download/lectures/p2lab/21-03-12-week_03...2012/03/21  · •Scan the UML or take a picture and add them both to your repository as a .pngor .jpg

UML – Sequence Diagramm

Exercise 2 – DBC, Assertions, Exceptions – UML – Exercise 3 42

Page 39: P2: Design By Contractscg.unibe.ch/download/lectures/p2lab/21-03-12-week_03...2012/03/21  · •Scan the UML or take a picture and add them both to your repository as a .pngor .jpg

UML – Sequence Diagramm

Exercise 2 – DBC, Assertions, Exceptions – UML – Exercise 3 43

Page 40: P2: Design By Contractscg.unibe.ch/download/lectures/p2lab/21-03-12-week_03...2012/03/21  · •Scan the UML or take a picture and add them both to your repository as a .pngor .jpg

UML - Tips

•Different aspects, different diagram type•Keep it simple•Focus on what you want to communicate, forget the rest

Exercise 2 – DBC, Assertions, Exceptions – UML – Exercise 3 44

Page 41: P2: Design By Contractscg.unibe.ch/download/lectures/p2lab/21-03-12-week_03...2012/03/21  · •Scan the UML or take a picture and add them both to your repository as a .pngor .jpg

UML - Tips

Exercise 2 – DBC, Assertions, Exceptions – UML – Exercise 3 45

Not enough information

Page 42: P2: Design By Contractscg.unibe.ch/download/lectures/p2lab/21-03-12-week_03...2012/03/21  · •Scan the UML or take a picture and add them both to your repository as a .pngor .jpg

UML - Tips

Exercise 2 – DBC, Assertions, Exceptions – UML – Exercise 3 46

Too much information

Page 43: P2: Design By Contractscg.unibe.ch/download/lectures/p2lab/21-03-12-week_03...2012/03/21  · •Scan the UML or take a picture and add them both to your repository as a .pngor .jpg

UML - Tips

Exercise 2 – DBC, Assertions, Exceptions – UML – Exercise 3 47

Page 44: P2: Design By Contractscg.unibe.ch/download/lectures/p2lab/21-03-12-week_03...2012/03/21  · •Scan the UML or take a picture and add them both to your repository as a .pngor .jpg

Additional Material

• http://scg.unibe.ch/teaching/p2/ (P2 reading material, UML Reference)• Book: UML Distilled, Martin Fowler

Exercise 2 – DBC, Assertions, Exceptions – UML – Exercise 3 48

Page 45: P2: Design By Contractscg.unibe.ch/download/lectures/p2lab/21-03-12-week_03...2012/03/21  · •Scan the UML or take a picture and add them both to your repository as a .pngor .jpg

Exercise 3 - Demo

• A hooman that moves around a 48x48 board• Commands : right, left, up, down• Leaves a trail

• Input: String representing a hooman program, which denotes where he should walk.• Example:

right 5down 4left 3up 10

Exercise 2 – DBC, Assertions, Exceptions – UML – Exercise 3 49

Page 46: P2: Design By Contractscg.unibe.ch/download/lectures/p2lab/21-03-12-week_03...2012/03/21  · •Scan the UML or take a picture and add them both to your repository as a .pngor .jpg

Exercise 3 - Tips

• You start with• CovidRenderer: Handles GUI• Enviroment: Skeleton class that should handle the whole area• Area: Enum types of: SAFE, TRAIL, COVID (Infection hotspots)

• You have to• Parse input program (split lines into commands)• Execute hooman actions and handle incorrect commands.• Parsing and executions should be split!• Keep track of trail and handle quarantines.

• Use the information from the lecture and form these slides to make the two UML diagrams• Scan the UML or take a picture and add them both to your repository as a .png or .jpg

Exercise 2 – DBC, Assertions, Exceptions – UML – Exercise 3 50

• git pull p2-exercises master• Read exercise_03.md• Happy Coding!