Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Introducing ASML Methods, Values, Constraints, Constants, Variables Lecture 10 Software Engineering COMP201.

Similar presentations


Presentation on theme: "1 Introducing ASML Methods, Values, Constraints, Constants, Variables Lecture 10 Software Engineering COMP201."— Presentation transcript:

1 1 Introducing ASML Methods, Values, Constraints, Constants, Variables Lecture 10 Software Engineering COMP201

2 2 I. Methods Methods are named operations that may be invoked in various contexts The definition of a method may include parameters that will be associated with particular values each time the method is invoked Methods may also specify a return value that is available after the operation has been performed

3 3 The general form for a method definition name (parameter 1 as Type, … parameter n as Type) as Type the values sent to the method, if there any the name of method The type of the return value, if there is one

4 4 Method IsWord() IsWord ( i as Integer ) as Boolean return (0<= i and i <= 0xFFFF) Main() step if IsWord ( 123 ) then Writelone (“123 is a word of memory.”) IsWord evaluates an argument i and returns the value true if i  0 and i  0xFFFF (hexadecimal number). If i doesn’t fit these criteria, IsWord returns the value false. An expression in the form IsWord() is an application of the name IsWord to argument i, which is a long integer.

5 5 Functions and update procedures Functions have no effect on state variables Functions must return a value IsWord ( i as Integer ) as Boolean return (0<= i and i <= 0xFFFF) Update procedures configure the values of state variables for the next sequential step of the machine Update procedures may optionally return a value

6 6 Update procedure power() var x as Integer = 3 var y as Integer = 0 var index as Integer = 3 power (x as Integer, n as Integer) as Integer var result as Integer = 1 var i as Integer = 1 step while i<=n result:=result*x i:=i+1 step return result Main() step y := power(5,3) step WriteLine (“5 cubed =” +y) WriteLine(“3 cubed =” + power(3,index)) x:= power(x, power(2,2)) step WriteLine (“x = ” +x) Output: 5 cubed = 125 3 cubed = 27 x = 81

7 7 Local names for values Statement in the form identifier = expression introduce identifier as a local name for the value given on the right hand side of the equals sign (“ = ”) by expression. Main() mySet = {1, 2, 3} step WriteLine(“mySet has” + asString(size(mySet))+ “ elements. ”) Local names may be introduced within any block of statement. Local names could appear after the “then” keyword of an if … then … else statement

8 8 Local names in sequences of steps A local name can be referenced anywhere in its statement block. In a sequence of steps, local names introduced within a step block are available within any following step block. Main() step mySet1 = {1, 2, 3} WriteLine (“mySet has” + asString(size(mySet1))+ “ elements. ”) step mySet2 = mySet1 union {4, 5, 6} WriteLine(“mySet has” + asString(size(mySet2))+ “ elements. ”)

9 9 Method overloading Method overloading allows you: to use the same name in different methods in each instance to have the compiler choose the correct instance Must be a clear way for the compiler to decide which method should be called for any particular instance

10 10 Method overloading example S = { 1, 8, 2, 12, 13, 6} Max (i as Integer, j as Integer) as Integer return if i > j then i else j Max (s as Set of Integer) as Integer return any m | m in S where not (exists n in S where n>m ) run() step writeln(“The largest number in the set is ” + ( Max(S) ) ) step writeln(“The largest of the two integers is ” + ( Max(2,3) ) ) A series of methods with the same name but differentiated by their parameter lists are overloaded methods

11 11 II.Values What are values? If x and y are values, then we can ask whether x is equal to y If the S is a set, then we can ask whether x is an element of S A value is an immutable element that supports two operations: equality testing and set membership. The term value has the same meaning as in mathematical sets

12 12 Structured values Some values are composed of other values Some structured values are built into AsmL; others may be defined by you ordered pair structure Location base as Integer offset as Integer Location (1,4) is a value of the user-defined structure type Location

13 13 Built in value types Double-precision 64-bit floating-point format type as specified in IEEE 754 Double Single-precision 32-bit floating-point format type as specified in IEEE 754 Float 64-bit signed integer typeLong 32-bit signed integer typeInteger 16-bit signed integer typeShort 8-bit unsigned integer typeByte A type that can be assigned true and false Boolean MeaningData Type

14 14 Built in value types A set containing elements of type A Set of A A table whose entries map of elements of type A to type B Map of A to B A sequence of elements of type A Seq of A Unicode string typeString Unicode character typeChar MeaningData Type

15 15 Built in value types Types in the form t? includes all of the values of type t plus the special value undef. For example, a variable declared as Boolean? could contain either of the Boolean value true or false or the value undef. A? A tuple consisting of elements of type A1, A2, … Tuple values are written in the same form For example, (1,2) is of a value of the built-in type (Integer,Integer) (A1,A2,…) Meaning Data Type

16 16 III. Constraints Assertions Assertions require and ensure statements document constraints placed upon the model Violating a constraint at runtime is called an assertion failure AllTickets = {1..1024} IsTicketID (i as Integer) as Boolean require i > 0 return ( i in AllTickets) ChooseAnyTicket() as Integer ensure IsTicketID(result) return any t | t in AllTickets The keyword result denotes the value of the return statement

17 17 Type constraints Types of constants, variables and method parameters may be specified using the syntax : name as type Types may be explicitly checked using the “ is” operator : value is type an expression will be either true or false, depending on whether the value given is an element of the type Types are not themselves values. For example, you can’t pass a type as an argument

18 18 IV. Constants Constants are fixed, named values Constants can either be global or local Global constants are declared in the beginning of the program Local constants are declared within a statement block The general form of declaring and initialising a constant is: Name [as Type] = value MaxInteger = 100 run() MinInteger = 0

19 19 V. Variables Variables are names given to memory locations. Updating variables is the only way to change a state of a machine. Variables can be complex types such as structures or maps as well as simple types. Here is the general form for declaring and initialising variables: var name [as Type] = value

20 20 Types of variables There are three types of variables in AsmL –Global variables –Local variables –Instance-based variables Instance-based variables are variables that exist for each instance of a class var x as Integer = 10 // shows explicit typing var x = 10 // shows implicit typing var greeting as String = “Hello” // shows a // string variable var y = [1..6] // shows a sequence of // integers from 1 to 6 var menu = {“ham”, “cheese”, “bit”} // shows a // set


Download ppt "1 Introducing ASML Methods, Values, Constraints, Constants, Variables Lecture 10 Software Engineering COMP201."

Similar presentations


Ads by Google