Felix Grund
Instructor @ UBC
class Pump {
public static final int COST_PER_LITRE = 1.45;
// REQUIRES: 10 * COST_PER_LITRE <= amount
// <= 30 * COST_PER_LITRE
// EFFECTS: payment is recorded
void prePay(int amount) {
// stub
}
// EFFECTS: returns at least 10 litres of gas
int dispense() {
return 0; // stub
}
}
class APump extends Pump {
// REQUIRES: 5 * COST_PER_LITRE <= amount
// <= 30 * COST_PER_LITRE
// EFFECTS: payment is recorded
void prePay(int amount) {
// stub
}
// EFFECTS: returns at least 5 litres of gas
int dispense() {
return 0; // stub
}
}
class BPump extends Pump {
// REQUIRES: 20 * COST_PER_LITRE <= amount
// <= 30 * COST_PER_LITRE
// EFFECTS: payment is recorded
void prePay(int amount) {
// stub
}
// EFFECTS: returns at least 20 litres of gas
int dispense() {
return 0; // stub
}
}
Is precondition narrower?
Is postcondition wider?
Is LSP violated?
A
B
C
N-N-N
N-Y-Y
Y-N-Y
N-N-N
No good substitute if LSP is violated!
?-?-?
?-?-?
?-?-?
?-?-?
//EFFECTS: returns a speed of 1.0, 1.5, 2.0, 2.5 or 3.0
public double computeSpeed() {
return 1.0;// stub
}
Is postcondition wider?
N
N
Y
Y
//EFFECTS: returns a speed of 1.0, 2.0 or 3.0
1
//EFFECTS: returns a speed in the range 1.0 to 1.5 or in the range 2.5 to 3.0
2
//EFFECTS: returns a speed of 0.0, 1.0, 2.0 or 3.0
3
//EFFECTS: returns a speed of 1.5 or 2.5
4
//REQUIRES: choice is one of "a", "b", "c" or "d"
//EFFECTS: (omitted)
public void handleChoice(String choice) {
// stub
}
Is precondition narrower?
//REQUIRES: choice is one of "A", "B", "C" or "D"
1
//REQUIRES: choice is one of "a", "b", "c", "d", "e"
2
//REQUIRES: choice is one of "b", "c", "d", "e", "f", "g"
3
//REQUIRES: choice is one of "a", "A", "b", "B", "c", "C", "d" or "D"
4
//REQUIRES: choice is one of "a", "b", "c"
5
N
Y
N
Y
Y
By Felix Grund