Csc 1302, Honors Principles of Computer Science II (Fall 2021)

Week 5 (24 September 2021)

Pseudo Code for Union, Intersect, and Minus

pseudo-code

Tuple Concatenate, Rename and Times Operators

During this week you will write the following methods:
  1. (Tuple.java):
    // This method combines two tuples into one and assigns a new schema to the 
    // result tuple; the method returns the new tuple
    // e.g. t1 = <"jones",20,200> and t2=<1,2,2.5>
    // then t1.concatenate(t2,attr,dom) will be <"jones",20,200,1,2,2.5>
    // with schema attr = <A, R.B, R.C, S.B, S.C, D>
    // and dom = <VARCHAR,INTEGER,INTEGER,INTEGER,INTEGER,DECIMAL>
    public Tuple concatenate(Tuple t, ArrayList<String> attrs, ArrayList<String> doms) {
      Construct new Tuple object, tup, with attrs and doms;
      for each component in the "this" tuple:
        add the component to tup; // look at how you cloned a tuple for how to do this step
      for each component in the Tuple t:
        add the component to tup; // look at how you cloned a tuple for how to do this step
      return tup;
    }
    
  2. (Relation.java) Rename Operator:
    // The rename method takes as parameter an array list of new column names, cnames, 
    // and returns a new relation object that contains the same set of tuples, but with
    // new columns names. We can assume that the size of cnames is same as size of this.attributes
    public Relation rename(ArrayList<String> cnames) {
      Construct new empty attrs and doms array lists;
      Copy all column names from cnames into attrs and all domains from this.domains to doms;
      Construct new Relation object, rel.
      for each tuple in this.table:
        clone the tuple and add to rel.table;
      return rel;
    }
    
  3. (Relation.java) Times Operator:
    // The times method returns the cartesian product of two relations.
    // As an example, let R and S be the following two relations:
    // R(A:VARCHAR, B:INTEGER, C:INTEGER) and S(B:INTEGER, C:INTEGER, D:DECIMAL)
    // and let R contain the tuples {<jones",20,200>, <smith",30,300> and
    // let S contian the tuples {<1,2,2.5>, <100,200,3.86>}
    // The R times S would have the schema 
    // R_TIMES_S(A:VARCHAR, R.B:INTEGER, R.C:INTEGER, S.B:INTEGER, S.C:INTEGER, D:DECIMAL)
    // and the tuples: {<jones",20,200,1,2,2.5>, <jones",20,200,100,200,3.86>,
    //                  <smith",30,300,1,2,2.5>, <smith",30,300,100,200,3.86>}
    // Notice the tuples in the output are formed by combining tuples in the
    // input relations in all possible ways, maintaining the order of columns
    public Relation times(Relation r2) {
      Construct new empty attrs and doms array lists;
      Copy all column names from this.attributes to attrs, renaming any attribute that
         also appears in r2.attributes; copy corresponding this.domains value to doms;
      Copy all column names from r2.attributes to attrs, renaming any attribute that
         also appears in this.attributes; copy corresponding r2.domains value to doms;
      Construct new Relation object, rel; 
      Using nested for-loops, get tuple t1 from this.table and t2 from r2.table and
        concatenate t1 and t2 using the method written earlier; Add the concatenated tuple to rel;
      return rel;
    }
    

Download the Driver Programs and implement all the methods in this assignment. Compile and run the driver programs. You should see the following output when you run Driver.java:

[raj@tinman w4]$ java DriverW6
REL1(COL1:INTEGER,COL2:VARCHAR)
Number of tuples: 5

1111:Robert Adams:
1112:Charles Bailey:
1114:Richard Johnson:
1115:Graham Gooch:
1116:John Miller:

RENAMECOLS(NEWCOL1:INTEGER,NEWCOL2:VARCHAR)
Number of tuples: 5

1111:Robert Adams:
1112:Charles Bailey:
1114:Richard Johnson:
1115:Graham Gooch:
1116:John Miller:

REL1(COL1:INTEGER,COL2:VARCHAR)
Number of tuples: 5

1111:Robert Adams:
1112:Charles Bailey:
1114:Richard Johnson:
1115:Graham Gooch:
1116:John Miller:

REL2(COL1:INTEGER,COL2:VARCHAR)
Number of tuples: 5

1113:John Smith:
1112:Charles Bailey:
1115:Graham Gooch:
1116:John Miller:
1117:Hugh Howell:

R1TIMESR2(REL1.COL1:INTEGER,REL1.COL2:VARCHAR,REL2.COL1:INTEGER,REL2.COL2:VARCHAR)
Number of tuples: 25

1111:Robert Adams:1113:John Smith:
1111:Robert Adams:1112:Charles Bailey:
1111:Robert Adams:1115:Graham Gooch:
1111:Robert Adams:1116:John Miller:
1111:Robert Adams:1117:Hugh Howell:
1112:Charles Bailey:1113:John Smith:
1112:Charles Bailey:1112:Charles Bailey:
1112:Charles Bailey:1115:Graham Gooch:
1112:Charles Bailey:1116:John Miller:
1112:Charles Bailey:1117:Hugh Howell:
1114:Richard Johnson:1113:John Smith:
1114:Richard Johnson:1112:Charles Bailey:
1114:Richard Johnson:1115:Graham Gooch:
1114:Richard Johnson:1116:John Miller:
1114:Richard Johnson:1117:Hugh Howell:
1115:Graham Gooch:1113:John Smith:
1115:Graham Gooch:1112:Charles Bailey:
1115:Graham Gooch:1115:Graham Gooch:
1115:Graham Gooch:1116:John Miller:
1115:Graham Gooch:1117:Hugh Howell:
1116:John Miller:1113:John Smith:
1116:John Miller:1112:Charles Bailey:
1116:John Miller:1115:Graham Gooch:
1116:John Miller:1116:John Miller:
1116:John Miller:1117:Hugh Howell: