• Keine Ergebnisse gefunden

Exercise1IntroductiontoJML PracticeSheet7 AdvancedAspectsofObject-OrientedProgramming(SS2015)

N/A
N/A
Protected

Academic year: 2022

Aktie "Exercise1IntroductiontoJML PracticeSheet7 AdvancedAspectsofObject-OrientedProgramming(SS2015)"

Copied!
2
0
0

Wird geladen.... (Jetzt Volltext ansehen)

Volltext

(1)

Prof. Dr. A. Poetzsch-Heffter Mathias Weber, M.Sc.

University of Kaiserslautern Department of Computer Science Software Technology Group

Advanced Aspects of Object-Oriented Programming (SS 2015) Practice Sheet 7

Date of Issue: 02.06.15

Deadline: 09.06.15 (before the lecture 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 (http://www.eecs.ucf.edu/~leavens/JML//jmldbc.pdf) as a starting point.

c) Download a version of the common JML tools (e.g.http://www.eecs.ucf.edu/~leavens/JML/OldReleases/

JML.5.5.tar.gz), follow the included install instructions and make yourself familiar with their usage. Use the tools for the following exercises to check your specifications and to execute your annotated programs.Note: The tools only support Java 1.4, so you cannot use generics and the syntax extensions of Java 5 to 8.

d) Specify the following class. Give pre- and postconditions for the constructor and methods and a non-trivial class and loop invariant.

class Container { int[] a;

int n;

Container ( int[] input ){

n = input . length ; a = new int[n ];

System . arraycopy ( input , 0, a , 0, n );

}

int extractMin () {

int m = Integer . MAX_VALUE ; int mindex = 0;

for (int i = 0; i < n; i ++) { if (a[i] < m) {

mindex = i;

m = a[i ];

} } n - -;

a[ mindex ] = a[n ];

return m;

} }

e) It is obvious that the valuencan only decrease over time. How can you specify this using JML?

f) Use the API documentation of the classjava.io.ByteArrayInputStreamto write aJMLspecification for it.

(2)

Exercise 2 Pre- and Postconditions using Assertions

To use pre- and postconditions you do not have to use a complex tool such as JML. You can simply use assertions in your code which check the conditions during execution. These assertions can even be switched on and off, for example when deploying the program. Assertions are a native feature of the Java Programming Language since version 1.4.

a) Make yourself familiar with Java assertions, for example using the Java Language Specification §14.10.

b) Replace the JML annotations by Java assertions that check the same properties in the following code:

public class Person {

private /* @ spec_public non_null @ */

String name ;

private /* @ spec_public @ */

int weight ;

/* @ public invariant ! name . equals ("")

*@ && weight >= 0; @ */

// @ also

// @ ensures \ result != null ; public String toString () {

return " Person (\" " + name + "\" ,"

+ weight + ")";

} // @ also

// @ ensures \ result == weight ; public int getWeight () {

return weight ; }

/* @ also

@ requires n != null && !n. equals ("");

@ ensures n. equals ( name )

@ && weight == 0; @ */

public Person ( String n) { name = n; weight = 0;

} /* @ also

@ requires kgs >= 0;

@ requires weight + kgs >= 0;

@ ensures weight == \ old ( weight + kgs );

@ */

public void addKgs (int kgs ) { if ( kgs >= 0) {

weight += kgs ; } else {

throw new IllegalArgumentException ();

} } }

Exercise 3 Assertions for JML using Method Calls

public class C {

private /* @ spec_public @ */ int f = 0;

// @ public invariant f >= 0;

public void m(O o) { f ++;

o. notify ();

f - -;

} }

You can assume the classOto have a public methodvoid O.notify().

a) Translate the given code so it uses assertions to check the invariant at the relevant positions in the program.

b) Would it be possible to change the invariant tof == 0instead?

Referenzen

ÄHNLICHE DOKUMENTE

Q3: Do responses of bees to land use depend on bee traits (e.g. body size, nesting behaviour, sociality)?.. Site locations

The prima facie duty to reply, in such cases, will be reduced to the less demanding need to cite and positively note these papers in one’s routine work, if pertinent.. Relating

Bei der Wegbeschreibung innerhalb eines Gebäudes sind einige Unterschiede zwischen British Eng- lish und American English zu beachten, wobei sich die vorliegende Unterrichtseinheit

Jedes Stationenlernen bietet einen einheitlichen Handlungskontext, ist also situativ angelegt, damit die Schülerinnen und Schüler nicht von Satz zu Satz und von Station zu

– Enforces “Think before you code”. The developer has to think about the precise meaning of the inter- faces he uses, this allows to detect errors and problem quite early in

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

a) Summarise 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

Some Xpediter commands available during a traditional sourceless debugging test session are GPREGS to display the current contents of the general purpose registers, MEMORY to