Assignment 2 - Specifications and Modeling

Similar documents
Assignment 2 (Solution)

CMSC 433 Section 0101 Fall 2012 Midterm Exam #1

Assertions. Assertions - Example

EXAMINATIONS 2012 END-OF-YEAR SWEN222. Software Design. Question Topic Marks 1. Design Quality Design Patterns Design by Contract 12

Assertions, pre/postconditions

References: internet notes; Bertrand Meyer, Object-Oriented Software Construction; 10/14/2004 1

ITI Introduction to Computing II

Industrial Programming

What is an Object. Industrial Programming. What is a Class (cont'd) What is a Class. Lecture 4: C# Objects & Classes

C#: advanced object-oriented features

ITI Introduction to Computing II

Inheritance: Definition

Introduction to Eiffel

Exam Duration: 2hrs and 30min Software Design

CSE115 Introduction to Computer Science I Coding Exercise #7 Retrospective Fall 2017

n Specifying what each method does q Specify it in a comment before method's header n Precondition q Caller obligation n Postcondition

CS 215 Software Design Sample midterm solutions

C#: advanced object-oriented features

UNIT 3 ARRAYS, RECURSION, AND COMPLEXITY CHAPTER 11 CLASSES CONTINUED

CS 215 Software Design Sample Midterm Questions

Syntax of Eiffel: a Brief Overview

Commands, and Queries, and Features. Syntax of Eiffel: a Brief Overview. Escape Sequences. Naming Conventions

Chapter 4 Defining Classes I

Outline. iterator review iterator implementation the Java foreach statement testing

Basic Object-Oriented Concepts. 5-Oct-17

An introduction to Java II

Java: advanced object-oriented features

hwu-logo.png 1 class Rational { 2 int numerator ; int denominator ; 4 public Rational ( int numerator, int denominator ) {

Inheritance and Interfaces

CREATED BY: Muhammad Bilal Arslan Ahmad Shaad. JAVA Chapter No 5. Instructor: Muhammad Naveed

Encapsulation. Mason Vail Boise State University Computer Science

In-Class Exercises. ETH Zurich. ETH students recently designed a special kind of oven for cooking potatoes. Here are some facts about such an oven:

CS 201, Fall 2016 Sep 28th Exam 1

APCS Semester #1 Final Exam Practice Problems

Code Contracts in C#

Object Oriented Programming. Week 1 Part 3 Writing Java with Eclipse and JUnit

Code Contracts. Pavel Parízek. CHARLES UNIVERSITY IN PRAGUE faculty of mathematics and physics

Test-Driven Development (TDD)

EXAM Computer Science 1 Part 1

Name Return type Argument list. Then the new method is said to override the old one. So, what is the objective of subclass?

The Java Modeling Language JML

ICOM 4015 Advanced Programming Laboratory. Chapter 4 Introduction to Object Oriented Programming

Building Java Programs

Inheritance. A mechanism for specialization A mechanism for reuse. Fundamental to supporting polymorphism

1. Stack overflow & underflow 2. Implementation: partially filled array & linked list 3. Applications: reverse string, backtracking

Classes. Classes. Classes. Class Circle with methods. Class Circle with fields. Classes and Objects in Java. Introduce to classes and objects in Java.

In-Class Exercises. ETH Zurich. November

Imperative Languages!

Lecture 12: Classes II

CS112 Lecture: Defining Classes. 1. To describe the process of defining an instantiable class

Type Hierarchy. Comp-303 : Programming Techniques Lecture 9. Alexandre Denault Computer Science McGill University Winter 2004

1. Which of the following is the correct expression of character 4? a. 4 b. "4" c. '\0004' d. '4'

index.pdf January 21,

ECE 122. Engineering Problem Solving with Java

Example: Fibonacci Numbers

Use the scantron sheet to enter the answer to questions (pages 1-6)

Computer Science II (20082) Week 1: Review and Inheritance

CS250 Intro to CS II. Spring CS250 - Intro to CS II 1

Software Development. Modular Design and Algorithm Analysis

Outline CSE 142. Specification vs Implementation - Review. CSE142 Wi03 F-1. Instance Variables

Final exam. CS 2110, December 15, 2016, 9:00AM

More Java Basics. class Vector { Object[] myarray;... //insert x in the array void insert(object x) {...} Then we can use Vector to hold any objects.

ITI Introduction to Computing II

ITI Introduction to Computing II

ECSE 321 Assignment 2

Exercise 3 Subtyping and Behavioral Subtyping October 13, 2017

Object-oriented basics. Object Class vs object Inheritance Overloading Interface

Inspector Methods for State Abstraction

C a; C b; C e; int c;

CS 112 Introduction to Programming

Formal Specification and Verification

University of Utah School of Computing

CSC Advanced Object Oriented Programming, Spring Specification

Computer Science II (20073) Week 1: Review and Inheritance

CS1004: Intro to CS in Java, Spring 2005

Advanced JML Erik Poll Radboud University Nijmegen

Solutions to Quiz 1 (March 14, 2016)

Contract-based Programming in Ada 2012 A Tutorial

CS170 Introduction to Computer Science Midterm 2

a correct statement? You need to know what the statement is supposed to do.

SRM ARTS AND SCIENCE COLLEGE SRM NAGAR, KATTANKULATHUR

Abstract Data Types (ADT) and C++ Classes

CMPSCI 187 / Spring 2015 Hangman

CSC Inheritance. Fall 2009

Notes on Chapter Three

CH. 2 OBJECT-ORIENTED PROGRAMMING

Type Hierarchy. Lecture 6: OOP, autumn 2003

Classes, interfaces, & documentation. Review of basic building blocks

Object-Oriented Analysis, Design and Implementation. Case Study Part II

Exercises 1. class member { int membernum = 25; float memberpay; public void Input(cin >> membernum >> memberpay); void Output; }

We have written lots of code so far It has all been inside of the main() method What about a big program? The main() method is going to get really

Contract Programming For C++0x

Object-oriented programming. and data-structures CS/ENGRD 2110 SUMMER 2018

Last name:... First name:... Department (if not D-INFK):...

CSE 143. "Class Relationships" Class Relationships and Inheritance. A Different Relationship. Has-a vs. Is-a. Hierarchies of Organization

Mock exam 1. ETH Zurich. Date: 9./10. November 2009

MSO Lecture Design by Contract"

(a) Write the signature (visibility, name, parameters, types) of the method(s) required

BCS THE CHARTERED INSTITUTE FOR IT. BCS HIGHER EDUCATION QUALIFICATIONS BCS Level 5 Diploma in IT. Object Oriented Programming

5. Specification and Checking

Transcription:

Assignment 2 - Specifications and Modeling Exercise 1 A way to document the code is to use contracts. For this exercise, you will have to consider: preconditions: the conditions the caller of a method has to fulfill to be able to perform the call (e.g., to call the sqrt(x) method, which computes the square root of its argument, the caller has to provide a value x>=0). postconditions: the guarantees the caller gets after the method is executed (e.g., the method sqrt(x) will return a result such that result*result == x). invariants: the conditions that all the instances of a class have to satisfy while they can be observed by the clients (e.g., for a BankAccount the balance >= 0, after every deposit and withdraw operation). Contracts are part of the specification, not of the implementation, so they cannot modify the program state. Therefore, only pure expressions can be used in preconditions, postconditions and invariants. Consider a simple game, in which a player can move left, right, up and down on a n*n board and a partial Java implementation: public class Player { private int x, y; public final int n; public Player(int x, int y, int n) { this.x = x; this.y = y; this.n = n; public int getx() { return x; 1

public int gety() { return y; public void moveleft() { x = x - 1; if (x < 0) { x = n - 1;... Write a suitable invariant for the class Player, a precondition for the constructor and a postcondition for the method moveleft. Exercise 2 Consider the following C# code: public class Bag { private int[] elems; private int count; public Bag(int[] initialelements) { this.count = initialelements.length; int[] e = new int[initialelements.length]; initialelements.copyto(e, 0); this.elems = e; public Bag(int[] initialelements, int start, int howmany) { this.count = howmany; int[] e = new int[howmany]; Array.Copy(initialElements, start, e, 0, howmany); this.elems = e; public int Count() { return count; public int[] GetElements() { return elems; public int RemoveMin() { int m = System.Int32.MaxValue; 2

int mindex = 0; for (int i = 0; i < count; i++) { if (elems[i] < m) { mindex = i; m = elems[i]; count--; elems[mindex] = elems[count]; return m; public void Add(int x) { if (count == elems.length) { int[] b = new int[2*elems.length]; Array.Copy(elems, 0, b, 0, elems.length); elems = b; elems[count] = x; count++; Find class invariants and preconditions for all the methods of the class Bag, and postconditions for the method Add. Express them using the syntax of C# s Code Contracts: At the beginning of each method, Contract.Requires(expr); can be used to denote a precondition. Here, expr should be a pure boolean C# expression referring only to fields and pure methods with greater or equal visibility than the method. For example, in the precondition of a public method, we are not allowed to mention private fields. Similarly, Contract.Ensures(expr); can be used to denote a postcondition. If needed, Contract.ForAll(lower, upper, pred) can be used to express that the predicate pred holds for all integers from lower (inclusive) to upper (exclusive). For example, the predicate is_digit holds for all integers from 0 to 10. Contract.OldValue(expr) can be used in a postcondition to refer to the value of the expr before the execution of the method. Class invariants are denoted in a special contract invariant method. Again, the visibility of all referred fields must be greater or equal to the visibility of the contract invariant method. 3

[ContractInvariantMethod] private void ObjectInvariant() { Contract.Invariant(expr);... Methods which have no side effects can be marked with the [Pure] attribute (in the line before the method declaration). Only pure methods can be used in contract expressions. For example, getters are pure methods, because they do not modify the state. Exercise 3 1. Draw a UML class diagram for the system described below: (a) every student is either undergraduate or graduate (no student can be in both categories at the same time); (b) a student should register at a university, and only registered students are legal students; (c) every student has a unique student ID, and he or she has only one major; (d) students with the same major are regarded as classmates; students can have several classmates. 2. Which properties of the system above cannot be captured using UML class diagrams? Exercise 4 UML allows some properties of a generalization (such as relations) to be redefined, instead of being inherited. The purpose of redefinition is to add more specific constraints, which are particular to the specialized instances, but which do not contradict the existing constraints (the new constraints must be stronger than the old ones). 1. Use redefinition (marked in UML as {redefines property_name) to create the UML class diagram for the following system: (a) a company has one or more employees and each employee works for exactly one company. (b) each employee can work on one or more projects at the same time. (c) junior employees cannot be assigned to more than two projects. 4

(d) summer interns (which are employed for 1 or 2 months) can work on only one development project. 2. Discuss different ways of implementing the UML diagram in Java. 5