p2: exercise 2 discussion -...

54
P2: Exercise 2 Discussion Yannik Dällenbach 8 March 2019 1

Upload: others

Post on 28-Nov-2019

5 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: P2: Exercise 2 Discussion - scg.unibe.chscg.unibe.ch/download/lectures/p2lab/19-03-08-week_03-Assertions_DbC... · Exercise 2 SkipPlayer: Skip the next player’s turn? • Main problem:

P2: Exercise 2 Discussion

Yannik Dällenbach

8 March 2019

!1

Page 2: P2: Exercise 2 Discussion - scg.unibe.chscg.unibe.ch/download/lectures/p2lab/19-03-08-week_03-Assertions_DbC... · Exercise 2 SkipPlayer: Skip the next player’s turn? • Main problem:

Exercise 2

SkipPlayer: Skip the next player’s turn? • Main problem: Find the next player

Approaches• Use boolean flag• Use an ArrayList to get random index• (…more advanced / dynamic solutions possible)

!2

Page 3: P2: Exercise 2 Discussion - scg.unibe.chscg.unibe.ch/download/lectures/p2lab/19-03-08-week_03-Assertions_DbC... · Exercise 2 SkipPlayer: Skip the next player’s turn? • Main problem:

ScrambleUp

ScrambleUp: How to get a random player?• Main problem: Find a random player and exchange.

!3

Page 4: P2: Exercise 2 Discussion - scg.unibe.chscg.unibe.ch/download/lectures/p2lab/19-03-08-week_03-Assertions_DbC... · Exercise 2 SkipPlayer: Skip the next player’s turn? • Main problem:

ScrambleUp

public ISquare landHereOrGoHome() { if (this.isOccupied()) { return game.firstSquare(); } randomPlayer = game.getRandomPlayer();

// … int position = randomPlayer.position();

// …

return game.getSquare(position); }

!4

Page 5: P2: Exercise 2 Discussion - scg.unibe.chscg.unibe.ch/download/lectures/p2lab/19-03-08-week_03-Assertions_DbC... · Exercise 2 SkipPlayer: Skip the next player’s turn? • Main problem:

JavaDoc: Examples

/** * Square that swaps player that landed on it with another * random player. */ public class ScrambleUp extends Square implements ISquare {

// … }

!5

Missing details

Page 6: P2: Exercise 2 Discussion - scg.unibe.chscg.unibe.ch/download/lectures/p2lab/19-03-08-week_03-Assertions_DbC... · Exercise 2 SkipPlayer: Skip the next player’s turn? • Main problem:

JavaDoc: Examples

/** * The class ScrambleUp contains methods for swapping * a player landing on it with another random player. */ public class ScrambleUp extends Square implements ISquare {

// … }

!6

Filler words: „The class ScrambleUp“

Page 7: P2: Exercise 2 Discussion - scg.unibe.chscg.unibe.ch/download/lectures/p2lab/19-03-08-week_03-Assertions_DbC... · Exercise 2 SkipPlayer: Skip the next player’s turn? • Main problem:

JavaDoc: Examples

/** * Swaps entering player with another random selected player. * * Is created and called inside the {@link Game} class. * Extends {@link Square}. * * If there is only one player left in the game, the player * will not swapped. */ public class ScrambleUp extends Square implements ISquare {

// … }

!7

Better

Page 8: P2: Exercise 2 Discussion - scg.unibe.chscg.unibe.ch/download/lectures/p2lab/19-03-08-week_03-Assertions_DbC... · Exercise 2 SkipPlayer: Skip the next player’s turn? • Main problem:

Git

These are not good commit messages:

• No more errors!• I hate git• FIRST TRY• V3• slooowly getting there• …

Page 9: P2: Exercise 2 Discussion - scg.unibe.chscg.unibe.ch/download/lectures/p2lab/19-03-08-week_03-Assertions_DbC... · Exercise 2 SkipPlayer: Skip the next player’s turn? • Main problem:

Git

These are better:

Implemented SwapSquare

Implemented landHereOrGoHome to exchange the current with another random player. Overrides toString method

!9

Page 10: P2: Exercise 2 Discussion - scg.unibe.chscg.unibe.ch/download/lectures/p2lab/19-03-08-week_03-Assertions_DbC... · Exercise 2 SkipPlayer: Skip the next player’s turn? • Main problem:

Design by Contract, Assertions, and Exceptions

!1 0

Page 11: P2: Exercise 2 Discussion - scg.unibe.chscg.unibe.ch/download/lectures/p2lab/19-03-08-week_03-Assertions_DbC... · Exercise 2 SkipPlayer: Skip the next player’s turn? • Main problem:

Exception or Assertion?

/** * Sets the refresh rate for the current display. * @param rate new refresh rate */ public void setRefreshRate(int rate) {

// what if rate < 0? }

!1 1

Page 12: P2: Exercise 2 Discussion - scg.unibe.chscg.unibe.ch/download/lectures/p2lab/19-03-08-week_03-Assertions_DbC... · Exercise 2 SkipPlayer: Skip the next player’s turn? • Main problem:

Exception or Assertion?

/** * Sets the refresh rate for the current display. * @param rate new refresh rate, must be >= 0 */ public void setRefreshRate(int rate) {

assert rate >= 0; }

!1 2

Page 13: P2: Exercise 2 Discussion - scg.unibe.chscg.unibe.ch/download/lectures/p2lab/19-03-08-week_03-Assertions_DbC... · Exercise 2 SkipPlayer: Skip the next player’s turn? • Main problem:

Exception or Assertion?

/** * 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(); }

}

!1 3

Page 14: P2: Exercise 2 Discussion - scg.unibe.chscg.unibe.ch/download/lectures/p2lab/19-03-08-week_03-Assertions_DbC... · Exercise 2 SkipPlayer: Skip the next player’s turn? • Main problem:

Assertions

• Use when you expect a property to hold

• Use for contracts• Pre-/postconditions, invariants

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

holds

!1 4

Page 15: P2: Exercise 2 Discussion - scg.unibe.chscg.unibe.ch/download/lectures/p2lab/19-03-08-week_03-Assertions_DbC... · Exercise 2 SkipPlayer: Skip the next player’s turn? • Main problem:

Assertions

/** * 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; // Implementation omitted assert(invariant());

}

!1 5

Page 16: P2: Exercise 2 Discussion - scg.unibe.chscg.unibe.ch/download/lectures/p2lab/19-03-08-week_03-Assertions_DbC... · Exercise 2 SkipPlayer: Skip the next player’s turn? • Main problem:

Assertions

• Favor assertions/preconditions for checking method parameters in private/internal API • Senders come from within your project => go fix the bug!• Simplifies design

• Use assertions for postconditions and invariants

!1 6

Page 17: P2: Exercise 2 Discussion - scg.unibe.chscg.unibe.ch/download/lectures/p2lab/19-03-08-week_03-Assertions_DbC... · Exercise 2 SkipPlayer: Skip the next player’s turn? • Main problem:

Exceptions

• Error handling• Expected behavior

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

!1 7

public void matches(String filename) throws NotImplementedException {

throw new NotImplementedException();

}

Page 18: P2: Exercise 2 Discussion - scg.unibe.chscg.unibe.ch/download/lectures/p2lab/19-03-08-week_03-Assertions_DbC... · Exercise 2 SkipPlayer: Skip the next player’s turn? • Main problem:

Exceptions

Do not abuse exceptions

!1 8

try { int index = 0; while (true) {

players[index++] = new Player(); }

} catch (ArrayIndexOutOfBoundsException e) {}

Page 19: P2: Exercise 2 Discussion - scg.unibe.chscg.unibe.ch/download/lectures/p2lab/19-03-08-week_03-Assertions_DbC... · Exercise 2 SkipPlayer: Skip the next player’s turn? • Main problem:

Exceptions

Do not abuse exceptions

!1 9

for (int i = 0; i < players.length; i++) { players[i] = new Player();

}

Page 20: P2: Exercise 2 Discussion - scg.unibe.chscg.unibe.ch/download/lectures/p2lab/19-03-08-week_03-Assertions_DbC... · Exercise 2 SkipPlayer: Skip the next player’s turn? • Main problem:

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!

!2 0

Page 21: P2: Exercise 2 Discussion - scg.unibe.chscg.unibe.ch/download/lectures/p2lab/19-03-08-week_03-Assertions_DbC... · Exercise 2 SkipPlayer: Skip the next player’s turn? • Main problem:

Checked and Unchecked Exceptions

• Checked exceptions must either be declared

!2 1

• or wrapped inside a try-catch block

• Use checked exceptions unless you have a very good reason not to!

public void fooBar() throws TodoException { /* … */}

public void fooBar() { try {

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

// handle exception }

}

Page 22: P2: Exercise 2 Discussion - scg.unibe.chscg.unibe.ch/download/lectures/p2lab/19-03-08-week_03-Assertions_DbC... · Exercise 2 SkipPlayer: Skip the next player’s turn? • Main problem:

NullPointerException

• Very common unchecked exception

• Often hard to tell where it came from• Value may be passed around for a while before it is used

=> Include null checks where appropriate

!2 2

Page 23: P2: Exercise 2 Discussion - scg.unibe.chscg.unibe.ch/download/lectures/p2lab/19-03-08-week_03-Assertions_DbC... · Exercise 2 SkipPlayer: Skip the next player’s turn? • Main problem:

NullPointerException

private void newGame() { setPlayer(null); execute();

}

private void setPlayer(Player player) { this.player = player;

}

private void execute() { this.player.move();

}

!2 3

Page 24: P2: Exercise 2 Discussion - scg.unibe.chscg.unibe.ch/download/lectures/p2lab/19-03-08-week_03-Assertions_DbC... · Exercise 2 SkipPlayer: Skip the next player’s turn? • Main problem:

NullPointerException

private void newGame() { setPlayer(null); execute();

}

private void setPlayer(Player player) { this.player = player;

}

private void execute() { this.player.move();

}

!2 4

Exception in thread "main" java.lang.NullPointerException at 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

Why is player == null here?

Page 25: P2: Exercise 2 Discussion - scg.unibe.chscg.unibe.ch/download/lectures/p2lab/19-03-08-week_03-Assertions_DbC... · Exercise 2 SkipPlayer: Skip the next player’s turn? • Main problem:

NullPointerException

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

}

!2 5

Exception in thread "main" java.lang.AssertionError at 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

Page 26: P2: Exercise 2 Discussion - scg.unibe.chscg.unibe.ch/download/lectures/p2lab/19-03-08-week_03-Assertions_DbC... · Exercise 2 SkipPlayer: Skip the next player’s turn? • Main problem:

Another example

/** * 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; }

!2 6

What if the stack is empty?

Page 27: P2: Exercise 2 Discussion - scg.unibe.chscg.unibe.ch/download/lectures/p2lab/19-03-08-week_03-Assertions_DbC... · Exercise 2 SkipPlayer: Skip the next player’s turn? • Main problem:

Another example

/** * 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;

}

!2 7

What if the stack contains null values?

Page 28: P2: Exercise 2 Discussion - scg.unibe.chscg.unibe.ch/download/lectures/p2lab/19-03-08-week_03-Assertions_DbC... · Exercise 2 SkipPlayer: Skip the next player’s turn? • Main problem:

Another example

/** * 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;

}

!2 8

👌

Page 29: P2: Exercise 2 Discussion - scg.unibe.chscg.unibe.ch/download/lectures/p2lab/19-03-08-week_03-Assertions_DbC... · Exercise 2 SkipPlayer: Skip the next player’s turn? • Main problem:

Exercise 3

!2 9

Page 30: P2: Exercise 2 Discussion - scg.unibe.chscg.unibe.ch/download/lectures/p2lab/19-03-08-week_03-Assertions_DbC... · Exercise 2 SkipPlayer: Skip the next player’s turn? • Main problem:

Turtle Game

Demo

!3 0

Page 31: P2: Exercise 2 Discussion - scg.unibe.chscg.unibe.ch/download/lectures/p2lab/19-03-08-week_03-Assertions_DbC... · Exercise 2 SkipPlayer: Skip the next player’s turn? • Main problem:

Turtle Game

• A turtle that moves around a 50x50 board• Move left, right, up, down or jump• Leave a red trail

• Input: String representing a turtle program

right 5 down 4 left 3 jump 20 20 down 10

!3 1

Page 32: P2: Exercise 2 Discussion - scg.unibe.chscg.unibe.ch/download/lectures/p2lab/19-03-08-week_03-Assertions_DbC... · Exercise 2 SkipPlayer: Skip the next player’s turn? • Main problem:

Turtle Game

• You start with• TurtleRenderer: GUI• BoardMaker: Class that gets text from GUI and returns a

boolean array of size 50x50

• You implement• Parse input program (split lines into commands)• Execute turtle actions• Keep track of trail

!3 2

As always: git pull p2-exercises masterRead exercise_03.md

Page 33: P2: Exercise 2 Discussion - scg.unibe.chscg.unibe.ch/download/lectures/p2lab/19-03-08-week_03-Assertions_DbC... · Exercise 2 SkipPlayer: Skip the next player’s turn? • Main problem:

Introduction to UMLMathias Stocker

Page 34: P2: Exercise 2 Discussion - scg.unibe.chscg.unibe.ch/download/lectures/p2lab/19-03-08-week_03-Assertions_DbC... · Exercise 2 SkipPlayer: Skip the next player’s turn? • Main problem:

Main areas of application

•Documentation•Drafts

34

Page 35: P2: Exercise 2 Discussion - scg.unibe.chscg.unibe.ch/download/lectures/p2lab/19-03-08-week_03-Assertions_DbC... · Exercise 2 SkipPlayer: Skip the next player’s turn? • Main problem:

Documentation

•Can be done automatically•Can be an «overkill»

35

Page 36: P2: Exercise 2 Discussion - scg.unibe.chscg.unibe.ch/download/lectures/p2lab/19-03-08-week_03-Assertions_DbC... · Exercise 2 SkipPlayer: Skip the next player’s turn? • Main problem:

Documentation

source: java-player.sourceforge.net

36

Page 37: P2: Exercise 2 Discussion - scg.unibe.chscg.unibe.ch/download/lectures/p2lab/19-03-08-week_03-Assertions_DbC... · Exercise 2 SkipPlayer: Skip the next player’s turn? • Main problem:

A draft helps you to…

… simplify reality… unterstanding 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

37

Page 38: P2: Exercise 2 Discussion - scg.unibe.chscg.unibe.ch/download/lectures/p2lab/19-03-08-week_03-Assertions_DbC... · Exercise 2 SkipPlayer: Skip the next player’s turn? • Main problem:

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

Modeling your system…

38

Page 39: P2: Exercise 2 Discussion - scg.unibe.chscg.unibe.ch/download/lectures/p2lab/19-03-08-week_03-Assertions_DbC... · Exercise 2 SkipPlayer: Skip the next player’s turn? • Main problem:

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

Modeling your system…

39

Page 40: P2: Exercise 2 Discussion - scg.unibe.chscg.unibe.ch/download/lectures/p2lab/19-03-08-week_03-Assertions_DbC... · Exercise 2 SkipPlayer: Skip the next player’s turn? • Main problem:

Class diagram

40

Page 41: P2: Exercise 2 Discussion - scg.unibe.chscg.unibe.ch/download/lectures/p2lab/19-03-08-week_03-Assertions_DbC... · Exercise 2 SkipPlayer: Skip the next player’s turn? • Main problem:

Classes and Interfaces

41

Page 42: P2: Exercise 2 Discussion - scg.unibe.chscg.unibe.ch/download/lectures/p2lab/19-03-08-week_03-Assertions_DbC... · Exercise 2 SkipPlayer: Skip the next player’s turn? • Main problem:

Classes and Interfaces

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

AttributesacessIdentifier : typeExample: - size: int

MethodsaccessIdentifier(parameter: type): returnType

42

Page 43: P2: Exercise 2 Discussion - scg.unibe.chscg.unibe.ch/download/lectures/p2lab/19-03-08-week_03-Assertions_DbC... · Exercise 2 SkipPlayer: Skip the next player’s turn? • Main problem:

Implementation and extension

43

Page 44: P2: Exercise 2 Discussion - scg.unibe.chscg.unibe.ch/download/lectures/p2lab/19-03-08-week_03-Assertions_DbC... · Exercise 2 SkipPlayer: Skip the next player’s turn? • Main problem:

Dependency

44

Page 45: P2: Exercise 2 Discussion - scg.unibe.chscg.unibe.ch/download/lectures/p2lab/19-03-08-week_03-Assertions_DbC... · Exercise 2 SkipPlayer: Skip the next player’s turn? • Main problem:

Aggregation vs. Composition

45

Page 46: P2: Exercise 2 Discussion - scg.unibe.chscg.unibe.ch/download/lectures/p2lab/19-03-08-week_03-Assertions_DbC... · Exercise 2 SkipPlayer: Skip the next player’s turn? • Main problem:

Sequence diagram

46

Page 47: P2: Exercise 2 Discussion - scg.unibe.chscg.unibe.ch/download/lectures/p2lab/19-03-08-week_03-Assertions_DbC... · Exercise 2 SkipPlayer: Skip the next player’s turn? • Main problem:

Sequence diagram

47

Page 48: P2: Exercise 2 Discussion - scg.unibe.chscg.unibe.ch/download/lectures/p2lab/19-03-08-week_03-Assertions_DbC... · Exercise 2 SkipPlayer: Skip the next player’s turn? • Main problem:

Sequence diagram

48

Page 49: P2: Exercise 2 Discussion - scg.unibe.chscg.unibe.ch/download/lectures/p2lab/19-03-08-week_03-Assertions_DbC... · Exercise 2 SkipPlayer: Skip the next player’s turn? • Main problem:

Keep in mind

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

rest

49

Page 50: P2: Exercise 2 Discussion - scg.unibe.chscg.unibe.ch/download/lectures/p2lab/19-03-08-week_03-Assertions_DbC... · Exercise 2 SkipPlayer: Skip the next player’s turn? • Main problem:

On paper: Not enough information

50

Page 51: P2: Exercise 2 Discussion - scg.unibe.chscg.unibe.ch/download/lectures/p2lab/19-03-08-week_03-Assertions_DbC... · Exercise 2 SkipPlayer: Skip the next player’s turn? • Main problem:

On paper: Too much information

51

Page 52: P2: Exercise 2 Discussion - scg.unibe.chscg.unibe.ch/download/lectures/p2lab/19-03-08-week_03-Assertions_DbC... · Exercise 2 SkipPlayer: Skip the next player’s turn? • Main problem:

On paper

52

Page 53: P2: Exercise 2 Discussion - scg.unibe.chscg.unibe.ch/download/lectures/p2lab/19-03-08-week_03-Assertions_DbC... · Exercise 2 SkipPlayer: Skip the next player’s turn? • Main problem:

Exercise 3

Use the information from the lecture and from this presentation to solve the UML related tasks in Exercise 3

Add both diagrams in a common format (e.g. JPG, PDF) to the exercise root in your group folder.

If you do not have a scanner, you can just take a photo of the UML diagrams with a smartphone.

53

Page 54: P2: Exercise 2 Discussion - scg.unibe.chscg.unibe.ch/download/lectures/p2lab/19-03-08-week_03-Assertions_DbC... · Exercise 2 SkipPlayer: Skip the next player’s turn? • Main problem:

To learn more

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

• Book: UML Distilled, Martin Fowler

54