Eiffel Introduction
Eiffel introduction
Rensselaer, 2000
James C. McKim, JR, Rensselaer At Hartford
K] [N g of @ r k translation
4. Hello World and other examples
4.1 Hello (example)
Class Hello
Creation make
FEATURE
Make IS
- Say Hello to the significant people in the world.
DO
IO.PUTSTRING ("Hello, OOPERS% N")
end
end
Variable IO is a reference, pointing to an object of STANDARD_FILES. Behind We will see how category Hello accesses this variable.
4.2 Eiffel System [Translation 3 / OOSC2E, P196]
l The name of the code file usually should be the same as the names other than the code included, and use .e as the extension.
l So, the category Hello code should be written in a file called Hello.E.
l Each different system should be in different directories.
An example of two systems of Hello and STACK is given below.
4.3 Stack Class
Class my_stack [g]
Creation make
FEATURE
Capacity, Depth: Integer
Push (x: g) IS
- make x The top item.
Require
NOT_FULL: Depth DO Depth: = DEPTH 1 S.PUT (x, depth) Ensure DEPTH = Old Depth 1 TOP = X End - push POP IS - Remove the top item. Require NOT_EMPTY: DEPTH> 0 DO Depth: = depth - 1 Ensure Depth = Old Depth - 1 - Top = The item Remaining on the stack (if any) - That Has Been there The Least Amount of Time. END - POP TOP: G IS - The item That Has Been on The Stack for the Least - Amount of Time. Require NOT_EMPTY: DEPTH> 0 DO Result: = S.Item (DEPTH) END - TOP Feature {none} S: array [g] Make (C: Integer) - Initialize An Empty Stack with Capacity C. Require C> 0 DO Capacity: = C !! s.make (1, Capacity) Ensure Capacity = C DEPTH = 0 END - MAKE END - MY_STACK 4.4 Test_Stack Class test_stack CREATION MAKE_TEST Feature {none} Si: my_stack [integer]; ss: my_stack [string] Make_test Is DO TEST_INTEGER_STACK Test_string_stack end TEST_INTEGER_STACK IS DO !! si.make (3) Si.push (3) Si.push (2) Si.push (1) IO.PUTINT (Si.Depth) o.new_line IO.PUTINT (Si.Top) o.new_line Si.POP IO.PUTINT (Si.Top) o.new_line Si.POP IO.PUTINT (Si.Top) o.new_line Si.POP end Test_String_Stack IS DO !! ss.make (10) SS.PUSH ("in dixie.") SS.PUSH ("I Was") ss.push ("oh, i wish") IO.PUTSTRING (SS.TOP) SS.POP IO.PUTSTRING (SS.TOP) SS.POP IO.PUTSTRING (SS.TOP) o.new_line end end 4.5 Stack Class (another implementation) Class my_stack [g] Creation make Feature {any} Capacity: integer DEPTH: INTEGER IS DO Result: = Current_Depth end Push (x: g) IS - make x The top item. Require NOT_FULL: Depth DO Current_Depth: = current_depth 1 S.PUT (x, current_depth) Top: = X Ensure Depth = Old Depth 1; TOP = X End - push Top: g - The item That Has Been on The Stack for the Least - Amount of Time. - Require - not_empty: depth> 0 POP IS - Remove the top item. Require NOT_EMPTY: DEPTH> 0 DO Current_Depth: = current_depth - 1 IF current_Depth> 0 THEN Top: = S.Item (current_depth) end Ensure Depth = Old Depth - 1 - Top = The item Remaining on the stack (if any) - That Has Been there The Least Amount of Time. END - POP Feature {none} S: array [g] Current_Depth: Integer Make (C: Integer) - Initialize An Empty Stack with Capacity C. Require C> 0 DO Capacity: = C !! s.make (1, Capacity) Current_Depth: = 0 Ensure Capacity = C DEPTH = 0 END - MAKE end (to be continued)