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 2010) Practice Sheet 8
Date of Issue: 04.06.10Deadline: 09.06.10 (until 10 a.m. as PDF via E-Mail)
Exercise 1 Introduction to JML
TheJava Modeling Languageallows to specify properties of Java software by using special annotations. The JML homepage (http://www.jmlspecs.org) provides tutorials, papers, and tools you can use to solve the following exercises.
a) Summarize advantages of using a formal specification technique such as JML in comparison to informal ap- proaches. Could you think of any drawbacks caused by the usage of formal specification techniques?
b) Make yourself familiar with the conceptual framework provided by JML. Use the paper „Design by Contract with JML“ by Leavens and Cheon as a starting point.
c) Install one of the JML implementations and make yourself familiar with its usage. For eclipse you can take the JML plugin fromhttp://pm.inf.ethz.ch/research/universes/tools/eclipse/. If you don’t use eclipse take a version of the common JML tools (http://www.eecs.ucf.edu/~leavens/JML/download.shtml). Use the tools for the following exercises to check your specifications and to execute your annotated programs.
d) Specify the following class. Give pre- and postconditions for the constructor and methods and a non-trivial class and loop invariant.
c l a s s B e h a e l t e r { i n t[ ] a ; i n t n ;
B e h a e l t e r ( i n t[ ] i n p u t ) { n = i n p u t . l e n g t h ; a = new i n t[ n ] ;
S y s t e m . a r r a y c o p y ( i n p u t , 0 , a , 0 , n ) ; }
i n t e x t r a c t M i n ( ) {
i n t m = I n t e g e r . MAX_VALUE;
i n t mindex = 0 ;
f o r (i n t i = 0 ; i < n ; i++) { i f ( a [ i ] <m) {
mindex = i ; m= a [ i ] ; }
} n− −;
a [ mindex ] = a [ n ] ; r e t u r n m;
} }
e) It is obvious that the valuencan only decrease over time. How can you specify this using JML?
f) Specify the JDK classjava.io.ByteArrayInputStreamusingJML.
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 datastructure.Hint: Look which model provided by JML may be appropiate.
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.