Example: Bands in a rainbow are made up of :
red,orange,yellow,green,blue,indigo,violet
=====COLOURS.
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 */
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 */
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 : ??????
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 */
The constituents of type colour should not be confused with the characters for the constituents.
e.g. bandyellow /* This is OK */
output ('Band is of colour - ', band) /* illegal */
i1 /* integer */
ch' ' /* char */
bandred /* band is of type colour:
assigning a colour constant to band */
bandstrip /* band and strip must be of type colour:
varvar */
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 daymonday to friday do
work[day]true
endfor
work[saturday]false
work[sunday]false
/* monday to friday we work */
for daymonday 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 daysunday 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.
Assuming the following:
Since these are enumerated types tehy imply ordinal values
for the type.
ord(red) = 0 No equivalent to character
ord(violet) = 6 function chr.
succ(red) -> orange succ(orange) -> yellow
pred(violet) -> indigo pred(indigo) -> blue
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();
band.colour = RainbowColour.yellow;
if (band.colour == RainbowColour.blue)
:
for(band.colour = RainbowColour.red; band.colour <= RainbowColour.violet;band.colour++)
:
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);
}
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?