3.1. Enumerated Types.   

Example: Bands in a rainbow are made up of :


red,orange,yellow,green,blue,indigo,violet
=====COLOURS.

Therefore let us define a type colour which has those values for its constituents.

type colour = (red,orange,yellow,green,blue,indigo,violet);

Now we have defined a type of our own and like type


integer /* only integers from ? to ? */
char /* only Unicode characters */

it has bounds and individual elements.

What is type:


integer = -Integer.MAX_VALUE to Integer.MAX_VALUE /*
Integer.MAX_VALUE is largest integer */
char = chr(0) to chr(65535) /* Unicode chars */

Integer.MAX_VALUE is a pre-defined integer constant whose value is that of the largest integer representable on the machine on which the program is running. How do we know what Integer.MAX_VALUE is?

import local.units.sdsu.io.*;
import java.lang.*;
import java.io.*;
public class TestMaximumInteger
{
static PrintStream output = System.out;
public static void main(String argv[]) throws IOException
{
Format.print(output,"%d\n",Integer.MAX_VALUE);
}
}

              DEC10: 34359739367

PDP11: 32767
VAX : ??????

Having defined this new colour type - its values imply a boundary and we can declare variables of this type.

type colour = (red,orange,yellow,green,blue,indigno,violet);
var band:colour;
intensity:array[red..violet] of real;

      Remember array['A'..'Z'] of ...

type colour is called an enumerated type
/* ord,succ,pred */

Java does not have a built-in enumerated type facility but we can create something similar.

The constituents of type colour should not be confused with the characters for the constituents.

i.e. RED != 'RED'.
We CANNOT read or write enumerated types

e.g. band yellow /* This is OK */
output ('Band is of colour - ', band) /* illegal */

Assigning to enumerated types:

i 1 /* integer */
ch ' ' /* char */
band red /* band is of type colour:
assigning a colour constant to band */
band strip /* band and strip must be of type colour:
var var */

3.1.1. Enumerated type example.   



algorithm worktype // Example to show enumerated type.
// Unfortunately Java does not have an
* enumerated type facility. But the pseudo-code concept is still valuable. */
/* our very own type - Days of the week */
type
dayname = (sunday,monday,tuesday,wednesday,
thursday,friday,saturday)
/* we may like to have a type called troolean - true,false,half_half */
studwork = (yes, no, maybe)
/* But note we cannot use true,false in troolean or studwork
* elements have to be UNIQUE - true and false are of type boolean:
* boolean = (true,false). We use (yes, no, maybe)
*/.
var
work:array [sunday..saturday] of boolean // work is an array of boolean
/* work [monday] has a value of either true or false */
study:array [dayname] of studwork
/* alternative: study:array [sunday..saturday] of studwork */
day:dayname /* day is of type dayname */
begin
for
day monday to friday do
work[day] true
endfor
work[saturday] false
work[sunday] false
/* monday to friday we work */
for day monday to friday do
study[day] yes
endfor
study[saturday] no
study[sunday] maybe
/* monday to friday we study, saturday no, sunday maybe */

        /*  now check to see whether we work and/or study

on each day of week */
for day sunday to saturday do /* for each day */
output('On ')
/* convert enumerated type(internal representation)
to legitimate output. */
case day of /* Can use enumerated types. */
sunday: output('sunday')
monday: output('monday')
tuesday: output('tuesday')
wednesday: output('wednesday')
thursday: output('thursday')
friday: output('friday')
saturday: output('saturday')
endcase
/* If the day is saturday/sunday then YAA weekend */
if (day=saturday)or(day=sunday) then
output
('(weekend)')
endif
/* Now check whether we work or not */
output(' I ')
if not work[day] then
output
(' don''t ')
endif
output
('work.')
/* Now check whether we study or not */
output(' I ')
if study[day] = no then
output
(' don''t ')
else if study[day]=maybe then
output
('might')
endif
output
(' study ')
if ord(day)=6 then
output
('(I dance on tabletops)')
endif
output
('.')
endfor
end
.

3.1.2. ord succ pred for Enumerated types.   

Assuming the following:

type colour = (red,orange,yellow,green,blue,indigo,violet)

Since these are enumerated types tehy imply ordinal values for the type.


ord(red) = 0 No equivalent to character
ord(violet) = 6 function chr.

Also successor and predecessor are available via succ and pred functions.

succ(red) -> orange succ(orange) -> yellow
pred(violet) -> indigo pred(indigo) -> blue

NOTE: succ(violet) and pred(red) are undefined.

We could create a new type in Java via the class mechanism:

   class RainbowColour

{
final static int red = 0;
final static int orange = 1;
final static int yellow = 2;
final static int green = 3;
final static int blue = 4;
final static int indigo = 5;
final static int violet = 6;
int colour;
}
and declare variable as:
RainbowColour band = new RainbowColour();

We could then refer to band.colour to get or set a colour value:

   band.colour = RainbowColour.yellow;

if (band.colour == RainbowColour.blue)
:
for(band.colour = RainbowColour.red; band.colour <= RainbowColour.violet;band.colour++)
:

We could even write methods in the class which would act like succ and pred.

  static RainbowColour succ(RainbowColour col)

{
RainbowColour newcolour = new RainbowColour();
newcolour.colour = col.colour+1;
return(newcolour);
}
static RainbowColour pred(RainbowColour col)
{
RainbowColour newcolour = new RainbowColour();
newcolour.colour = col.colour-1;
return(newcolour);
}

What should we do about incrementing or decrementing too much ?

Notice that nothing stops us from using band.colour = 27; which would be terribly wrong. Languages which support enumerated types would make this an error.

How could we stop this error from happening in our solution?