FEN 2013-04-07UCN T&B - PBA/CodeContract- Intro 1 Code Contract Introduction Specification of a Person Class.

Slides:



Advertisements
Similar presentations
Continuation of chapter 6…. Nested while loop A while loop used within another while loop is called nested while loop. Q. An illustration to generate.
Advertisements

Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 18 Program Correctness To treat programming.
Interfaces CSC 171 FALL 2004 LECTURE 14. Project 1 review public class Rational { private int numerator, denominator; public Rational(int numerator, int.
Problem Solving 5 Using Java API for Searching and Sorting Applications ICS-201 Introduction to Computing II Semester 071.
METHOD OVERRIDING Sub class can override the methods defined by the super class. Overridden Methods in the sub classes should have same name, same signature.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. Multithreading Example from Liang textbook.
FEN KbP: Seminar 2/JML-Intro1 JML Introduktion Specifikation af en Personklasse.
1 Design by Contract with JML CS 3331 Fall 2009 Gary T. Leavens and Yoonsik Cheon. Design by Contract with JML. Available from
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Immutable Objects and Classes.
C# Programming: From Problem Analysis to Program Design1 Creating Your Own Classes C# Programming: From Problem Analysis to Program Design 3rd Edition.
Differences between C# and C++ Dr. Catherine Stringfellow Dr. Stewart Carpenter.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved COS240 O-O Languages AUBG,
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 12 Object-Oriented.
FEN 2012 UCN Technology: Computer Science1 C# - Introduction Language Fundamentals in Brief.
Session 08: Architecture Controllers or Managers Graphical User Interface (GUI) FEN AK - IT Softwarekonstruktion.
Applets and Frames CS 21a: Introduction to Computing I First Semester,
Database Programming Dr. John Abraham. Data Sources Data source specifies the source of the data for an application. Click on Data from Menu Choose show.
Interfaces. –An interface describes a set of methods: no constructors no instance variables –The interface must be implemented by some class. 646 java.
ICS 201 Introduction to Computer Science
Features of Object Oriented Programming:  A class is a collection of things which posses common similarities.  In C#.NET a class is a user defined.
Sample Application Multi Layered Architecture (n-tier): –Graphical User Interface (GUI): Forms, components, controls The Visual Designer in Visual Studio.
ILM Proprietary and Confidential -
Code Contracts Parameterized Unit Tests Tao Xie. Example Unit Test Case = ? Outputs Expected Outputs Program + Test inputs Test Oracles 2 void addTest()
Introduction to Java Classes and Objects. What is a class A class is description of a structure that contains both data and methods – Describes a set.
Java Classes. Consider this simplistic class public class ProjInfo {ProjInfo() {System.out.println("This program computes factorial of number"); System.out.println("passed.
Applets and Frames. Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved L14: GUI Slide 2 Applets Usually.
Creational Pattern: Factory Method At times, a framework is needed to standardize the behavior of objects that are used in a range of applications, while.
1 Interfaces and Abstract Classes Chapter Objectives You will be able to: Write Interface definitions and class definitions that implement them.
Classes. Student class We are tasked with creating a class for objects that store data about students. We first want to consider what is needed for the.
MIT AITI 2004 – Lecture 13 Abstract Classes and Interfaces.
CPSC 481 Fateme Rajabiyazdi #W4. Visual Studio Download Visual Studio 2013 (Ultimate) from – MSDN Academic Alliance Software Center – IT account.
Microsoft Code Contracts How to program Pre-conditions, Post-conditions, and Object Invariants Microsoft Code Contracts1.
 A class is a collection of things which posses common similarities.  In C#.NET a class is a user defined Data type and is Known as Reference.
SourceAnatomy1 Java Source Anatomy Barb Ericson Georgia Institute of Technology July 2008.
CS 350 – Software Design The Decorator Pattern – Chapter 17 In this chapter we expand our e-commerce case study and learn how to use the Decorator Pattern.
OOP in C# - part 1 OOP in C#: –Object Interaction. –Inheritance and Polymorphism (next module). FEN 20121UCN Technology: Computer Science.
CSC 205 Programming II Lecture 4 Abstract Class. The abstract keyword indicate that a class is not instantiable Defining a type which will be specialized.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Fall 2013 Chapter 10 Thinking.
Programming in Java Transitioning from Alice. Becomes not myFirstMethod but …. public static void main (String[] arg) { // code for testing classes goes.
Section 2.2 The StringLog ADT Specification. 2.2 The StringLog ADT Specification The primary responsibility of the StringLog ADT is to remember all the.
Chapter 10 Thinking in Objects
Web Design & Development Lecture 9
Lecture 12 Inheritance.
Chapter 10 Thinking in Objects
using System; namespace Demo01 { class Program
Reference: COS240 Syllabus
2.7 Inheritance Types of inheritance
Chapter 10 Thinking in Objects
Abstract Class, Interface, Package
CS360 Windows Programming
Code Contracts and Pex Peli de Halleux, Nikolai Tillmann
CS/ENGRD 2110 Spring 2018 Lecture 5: Local vars; Inside-out rule; constructors
Implementing Polymorphism
Abstraction Functions and Representation Invariants
CS/ENGRD 2110 Spring 2017 Lecture 5: Local vars; Inside-out rule; constructors
Functions Used to write code only once Can use parameters.
Chapter 9 Thinking in Objects
Interface.
CSC 113: Computer programming II
Introduction to Java Programming
Code Animation Examples
Example with Static Variable
CS/ENGRD 2110 Fall 2018 Lecture 5: Local vars; Inside-out rule; constructors
class PrintOnetoTen { public static void main(String args[]) {
Chapter 13 Exception Handling: A Deeper Look
CS/ENGRD 2110 Spring 2019 Lecture 5: Local vars; Inside-out rule; constructors
Chapter 11 Inheritance and Polymorphism Part 1
Chapter 11 Inheritance and Encapsulation and Polymorphism
INTERFACES Explained By: Sarbjit Kaur. Lecturer, Department of Computer Application, PGG.C.G., Sector: 42, Chandigarh.
CPSC 233 Tutorial 13 March 11/12th, 2015.
Presentation transcript:

FEN UCN T&B - PBA/CodeContract- Intro 1 Code Contract Introduction Specification of a Person Class

Installation Download from this URL: FEN UCN T&B - PBA/CodeContract-Intro2 Choose ‘Premium Edition’ Download the documentation Install the plug-in and open Visual Studio

Visual Studio Use this namespace Add a reference (right- click on the project) to Microsoft.Contracts FEN UCN T&B - PBA/CodeContract-Intro3

Right-click on the project and choose ‘Properties’, then choose ‘ Code Contracts’ FEN UCN T&B - PBA/CodeContract-Intro4

FEN UCN T&B - PBA/CodeContract-Intro5 class Person { private String name; private int weight; public int Weight { get; private set; } [ContractInvariantMethod] private void ObjectInvariant() { Contract.Invariant(!name.Equals("") && weight >= 0); } [Pure] public override String ToString() { Contract.Ensures( Contract.Result () != null); return "Person(\"" + name + "\"," + weight + ")"; } [Pure] public int GetWeight() { Contract.Ensures(Contract.Result () == weight); return weight; } public void AddKgs(int kgs) { Contract.Requires(kgs >= 0); Contract.Requires(weight+kgs >= 0); Contract.Ensures(weight == Contract.OldValue(weight) + kgs); weight = weight + kgs; } public Person(String n) { Contract.Requires(n != null && !n.Equals("")); Contract.Ensures(n.Equals(name) && weight == 0); name = n; weight = } Specification and implementation are bundled together. This is not nice!!!

FEN UCN T&B - PBA/CodeContract-Intro6 Separate specification and implementation [ContractClass(typeof(IPersonContract))] interface Iperson { /// also ensures \result != null; /// [Pure] String ToString(); /// ensures \result >= 0; /// [Pure] //Contract.Ensures(Contract.Result () >= 0); int GetWeight(); /// /// requires kgs >= 0; ensures getWeight() == (\old(getWeight()) + kgs); /// void AddKgs(int kgs); } What we would like: Specification in an interface But specs in Code Contract are method calls, and method calls are not allowed in interfaces...?  Telling that here is a contract

FEN UCN T&B - PBA/CodeContract-Intro7 [ContractClassFor(typeof(IPerson))] internal abstract class IPersonContract : IPerson { [ContractInvariantMethod] private void ObjectInvariant() { Contract.Invariant(GetWeight() >= 0); } public override String ToString() { Contract.Ensures(Contract.Result () != null); return default(string); } public int GetWeight() { Contract.Ensures(Contract.Result () >=0); return default(int); } public void AddKgs(int kgs) { Contract.Requires(kgs >= 0); Contract.Requires(GetWeight()+kgs >= 0); Contract.Ensures(GetWeight() == Contract.OldValue(GetWeight()) + kgs); } Spec in internal abstract class with dummy implementation interface Iperson { [Pure] String ToString(); int GetWeight(); void AddKgs(int kgs); } Type invariant

FEN UCN T&B - PBA/CodeContract-Intro8 class Person: Iperson { [ContractInvariantMethod] private void ObjectInvariant() { Contract.Invariant(!name.Equals("“) && name!= null && weight >= 0); } private String name; private int weight; public Person(String n) { Contract.Requires(n != null&& !n.Equals("")); Contract.Ensures(n.Equals(name) && weight == 0); name = n; weight = 0; } Implementation in the class Representation invariant public override String ToString() { Contract.Ensures(Contract.Result ()!= null); return "Person(\"" + name + "\"," + weight + ")"; } public int GetWeight() { Contract.Ensures(Contract.Result () == weight); return weight; } public void AddKgs(int kgs) { Contract.Ensures(weight == Contract.OldValue(weight) + kgs); weight = weight + kgs; } Constructor must establish the invariant

FEN UCN T&B - PBA/CodeContract-Intro9 static void Main(string[] args) { IPerson p = new Person("Kurt"); Console.WriteLine(p.ToString()); p.AddKgs(99); Console.WriteLine("GetWeight: "+p.GetWeight()); Console.WriteLine(p.ToString()); Console.ReadLine(); p.AddKgs(-9); Console.ReadLine(); Console.WriteLine(p.ToString()); IPerson pp = new Person(""); Console.WriteLine(pp.ToString()); Console.ReadLine(); } interface as static type class as dynamic type