CIS 671, Winter 2002
Homework 6: Object-Relational Database Systems - SQL3 Abstract Data Types (ADT's) *
Solutions         Corrected 3/12/02.         :variable-name is now used consistently.
  1. A constructor function called newPC that takes values for the five attributes of the PC ADT and returns a new object of that type. Use the built-in constructor PC() to define this function.
  2.      
         create type PC(
             processorSpeed integer,
             RAM            integer,
             hardDiskDrive  integer,
             CDspeed        integer,
             price          float,
             equals         equalPC,
             less than      ltPC
         );
    
    
  3. Write the following functions for your ADT above, using the extended SQL for internal functions:
    1. A constructor function called newPC that takes values for the five attributes of the PC ADT and returns a new object of that type. Use the built-in constructor PC() to define this function.
    2.  
           function newPC( :ps  integer,
      		      :r  integer,
      		     :hd  integer,
      		     :cd  integer,
      		      :p  float)
              returns PC;
           :npc PC; /* declare local variable */ 
           begin
             :npc := PC();
             :npc.processorSpeed := :ps;
             :npc.RAM := :r;
             :npc.hardDiskDrive := hd;
             :npc.CDspeed := :cd;
             :npc.price := :p
             return :npc;
           end;  
    3. A function value that takes a PC object as argument and returns an "evaluation" of the PC, which is a real number telling how "good" the PC is. The formula for the "value" of the PC is the processor speed plus 4 times the RAM (in megabytes), plus 40 times the hard disk (in gigabytes), and 10 times the speed of the CD.
    4. 
      function value( :pc PC)
              returns float;
           :v float; /* declare local variable */ 
           begin
             :v := :pc.processorSpeed + 4 * :pc.RAM + 40 * :pc.hardDiskDrive
                   + 10 * :pc.CDspeed
             return :v;
           end;
      
      
    5. A function better that takes a PC object as argument and returns another PC object with twice the processor speed, RAM, hard disk, and CD speed, and the same price. You may use the constructor newPC from part (a).
    6.  
           function better( :pc PC)
              returns PC;
           :bpc PC; /* declare local variable */ 
           begin
             :bpc := newPC(
      	       :pc.processorSpeed*2 ,
      	       :pc.RAM*2 ,
      	       :pc.hardDiskDrive*2 ,
      	       :pc.CDspeed*2 ,
      	       :pc.price
                           );
             return :bpc;
           end;  
    7. A function equalPC that serves as the equality for objects. This function reports two PC's are "equal" if they have the same speed, same RAM and hard disk size, regardless of the value of the other components.
    8.      
           function equalPC( :pc1 PC,  :pc2 PC)
              returns boolean;
           begin
      	return( (:pc1.processorSpeed := :pc2.processorSpeed)  AND
                      (:pc1.RAM            := :pc2.RAM)             AND
      	        (:pc1.hardDiskDrive  := :pc2.hardDiskDrive) )
           END
      
      
    9. A function ltPC that serves as the less-than function for the ADT PC. This function finds p1 < p2 if the "value" of PC p1 is less than that of PC p2, where "value" is as defined in part (b). You may use the function value defined in part (b).
    10.      
           function ltPC( :pc1 PC,  :pc2 PC)
              returns boolean;
           begin
      	return( value(:pc1) < value(:pc2) )
           END
      
      

(*) Adapted from Ullman & Widom, A First Course in Database Systems, Prentice-Hall, 1997, pp. 456-457.



D. S. Kerr
October 25, 2001