FEN KbP: Seminar 2/JML-Intro1 JML Introduktion Specifikation af en Personklasse
FEN KbP: Seminar 2/JML-Intro2 public class Person { private spec_public String name; private int weight; public invariant && weight >= also ensures \result != null; public String toString() { return "Person(\“” +name+"\","+weight+")"; } ensures \result == weight; public int getWeight() { return weight; } requires kgs >= requires weight + kgs >= ensures weight == \old(weight + public void addKgs(int kgs) { weight= weight+kgs; } requires n != null && ensures && weight == public Person(String n) { name= n; weight= 0; }
FEN KbP: Seminar 2/JML-Intro3 Adskild specifikation og implementation public interface PersonIF { public invariant getWeight() >= also ensures \result != null; public pure String toString(); ensures \result >= 0; public int getWeight(); requires kgs >= ensures getWeight() == (\old(getWeight()) + public void addKgs(int kgs); } Specifikation i interface
FEN KbP: Seminar 2/JML-Intro4 public class PersonImp implements PersonIF { private spec_public String name; private int weight; public invariant && weight >= also ensures \result != null; public pure String toString() { return "Person(\"“ +name+"\","+weight+")"; } also ensures \result == weight; public int getWeight() { return weight; } public void addKgs(int kgs) { weight= weight+kgs; } requires n != null && ensures && weight == public PersonImp(String n) { name= n; weight= 0; } public class PersonMain1 { public static void main(String[] args) { PersonIF p= new PersonImp("Kurt"); p.addKgs(99); System.out.println(p.toString()); } Implementation i class interface som statisk type class som dynamisk Implementation i class Constructor skal etablere invarianten.