Tuesday 2/5 Week 4

Chapter 4

Student Notes

 

Objectives

Collect Project 2

Subtypes

Program development

Return last reading quizzes

 

 

 

1.  Subtypes

·        Subtypes allow us to define a

·         

·        Here are two we have seen all ready:

      SUBTYPE Natural IS Integer RANGE 0..Integer’Last;

  SUBTYPE Positive IS Integer Range 1..Integer’Last;

·        Integer’Last is the largest integer value. More info on ‘Last in enumeration types

·        The largest integer on our system is 2147483647 which is 236-1

·        The smallest integer is -2147483648 which is -236

·         

·         

·        SUBTYPE NonNegFloat IS Float RANGE 0..Float’Last;

 

 

 

      

2.  Working with subtypes

SUBTYPE SmallInt IS Integer RANGE -50..50;

SUBTYPE CapitalLetter IS Character RANGE ‘A’..’Z’;

 

X, Y, Z : SmallInt;

NextChar : CapitalLetter;

Hours_Worked : NonNegFloat;

 

The following statements will raise a

 

at run time, but will compile correctly. Why?

 

X := 26;

Y := 25;

Z := X + Y;

 

 

 

NextChar : = ‘a’;

 

 

 

 

 

6.  Compatibility between subtypes

·        Types are considered compatible if

·         

·        (ex. Integer and SmallInt)

·         or

·         ( ex. Positive and SmallInt)

·        X : Integer;

Y : SmallInt;

X := Y; <=

Y := X; <=

 

 

7.  A Handy program development Technique

·        (Program 4.1)

·        Put your algorithm right into the code file as comments. This will help keep you on track as you code. It also largely completes any commenting necessary.

·        Use the NULL; statement to make a valid program