copyright © Michael B. Feldman, All Rights Reserved Lecture 4 – Programs copyright © Michael B. Feldman, All Rights Reserved
Hello.adb with Ada.Text_IO; procedure Hello is ----------------------------------------------------------- --| A first very small, obligatory program --| Author: Michael B. Feldman; last revised September 1999 ----------------------------------------------------------- begin -- Hello Ada.Text_IO.Put_Line (Item => "This is the obligatory Hello program!"); end Hello;
Hello_Terse.adb with Ada.Text_IO; use Ada.Text_IO; procedure Hello_Terse is ----------------------------------------------------- --| A terse version of the Hello program --| Michael B. Feldman; last revised Sept. 1999 ----------------------------------------------------- begin Put_Line ("This is the obligatory Hello program!"); end Hello_Terse; -- we didn’t compile this one -- it works, but we won’t use USE clauses.
Hello_Error.adb -- with Ada.Text_IO; -- use Ada.Text_IO; procedure Hello_Error is ----------------------------------------------------- --| What happens if we omit the with and use clauses? --| Michael B. Feldman; last revised Oct. 2008 ----------------------------------------------------- begin Put_Line ("This is the obligatory Hello program!"); end Hello_Error; -- this one won’t compile
Display_Integer_Error.adb with Ada.Text_IO; with ada.integer_text_io; use Ada.Text_IO; --use ada.integer_text_io; procedure Display_Integer_Error is Number1: Integer; Number2: Integer; begin Number1 := 3; ada.integer_text_io.Put_Line(Number1); end Display_Integer_Error; -- this one has an erroneous parameter for Put_Line -- we discussed it a lot -- Number2 never read and never assigned (warning occurs) -- we commented out the USE clause and added the fully qualified reference
Display_Integer.adb with Ada.Text_IO; use Ada.Text_IO; with Ada.Integer_Text_IO; use Ada.Integer_Text_IO; procedure Display_Integer is Number1: Integer; Number2: Integer; begin Number1 := 3; Put(Number1); New_Line; end Display_Integer; -- this works but we will remove USE clauses and -- substitute fully qualified names
Simple_Addition.adb with Ada.Text_IO; use Ada.Text_IO; with Ada.Integer_Text_IO; use Ada.Integer_Text_IO; procedure Simple_Addition is Number1: Integer; Number2: Integer; begin Number1 := 3; Number2 := -5; Put(Number1); Put(Number2); Put(Number1+Number2); New_Line; end Simple_Addition; -- In this course when you do a computation, store the result before -- printing it. -- In this course we won’t use USE clauses
Simple_Addition_Better.adb with Ada.Text_IO; with Ada.Integer_Text_IO; procedure Simple_Addition_Better is Number1: Integer; Number2: Integer; Number3: integer; begin Number1 := 3; Number2 := -5; Ada.Integer_Text_IO.Put(Number1); Ada.Integer_Text_IO.Put(Number2); number3 := Number1+Number2; Ada.Integer_Text_IO.Put(Number3); ada.text_io.New_Line; end Simple_Addition_better;
Show_Titles.adb with Ada.Text_IO; use Ada.Text_IO; with Ada.Integer_Text_IO; use Ada.Integer_Text_IO; procedure Show_Titles is Number1: Integer; Number2: Integer; begin Number1 := 3; Number2 := -5; Put("Number 1 = "); Put(Number1, 5); New_Line; Put("Number 2 = "); Put(Number2, 5); New_Line; Put("Sum of the two numbers = "); Put(Number1+Number2, 1); New_Line; New_Line; end Show_Titles; -- style problems: has use clauses -- has multiple statements on a line -- outputs the result of a computation without storing it
Show_Titles_better.adb with Ada.Text_IO; with Ada.Integer_Text_IO; procedure Show_Titles_better is Number1: Integer; Number2: Integer; Temp : integer; begin Number1 := 3; Number2 := -5; Ada.Text_IO.Put("Number 1 = "); Ada.Integer_Text_IO.Put(Item => Number1,width=> 5); -- named parameters Ada.Text_IO.New_Line; Ada.Text_IO.Put("Number 2 = "); Ada.Integer_Text_IO.Put(Number2, 5); -- positional parameters ada.text_io.new_line; Ada.Text_IO.Put("Sum of the two numbers = "); Temp = Number1 + Number2; Ada.Integer_Text_IO.Put(Temp, width=> 1); -- 1 positional, 1 named Ada.Text_IO.New_Line(2); ada.integer_text_io.put (width =>5, item => number1); -- parameter order doesn't matter with named parameters end Show_Titles_better; -- conforms to course style guidelines -- when have positional and named parameters, named must follow positional ones
Named_Parameters_Better.adb with Ada.Text_IO; use Ada.Text_IO; with Ada.Integer_Text_IO; use Ada.Integer_Text_IO; procedure Named_Parameters_Better is Number1: Integer; Number2: Integer; begin Number1 := 3; Number2 := -5; Put(Item => "Number 1 = "); Put(Item => Number1, Width => 5); New_Line; Put(Item => "Number 2 = "); Put(Item => Number2, Width => 5); New_Line; Put(Item => "Sum of the two numbers = "); Put(Item => Number1+Number2, Width => 1); New_Line; end Named_Parameters_Better; -- still has use clauses -- still doesn’t store computed result before printing it -- BUT each statement is on a separate line
Show_Float_Without_Use_Clauses.adb with Ada.Text_IO; with Ada.Float_Text_IO; procedure Show_Float_Without_Use_Clauses is Number1: Float; Number2: Float; begin Number1 := 3.5; Number2 := -5.27; ada.text_io.Put(Item => "Number 1 = "); ada.float_text_io.Put(Item => Number1); ada.text_io.New_Line; ada.text_io.Put(Item => "Number 2 = "); ada.float_text_io.Put(Item => Number2, Fore => 5, Aft => 4, Exp => 0); ada.text_io.New_Line; ada.text_io.Put(Item => "Sum of the two numbers = "); ada.float_text_io.Put(Item => Number1+Number2, Fore => 4, Aft => 1); ada.text_io.New_Line; end Show_Float_Without_Use_Clauses;
Conditional_Without_Use_Clauses.adb with Ada.Text_IO; with Ada.Integer_Text_IO; procedure Conditional_Without_Use_Clauses is Number1: Integer; Number2: Integer; LargerNum: Integer; begin Ada.text_io.Put("Please enter integer Number1 >"); Ada.Integer_text_io.Get(Number1); Ada.text_io.Put("Please enter integer Number2 >"); Ada.Integer_text_io.Get(Number2); if Number1 >= Number2 then LargerNum := Number1; else LargerNum := Number2; end if; Ada.text_io.Put("Number 1 ="); Ada.Integer_text_io.Put(Number1); Ada.text_io.New_Line; Ada.text_io.Put("Number 2 ="); Ada.Integer_text_io.Put(Number2); Ada.text_io.New_Line; Ada.text_io.Put("The larger of the two numbers ="); Ada.Integer_text_io.put(LargerNum); Ada.text_io.New_Line(2); end Conditional_Without_Use_Clauses;
Nested_Conditional_Better.adb with Ada.Text_IO; with Ada.Integer_Text_IO; procedure Nested_Conditional_Better is Number1, Number2, Number3: Integer; begin Ada.text_io.Put("Please enter 3 integers >"); Ada.Integer_Text_IO.Get(Number1); Ada.Integer_Text_IO.Get(Number2); Ada.Integer_Text_IO.Get(Number3); Ada.text_io.Put("The largest of the three numbers = "); if Number1 >= Number2 then if Number1 >= Number3 then Ada.Integer_Text_IO.Put(Number1); else Ada.Integer_Text_IO.Put(Number2); end if; else if Number2 >= Number3 then Ada.Integer_Text_IO.Put(Number2); else Ada.Integer_Text_IO.Put(Number3); end if; end if; Ada.text_io.New_Line; end Nested_Conditional_Better;
Multiconditional_Better.adb with Ada.Text_IO; with Ada.Integer_Text_IO; procedure Multiconditional_Better is Number1: Integer; begin Ada.Text_IO.Put("Enter an integer between 1 and 3 inclusive > "); Ada.Integer_Text_io.Get(Number1); if Number1 = 1 then Ada.Text_IO.Put("I'm in case 1"); Ada.Text_IO.New_Line; elsif Number1 = 2 then Ada.Text_IO.Put("I'm in case 2"); Ada.Text_IO.New_Line; elsif Number1 = 3 then Ada.Text_IO.Put("I'm in case 3"); Ada.Text_IO.New_Line; end if; end Multiconditional_Better;
Show_Base.adb with ada.integer_text_io; with ada.text_io; procedure show_base is number : integer; begin number := 63; ada.integer_text_io.put (number); ada.text_io.new_line; ada.integer_text_io.put (number, 5, base => 2); ada.text_io.new_line; ada.integer_text_io.put (number, 5, base => 8); ada.text_io.new_line; ada.integer_text_io.put (number, 5, base => 16); ada.text_io.new_line; end show_base;
Show_attributes.adb with ada.integer_text_io; with ada.text_io; procedure show_attributes is type myArray is array (-3..10) of integer; type yourArray is array (5..30) of integer; type hisArray is array (100..105) of integer; myA : myArray; myY : yourArray; myH : hisArray; lower_bound, upper_bound,temp : integer; begin -- myArray lower_bound := myA'first; upper_bound := myA'last; ada.Integer_text_io.put (lower_bound); ada.Integer_text_io.put (upper_bound); ada.text_io.new_line; -- yourArray lower_bound := myY'first; upper_bound := myY'last; ada.Integer_text_io.put (lower_bound); ada.Integer_text_io.put (upper_bound); yada.text_io.new_line; -- hisArray lower_bound := myH'first; upper_bound := myH'last; ada.Integer_text_io.put (lower_bound); ada.Integer_text_io.put (upper_bound); ada.text_io.new_line; end show_attributes;
More_Show_Attributes.adb with ada.text_io; with ada.Integer_text_io; procedure More_Show_Attributes is temp: integer; begin -- showing proper use of 'pred temp := integer'last; ada.Integer_text_io.put (temp); temp := integer'pred(integer'last); ada.Integer_text_io.put (temp); -- showing proper use of 'succ ada.text_io.new_line; temp := integer'first; ada.Integer_text_io.put (temp); temp := integer'succ(integer'first); ada.Integer_text_io.put (temp); end More_Show_Attributes;