Prof. Dr. A. Poetzsch-Heffter Dipl.-Inform. Kathrin Geilmann
University of Kaiserslautern Department of Computer Science Software Technology Group
Advanced Aspects of Object-Oriented Programming (SS 2011) Practice Sheet 9
Date of Issue: 07.06.11Deadline: 21.06.11 (until 10 a.m. as PDF via E-Mail)
Exercise 1 Questions
a) Add one or more questions to the document athttp://bit.ly/lGbs2v.
Exercise 2 Abstraction
a) Explain the necessity for JML’s concept ofmodel fields. Why are these especially important in the context of interface specifications?
b) Specify the followingQueueinterface according to the „well-known behaviour“ of this data structure.Hint: Look which model provided by JML may be appropriate.
p u b l i c i n t e r f a c e Queue {
O b j e c t p e e k ( ) t h r o w s E m p t y Q u e u e E x c e p t i o n ; O b j e c t d e q u e u e ( ) t h r o w s E m p t y Q u e u e E x c e p t i o n ; v o i d e n q u e u e ( O b j e c t i t e m ) ;
b o o l e a n i s E m p t y ( ) ; i n t s i z e ( ) ; }
c l a s s E m p t y Q u e u e E x c e p t i o n e x t e n d s E x c e p t i o n {}
c) Take the interface with the specification and modify it, such that a) it declares a class Queue instead of an inter- face, b) implements all methods, c) relates the implemented methods to the specified model using depends and representation clauses.
Exercise 3 Behavioral Subtyping I
a) Does JML allow specification inheritance for invariants?
b) What is the meaning of the keyword "also" in JML specifications?
c) Given the following code:
p u b l i c c l a s s P a r e n t { / /@ r e q u i r e s i >= 0 ; / /@ e n s u r e s \r e s u l t >= i ; i n t m(i n t i ) { . . . } }
p u b l i c c l a s s C h i l d e x t e n d s P a r e n t { / /@ a l s o
/ /@ r e q u i r e s i <= 0 / /@ e n s u r e s \r e s u l t <= i ; i n t m(i n t i ) { . . . } }
What can the result ofm(0)be for a Child instance? Give a full specification ofmfor Child.
d) Do we have behavioral subtyping for the following class hierarchies? Explain why (or why not). If not, give a code fragment, which shows that behavioral subtyping does not hold.
p u b l i c c l a s s A {
/ /@ i n v a r i a n t v a l u e >= 0 ; p r o t e c t e d i n t v a l u e ;
/∗@
@ p u b l i c b e h a v i o r
@ r e q u i r e s a >= 0 ;
@∗/
p u b l i c v o i d s e t (i n t a ) { v a l u e = a ; }
/∗@
@ p u b l i c b e h a v i o r
@ e n s u r e s \r e s u l t >= 0 ;
@∗/
p u b l i c i n t g e t ( ) { r e t u r n v a l u e ; } }
p u b l i c c l a s s B e x t e n d s A {
/∗@
@ p u b l i c b e h a v i o r
@ r e q u i r e s a >= 1 0 ;
@∗/
p u b l i c v o i d s e t L a r g e (i n t a ) { v a l u e = a ; } }
p u b l i c c l a s s C { p r o t e c t e d i n t v a l u e ;
/∗@
@ p u b l i c b e h a v i o r
@ r e q u i r e s a > 0 ;
@∗/
p u b l i c v o i d s e t (i n t a ) { v a l u e = a ; }
/∗@
@ p u b l i c b e h a v i o r
@ e n s u r e s \r e s u l t > 0 ;
@∗/
p u b l i c i n t g e t ( ) { r e t u r n v a l u e ; } }
p u b l i c c l a s s D e x t e n d s C {
/∗@
@ a l s o
@ p u b l i c b e h a v i o r
@ r e q u i r e s a > 1 0 ;
@∗/
p u b l i c v o i d s e t (i n t a ) { v a l u e = a ; }
/∗@
@ a l s o
@ p u b l i c b e h a v i o r
@ e n s u r e s \r e s u l t > 1 0 ;
@∗/
p u b l i c i n t g e t ( ) { r e t u r n v a l u e ; } }
Does the situation change, if we add the following invariant to C:
//@ invariant value > 0;
p u b l i c a b s t r a c t c l a s s E { / /@ p u b l i c m o d e l i n t c o u n t ;
/∗@
@ p u b l i c b e h a v i o r
@ e n s u r e s c o u n t == 0 ;
@∗/
p u b l i c a b s t r a c t v o i d r e s e t ( ) ;
/∗@
@ p u b l i c b e h a v i o r
@ e n s u r e s c o u n t > \o l d ( c o u n t ) ;
@∗/
p u b l i c a b s t r a c t v o i d i n c r e m e n t ( ) ; }
p u b l i c c l a s s F e x t e n d s E {
/ /@ p r i v a t e r e p r e s e n t s c o u n t <− v a l u e ;
p r i v a t e i n t v a l u e ; p u b l i c v o i d r e s e t ( ) {
v a l u e = 0 ; }
/∗@
@ a l s o
@ p u b l i c b e h a v i o r
@ e n s u r e s c o u n t == \o l d ( c o u n t ) + 1 ;
@∗/
p u b l i c v o i d i n c r e m e n t ( ) { v a l u e += 1 ;
} }
Exercise 4 Behavioral Subtyping II
Take the annotated interfaceQueueof Exercise 2 b) and write a classArrayQueuethat implements it. Modify the specifications, such that they ensure thatArrayQueueis a behavioral subtype ofQueue.