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:

Similar documents
In-Class Exercises. ETH Zurich. November

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

Assertions, pre/postconditions

Assignment 4: Object creation

Solution 9: Data structures

The Eiffel language. Slides partly based on :

Assignment 2 - Specifications and Modeling

OO Design Principles

Assertions. Assertions - Example

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

Assignment 7: Inheritance

Introduction to Eiffel

Outline. iterator review iterator implementation the Java foreach statement testing

Creating Java Programs with Greenfoot

More Shared Memory Programming

Eiffel: Analysis, Design and Programming. ETH Zurich, September-December Exception handling

Index. Index. More information. block statements 66 y 107 Boolean 107 break 55, 68 built-in types 107

7 Control Structures, Logical Statements

Control flow in Eiffel

Software Architecture 4. July 2005

Software Development Cycle. Unified Modeling Language. Unified Modeling Language. Unified Modeling Language. Unified Modeling Language.

Lecture Chapter 2 Software Development

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

CSE 331 Midterm Exam Sample Solution 2/18/15

Contract Programming For C++0x

Universe Type System for Eiffel. Annetta Schaad

Fortgeschrittene objektorientierte Programmierung (Advanced Object-Oriented Programming)

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

Automated Fixing of Programs with Contracts

Lecture 8: Control Structures I

Object Oriented Program Correctness with OOSimL

CSI33 Data Structures

Programming Languages Seminar. Lecture in the subject of. Yair Moshe

Chapter 4 Defining Classes I

Specification-based Testing of Embedded Systems H. Schlingloff, SEFM 2008

UNIT-II Introduction to UML

An Annotated Language

Lecture 13: Introduction to inheritance and genericity

Assignment 4: Object creation

Outline for Today CSE 142. CSE142 Wi03 G-1. withdraw Method for BankAccount. Class Invariants

JAVA BASICS II. Example: FIFO

Software Architecture. Abstract Data Types

Lecture 9 / Tutorial 8 Software Contracts

Interfaces & Generics

Test-Driven Development (TDD)

Input Space Partitioning

CS 1110, LAB 12: SEQUENCE ALGORITHMS First Name: Last Name: NetID:

EECS2030 Week 7 worksheet Tue Feb 28, 2017

What is the return type of getnameofmonth?

Constants, once routines, and helper functions

Programming By Contract: Designing for Correctness

MIDTERM EXAMINATION - CS130 - Spring 2005

Axiomatic Rules. Lecture 18: Axiomatic Semantics & Type Safety. Correctness using Axioms & Rules. Axiomatic Rules. Steps in Proof

Hoare Logic: Proving Programs Correct

Object-Oriented Framework for Teaching Introductory Programming

Inheritance and Interfaces

Exercise 3 Subtyping and Behavioral Subtyping October 13, 2017

CS 215 Software Design Sample midterm solutions

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

CSE143 Summer 2008 Final Exam Part B KEY August 22, 2008

ETHZ D-INFK Final Exam Booklet

Exercise 3: Objects, Design by Contract. Master Solution

Representation Invariants and Abstraction Functions

COP5621 Exam 4 - Spring 2005

Getting to know Greenfoot

ECSE 321 Assignment 2

The Liskov Substitution Principle

Fondamenti della Programmazione: Metodi Evoluti. Lezione 5: Invariants and Logic

Chapter 3. Describing Syntax and Semantics

Documenting, Using, and Testing Utility Classes

Type checking. Jianguo Lu. November 27, slides adapted from Sean Treichler and Alex Aiken s. Jianguo Lu November 27, / 39

6. Hoare Logic and Weakest Preconditions

Part II. Hoare Logic and Program Verification. Why specify programs? Specification and Verification. Code Verification. Why verify programs?

Exercise 3 Logical Operators, Branching, Loops. Informatik I für Mathematiker und Physiker (HS 2015) Yeara Kozlov

CSE331 Autumn 2011 Midterm Examination October 28, 2011

Solutions Manual. Data Structures and Algorithms in Java, 5th edition International Student Version. M. T. Goodrich and R.

CS158 Section B Exam 1 Key

Why Design by Contract! CS 619 Introduction to OO Design and Development. Design by Contract. Fall 2012

SCHOOL OF COMPUTING, MATHEMATICAL AND INFORMATION SCIENCES Semester 2 Examinations 2008/2009. CI311 Specification and Refinement

Prog Struct & Abstr EXAM 3 Spring 2015, Page 1 of 12

So far. Specifications, conclusion. Abstract Data Types (ADTs) Comparison by Logical Formulas. Outline

Synchronization: Semaphores

The experimental guide: comparing automatic test generation with manual test generation from UML state machine diagrams of a vending machine program

Lecture 14: More Inheritance & Genericity

Exercise 10 Object Structures and Aliasing November 27, 2015

Principles of Software Construction: Concurrency, Part 2

Lesson 10A OOP Fundamentals. By John B. Owen All rights reserved 2011, revised 2014

Contracts. Dr. C. Constantinides. June 5, Department of Computer Science and Software Engineering Concordia University Montreal, Canada 1/71

Einführung in die Programmierung Introduction to Programming

The Design Recipe Fall 2017

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

Unit 4: Client View of a Component Methods

Java: advanced object-oriented features

Harvard School of Engineering and Applied Sciences CS 152: Programming Languages

Specification-based Testing of Embedded Systems H. Schlingloff, SEFM 2008

Name: CSC143 Exam 1 1 CSC 143. Exam 1. Write also your name in the appropriate box of the scantron

CIS 890: Safety Critical Systems

UML class diagrams. Nigel Goddard. School of Informatics University of Edinburgh

University of Utah School of Computing

Reasoning about programs

Transcription:

In-Class Exercises ETH Zurich 1 Contracts ETH students recently designed a special kind of oven for cooking potatoes. Here are some facts about such an oven: each oven is equipped with a door which is either open or closed; the oven is fairly small, therefore only one potato can fit inside; it is only possible to put a potato in or take one out when the door is open; to start or stop cooking, one has to use the start/stop switch; for safety reasons, the oven would not start cooking if its door is open or there is nothing to cook; the door cannot be opened during cooking: cooking has to be stopped first. The following class POTATO OVEN models such an oven. Please fill in the missing contracts (preconditions, postconditions, and class invariants), so that each fact from the informal specification above is reflected in the class interface. Please note the number of dotted lines does not indicate the number of missing contracts. class POTATO OVEN feature Access potato to cook : POTATO The potato inside the oven. feature Status report is door open: BOOLEAN Is the oven door open? is cooking : BOOLEAN Is the oven cooking? is empty: BOOLEAN Is the oven empty? 1

Result = (potato to cook = Void) feature Basic operation open door Open the door. close door Close the door. 2

put (a potato: POTATO) Put a potato into the oven. remove Remove the potato. 3

switch on Turn on the start/stop switch. switch off Turn off the start/stop switch. invariant 4

................................ 5

2 Inheritance Below you see the class GAME CHARACTER. The class represents game characters. There are three types of game characters: dragon, marshmallow man and zombie. Every character has a health level in the range of 0 to 100, where 0 means that the character is dead and 100 that it has full strength. Since zombies are dead by definition, their health level stays at 0 at all times. Each of the character types has a damage potential that it can inflict on others. For all of them the damage doubles if the character is angry. class 2 GAME CHARACTER Listing 1: Class GAME CHARACTER 4 create make 6 feature Initialization 8 make (t: INTEGER) 10 Initialize with type t. 12 t valid : (t = marshmallow man xor t = dragon xor t = zombie) and not (t = marshmallow man and t = dragon and t = zombie) 14 do type := t 16 if type = zombie then health := 0 18 else health := 100 20 22 type set : type = t 24 feature Access 26 type: INTEGER 28 Type of character 30 health : INTEGER Health of character (0: dead, 100: full strength) 32 damage: INTEGER 34 Damage that the character can do do 36 if type = zombie then Result := zombie damage 38 elseif type = marshmallow man then Result := marshmallow man damage 40 else Result := dragon damage 42 6

if is angry then 44 Result := Result 2 46 zombie: not is angry and type = zombie implies Result = zombie damage 48 angry zombie: is angry and type = zombie implies Result = 2 zombie damage dragon: not is angry and type = dragon implies Result = dragon damage 50 angry dragon: is angry and type = dragon implies Result = 2 dragon damage marshmallow man: not is angry and type = marshmallow man implies Result = marshmallow man damage 52 angry marshmallow man: is angry and type = marshmallow man implies Result = 2 marshmallow man damage 54 feature Status report 56 is dead : BOOLEAN 58 Is the character dead? do 60 Result := (health = 0) 62 Result set : Result = (health = 0) 64 is angry: BOOLEAN 66 Is the character angry? (Then it can do more damage!) 68 feature Element change 70 set health (h: INTEGER) 72 Set health to h. 74 h valid : h >= 0 and h <= 100 h for zombie: type = zombie implies h = 0 76 do health := h 78 health set : health = h 80 82 set angry (b: BOOLEAN) Set is angry to b. 84 do is angry := b 86 is angry set : is angry = b 88 90 feature Constants 92 marshmallow man: INTEGER = 1 7

Marshmallow man 94 dragon: INTEGER = 2 96 Dragon 98 zombie: INTEGER = 3 Zombie (is always dead) 100 zombie damage: INTEGER = 1 102 Damage that a zombie does 104 dragon damage: INTEGER = 2 Damage that a dragon does 106 marshmallow man damage: INTEGER = 3 108 Damage that a marshmallow man does 110 invariant 112 type valid : (type = marshmallow man xor type = dragon xor type = zombie) and not ( type = marshmallow man and type = dragon and type = zombie) health valid : health >= 0 and health <= 100 114 zombie always dead: type = zombie implies health = 0 116 The above code does not exhibit a nice object-oriented design and it can hardly be called reusable. Redesign the code such that it uses inheritance instead of the type attribute to represent the three types of game characters. Write a ancestor class NEW GAME CHARACTER and effective descants ZOMBIE, MARSHMALLOW MAN, and DRAGON that inherit from NEW GAME CHARACTER. Your design should result in the deletion of the type attribute. result in the same behavior for the three types of game characters as the original code of class GAME CHARACTER. include semantically equivalent contracts as the original code of class GAME CHARACTER. If a feature stays the same in your re-factored code as in the original code, please indicate it by giving the full feature signature and adding a comment -- See original. Example: is dead : BOOLEAN See original. 8

class NEW GAME CHARACTER 9

10

class ZOMBIE 11

class MARSHMALLOW MAN 12

class DRAGON 13