Advanced Material

28
1 Advanced Material The following slides contain advanced material and are optional.

description

Advanced Material. The following slides contain advanced material and are optional. Outline. Syntax comparison: Eiffel vs Java Naming in Eiffel Feature comments: Less is better (sometimes...). Eiffel vs Java: Class declaration. class ACCOUNT end. class Account { }. - PowerPoint PPT Presentation

Transcript of Advanced Material

Page 1: Advanced Material

1

Advanced Material

The following slides contain advanced material and are optional.

Page 2: Advanced Material

2

Outline Syntax comparison: Eiffel vs Java Naming in Eiffel Feature comments: Less is better (sometimes...)

Page 3: Advanced Material

3

Eiffel vs Java: Class declaration

class ACCOUNTend

class Account {

}

Page 4: Advanced Material

4

Eiffel vs Java: inheritance

class ACCOUNTinherit ANYend

public class Account extends Object {

}

Page 5: Advanced Material

5

Eiffel vs Java: feature redefinition

class ACCOUNTinherit ANY

redefine out end

feature

out: STRINGdo

Result := “abc”end

end

public class Account extends Object {

String toString() {return “abc“;

}

}

Page 6: Advanced Material

6

Eiffel vs Java: Precursor vs. super call

class ACCOUNTinherit ANY

redefine out end

feature

out: STRINGdo

Result := Precursor {ANY}

endend

public class Account extends Object {

String toString() {return super();

}

}

Page 7: Advanced Material

7

Eiffel vs Java: deferred vs. abstract

deferred class ACCOUNT

feature deposit (a_num: INT)

deferredend

end

abstract class Account {abstract void deposit(int a);

}

Page 8: Advanced Material

8

Eiffel vs Java: frozen vs. final

frozen class ACCOUNTinherit ANYend

final class Account extends Object {

}

Page 9: Advanced Material

9

Eiffel vs Java: expanded vs. primitive types

expanded class ACCOUNTend

int, float, double, char

Page 10: Advanced Material

10

class ACCOUNTcreate

make

featuremake

do end

end

public class Account { public Account() {}}

Eiffel vs Java: creation features vs. constructors

Page 11: Advanced Material

11

Eiffel vs Java: constructor overloading

classACCOUNT

createmake, make_amount

featuremake

do end

make_amount (a_amount: INT)

do end

end

public class Account { public Account() {} public Account(int a) {}}

Page 12: Advanced Material

12

Eiffel vs Java: method overloading

classPRINTER

featureprint_int (a_int: INTEGER)

do end

print_real (a_real: REAL)do end

print_string (a_str: STRING)do end

end

public class Printer {public print(int i) {}

public print(float f) {} public print(String s) {}}

Page 13: Advanced Material

13

Eiffel: Exception Handling

classPRINTER

featureprint_int (a_int: INTEGER)

locall_retried: BOOLEAN

doif not l_retried then

(create {DEVELOPER_EXCEPTION}).raiseelse

-- Do something alternate.end

rescuel_retried := Trueretry

endend

Page 14: Advanced Material

14

Java: Exception Handling

public class Printer {public print(int i) {

try { throw new Exception()

}catch(Exception e) { }}

}

Page 15: Advanced Material

15

Eiffel vs Java: Conditional

classPRINTER

featureprint

doif True then

… else

… end endend

public class Printer {public print() {

if (true) {...

} else {

...}

}}

Page 16: Advanced Material

16

Eiffel vs Java: Loop 1

printlocal

i: INTEGERdo

fromi := 1

untili >= 10

loop…i := i +

1end

end

public class Printer {public print() {

for(int i=1;i<10;i++) {...

} }

}

Page 17: Advanced Material

17

Eiffel vs Java: Loop 2

printlocal

i: INTEGERdo

fromi := 1

untili >= 10

loopi := i +

1end

end

public class Printer {public print() {

int i=1;while(i<10) {

i++;}

}}

Page 18: Advanced Material

18

Eiffel vs Java: Loop 3print_1

dofrom list.startuntil list.afterloop

list.item.printlist.forth

end end

print_2 do

-- Enable “provisional syntax” in -- the project properies to

-- use “across”across list as e loop e.item.printend

end

public class Printer {public print() {

for(Element e: list) {e.print();

} }

}

Page 19: Advanced Material

19

Eiffel Naming: Classes Full words, no abbreviations (with some exceptions)

Classes have global namespace Name clashes may arise

Usually, classes are prefixed with a library prefix Traffic: TRAFFIC_ EiffelVision2: EV_ Base is not prefixed

Page 20: Advanced Material

20

Eiffel Naming: Features Full words, no abbreviations (with some exceptions)

Features have namespace per class hierarchy Introducing features in parent classes can cause

clashes with features from descendants

Page 21: Advanced Material

21

Eiffel Naming: Locals / Arguments Locals and arguments share namespace with

features Name clashes arise when a feature is

introduced,which has the same name as a local (even in parent)

To prevent name clashes: Locals are prefixed with l_ Some exceptions like “i“ exist Arguments are prefixed with a_

Page 22: Advanced Material

22

Feature comments: Version 1tangent_ from (a_point: POINT): LINE -- Return the tangent line to the current circle -- going through the point `a_point’, if the point -- is outside of the current circle. require outside_circle: not has (a_point)

Example from http://dev.eiffel.com/Style_Guidelines

Page 23: Advanced Material

23

Feature comments: Version 2tangent_ from (a_point : POINT): LINE -- The tangent line to the current circle -- going through the point `a_point’, if the point -- is outside of the current circle. require outside_circle: not has (a_point)

Page 24: Advanced Material

24

Feature comments: Version 3tangent_ from (a_point : POINT): LINE -- Tangent line to current circle from point `a_point’ -- if the point is outside of the current circle. require outside_circle: not has (a_point)

Page 25: Advanced Material

25

Feature comments: Version 4tangent_ from (a_point : POINT): LINE -- Tangent line to current circle from point `a_point’. require outside_circle: not has (a_point)

Page 26: Advanced Material

26

Feature comments: Final versiontangent_ from (a_point : POINT): LINE -- Tangent from `a_point’. require outside_circle: not has (a_point)

Page 27: Advanced Material

27

Feature comments: More informationtangent_ from (a_point : POINT): LINE -- Tangent from `a_point’. -- -- `a_point’: The point from … -- `Result’: The tangent line … -- -- The tangent is calculated using the -- following algorithm: -- … require outside_circle: not has (a_point)

Page 28: Advanced Material

28

Feature comments: Inherited commentstangent_ from (a_point : POINT): LINE -- <Precursor> require outside_circle: not has (a_point)